Asked — Edited

Coding Help Please

Good Morning,

I am working on a project and I need help with some code. The code I wrote monitors all 24 Digital Inputs, takes that data and places into a string value which is used to display. I then have if then statements to check for a one. I then have that output trigger the string value $FAA. If the condition is met then an email is sent out.

I guess what I am asking is if the condition is met for an email to be sent, I only need an email sent once. The code I have wrote, it will email a lot.

If any more coder with a lot more knowledge than me could take a look for me it would be appreciated.

Code posted below.



:START
#init all values

$B1T1= ""
$B1T2= ""
$B1T3= ""
$B1T4= ""
$B1T5= ""
$B1T6= ""
$B2T1= ""
$B2T2= ""
$B2T3= ""
$B2T4= ""
$B2T5= ""
$B2T6= ""
$B2T7= ""
$B3T1= ""
$B3T2= ""
$B3T3= ""
$B3T4= ""
$B3T5= ""
$B3T6= ""
#####################
$daymode = ""
$twimode = ""
$nitemode = ""
#####################
$mode = ""
$flsh = ""


:READ
#Read Inputs to get status

$B1T1 = GetDigital(D0)
sleep(10)
$B1T2 = GetDigital(D1)
sleep(10)
$B1T3 = GetDigital(D2)
sleep(10)
$B1T4 = GetDigital(D3)
sleep(10)
$B1T5 = GetDigital(D4)
sleep(10)
$B1T6 = GetDigital(D5)
sleep(10)
$B2T1 = GetDigital(D6)
sleep(10)
$B2T2 = GetDigital(D7)
sleep(10)
$B2T3 = GetDigital(D8)
sleep(10)
$B2T4 = GetDigital(D9)
sleep(10)
$B2T5 = GetDigital(D10)
sleep(10)
$B2T6 = GetDigital(D11)
sleep(10)
$B2T7 = GetDigital(D12)
sleep(10)
$B3T1 = GetDigital(D13)
sleep(10)
$B3T2 = GetDigital(D14)
sleep(10)
$B3T3 = GetDigital(D15)
sleep(10)
$B3T4 = GetDigital(D16)
sleep(10)
$B3T5 = GetDigital(D17)
sleep(10)
$B3T6 = GetDigital(D18)
sleep(10)
$daymode = GetDigital(D19)
sleep(10)
$twimode = GetDigital(D20)
sleep(10)
$nitemode = GetDigital(D21)
sleep(10)
$mode= GetDigital(D22)
sleep(10)
$flsh = GetDigital(D23)

#CONVERT 0 or 1 to pass or fail

if ($B1T1 = 0)
$B1T1D = "PASS"
elseif ($B1T1 = 1)
$B1T1D = "FAIL"
endif

if ($B1T2 = 0)
$B1T2D = "PASS"
elseif ($B1T2 = 1)
$B1T2D = "FAIL"
endif

if ($B1T3 = 0)
$B1T3D = "PASS"
elseif ($B1T3 = 1)
$B1T3D = "FAIL"
endif

if ($B1T4 = 0)
$B1T4D = "PASS"
elseif ($B1T4 = 1)
$B1T4D = "FAIL"
endif

if ($B1T5 = 0)
$B1T5D = "PASS"
elseif ($B1T5 = 1)
$B1T5D = "FAIL"
endif

if ($B1T6 = 0)
$B1T6D = "PASS"
elseif ($B1T6 = 1)
$B1T6D = "FAIL"
endif


if ($B2T1 = 0)
$B2T1D = "PASS"
elseif ($B2T1 = 1)
$B2T1D = "FAIL"
endif

if ($B2T2 = 0)
$B2T2D = "PASS"
elseif ($B2T2 = 1)
$B2T2D = "FAIL"
endif

if ($B2T3 = 0)
$B2T3D = "PASS"
elseif ($B2T3 = 1)
$B2T3D = "FAIL"
endif

if ($B2T4 = 0)
$B2T4D = "PASS"
elseif ($B2T4 = 1)
$B2T4D = "FAIL"
endif

if ($B2T5 = 0)
$B2T5D = "PASS"
elseif ($B2T5 = 1)
$B2T5D = "FAIL"
endif

if ($B2T6 = 0)
$B2T6D = "PASS"
elseif ($B2T6 = 1)
$B2T6D = "FAIL"
endif

if ($B2T7 = 0)
$B2T7D = "PASS"
elseif ($B2T7 = 1)
$B2T7D = "FAIL"
endif

if ($B3T1 = 0)
$B3T1D = "PASS"
elseif ($B3T1 = 1)
$B3T1D = "FAIL"
endif

if ($B3T2 = 0)
$B3T2D = "PASS"
elseif ($B3T2 = 1)
$B3T2D = "FAIL"
endif

if ($B3T3 = 0)
$B3T3D = "PASS"
elseif ($B3T3 = 1)
$B3T3D = "FAIL"
endif

if ($B3T4 = 0)
$B3T4D = "PASS"
elseif ($B3T4 = 1)
$B3T4D = "FAIL"
endif

if ($B3T5 = 0)
$B3T5D = "PASS"
elseif ($B3T5 = 1)
$B3T5D = "FAIL"
endif

if ($B3T6 = 0)
$B3T6D = "PASS"
elseif ($B3T6 = 1)
$B3T6D = "FAIL"
endif

if ($daymode = 1)
$daymodeD = "YES"
else 
$daymodeD = ""
EndIf 

if ($twimode = 1)
$twimodeD = "YES"
else 
$twimodeD = ""
endif

if ($nitemode = 1)
$nitemodeD = "YES"
else 
$nitemodeD = ""
endif 


#WHAT TO DO IN CASE OF FAILURE


if ($B1T1 OR $B1T2 OR $B1T3 OR $B1T4 OR $B1T5 OR $B1T6= 1)
$FAA = "CALL FAA AT 1-877-487-6867"
ELSE 
$FAA = ""
endif

if ($B2T1 OR $B2T2 OR $B2T3 OR $B2T4 OR $B2T5 OR $B2T6 OR $B2T7= 1)
$FAA = "CALL FAA AT 1-877-487-6867"
ELSE 
$FAA = ""
endif

if ($B3T1 OR $B3T2 OR $B3T3 OR $B3T4 OR $B3T5 OR $B3T6= 1)
$FAA = "CALL FAA AT 1-877-487-6867"
ELSE 
$FAA = ""
endif

#RUN COMMAND TO SEND EMAIL
if ($FAA = "CALL FAA AT 1-877-487-6867")
EXEC("C:\towerlights\email.bat")
else
goto(READ)
endif




ARC Pro

Upgrade to ARC Pro

ARC Pro is more than a tool; it's a creative playground for robot enthusiasts, where you can turn your wildest ideas into reality.

#1  

Once it triggers the call, do you want the script to stop until you manually restart it or do you want it to keep checking the condition and reset to start calling on next failure when everything is working again? (the former is probably easier).

Alan

#2  

The script need to continue on. To check the other ports. I also have a mobile page I created that has a visual also.

#3  

OK. Need to think about it some more. That adds to the complexity. You are probably going to need a separate "$notified_port_dx" variable for each port that suppresses notification if set to "y" and resets to "n" if you get a good read on the port.

Alan

#4  

Do I need to create a string value that stores the date? Compares the date and then decides how old the date is? The time it takes to have the particular port cleared could take days.

Or just comment out the email portion of the code and think about it.

I just now had a thought that a counter could help, but I would need to know how many possible times the counter would count within a certain time frame...

#5  

There are probably cleaner ways to do this with arrays. Also, might want to do file write and reads rather than storing everything in live variables so that you can restart the EZ-B, ARC, or the PC running it without losing current status.

I am hoping one of the more advanced scripting wizards will poke their head in this thread with some better ideas.

Alan

PRO
USA
#6  

Alan raised an important issue.

how do you deal with windows reboots ? you will need to manually logon, launch the ARC, connect to EZB, etc.

my suggestion: use a powershell script add a windows schedule task to run the powershell script

#7  

@ptp I will handle reboots on a monthly - bi-weekly basis. I can add a script to launch the script upon startup.

There will be some manual intervention with this. Thank You.

I think I will just forgo the email portion.

#8  

@thetechguru So, instead of having for instance a 0 or 1. Have a third condition.

So to expand, I will have to create an array 24 long by 3 wide...... Let me see what I can come up with.

Thanks.

PRO
Synthiam
#9  

To be clear - is your intention to send an email on change of digital state only?

PRO
USA
#10  

ok i just finished a power shell script:


if ($env: Processor_Architecture -ne "x86" )  { 
	write-warning 'Launching x86 PowerShell'
	&"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -noninteractive -noprofile -file $myinvocation.Mycommand.path -executionpolicy bypass
	exit
}

[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\EZ-Robot Inc\EZ-Builder\EZ_B.dll")
$ezb = New-Object -TypeName EZ_B.EZB

$ezb.Connect("192.168.18.85")
Start-Sleep -Milliseconds 500
if (-Not $ezb.IsConnected) {
	write-warning "Unable to connect to EZ-B"
} 

$body = ""
$failures = 0
for ($p=0; $p -le 23; $p++){ 
	$body = $body + "Port " + $p
	if ($ezb.Digital.GetDigitalPort($p)) {
		$body = $body + " PASS"
	}
	else {
		$body = $body + " FAIL"
		$failures = $failures + 1
	}
	$body = $body + "`n"
}
$ezb.Disconnect()

if ($failures -gt 0) {
	#print the message
	$body

	$secpasswd = ConvertTo-SecureString "put-your-password-here-hopefully-no-one-will-see-it" -AsPlainText -Force
	$credentials = New-Object System.Management.Automation.PSCredential ( "your-account@gmail.com ", $secpasswd )

	$mailParam = @{
		To = "your-account@gmail.com"
		From = "your-account@gmail.com"
		Subject = "EZB Port status"
		Body = $body
		SmtpServer = "smtp.gmail.com"
		Port = 587
		Credential = $credentials
		UseSsl = 1
	}
	Send-MailMessage @mailParam 
}

PRO
USA
#11  

if you want a EZScript version:


$body = ""
$failures = 0
REPEAT ($p, 0, 23, 1)
   $body = $body + "Port: " + tostring($p)
   if (GetDigital($p))
      $body = $body + " PASS"   
   else
      $body = $body + " FAIL"
      $failures = $failures + 1
   endif 
   #ezscript does not support new line
   $body = $body + " ||| " 
ENDREPEAT
if ($failures>0)
   #send email ...
   print($body)
endif

PS: I have changed REPEATWHILE to REPEAT is more neat! Thanks DJ :)

PRO
Synthiam
#12  

Here... This will trigger when the state of a digital port changes within the loop.


definearray($lastState, 24, false)

:loop

REPEAT ($cnt, 0, 23, 1)

  $v = getdigital($cnt)

  IF ($v != $lastState[$cnt])

    sayezbwait("Port " + $cnt + " to " + $v)

    $lastState[$cnt] = $v

  ENDIF

ENDREPEAT

sleep(100)

goto(loop)

#13  

@DJ and @ptp: Wow! Thank You! Give me a few years to process that code!

PRO
Synthiam
#14  

Here, i'll add some comments for you in the code...

What this will do is loop through all 24 digital ports and check their status. If the status of the port is different than the last time it was checked, a message will be spoken, which you can substitute for your email code. The $lastState array holds the last value of the digital port so that it doesn't keep notifying.

This code will only notify when the state of a digital port has changed.


# create an array that has 24 indexes (one for each digital port)
definearray($lastState, 24, false)

:loop

# Loop from 0 to 23, for each digital port
REPEAT ($cnt, 0, 23, 1)

  # Get the value of the digital port
  $v = getdigital($cnt)

  # check if the current digital port value does not equal the last value
  IF ($v != $lastState[$cnt])

    # The values are different, so let's notify the admin
    sayezbwait("Port " + $cnt + " to " + $v)

    # Set the last state to the current state so we do not keep sending emails on each loop
    $lastState[$cnt] = $v

  ENDIF

ENDREPEAT

# pause for a short time to not flood the data channel
sleep(100)

goto(loop)

PRO
Synthiam
#16  

Here's an smtp server to send email as well: https://synthiam.com/redirect/legacy?table=plugin&id=128

#17  

@DJ I am trying to use the email plug in. I read the how to on it. I have pre configured the gear on the email client. Now, to use the client, do I need to just say ControlCommand() and the "SMTP Client" plus the code variable that you mentioned above?

Thank You.

PRO
Synthiam
#18  

Here's the link to the smtp plugin with code examples: https://synthiam.com/redirect/legacy?table=plugin&id=128

Have fun!

#19  

@DJ,

What does it mean when an error says "Limits are outside of the array." I changed from 23 to 19.

Thanks