Your Pages‎ > ‎

Script Examples

Add your script examples below...


Blink IP address when local button is 'long' pressed - Electroguard 1/4/18
Long-pressing the local button for 1.5 secs or more will blink out the node IP address on the local LED  (10 blinks to represent 0)
Short-pressing (less than 1.5 secs) can do something else such as toggle a local relay

Code:Blink IP address

'Stripped down to leave BlinkIP and Short/Long button press code ...  

localIP$ = WORD$(IP$,1)

ledpin = 13                         'Sonoff green LED (active low)

ledoff = 1                          'Sonoff LED Off state

pin.mode ledpin, output

pin(ledpin) = ledoff

buttonpin = 0                       'Sonoff button (active low)

pin.mode buttonpin, input, pullup

interrupt buttonpin, pressed

start = 0                           'Button pressed

stop = 0                            'Button release   (for differentiating between short and long button presses)

wait


pressed:

if pin(buttonpin) = 0 then start = millis else stop = millis

if stop > start then

if stop - start < 1500 then

 'gosub toggle   'toggle local relay with short press

else

 gosub blinkip    'blink IP with long press

endif

endif

return


blinkip:

ledstate = pin(ledpin)

blinkon = 150

blinkoff = 300

blinkpause = 1000

blinkgap = 1400

pin(ledpin) = ledoff

pause blinkpause

for pos = 1 to len(localIP$)

 digitchr$ = mid$(localIP$,pos,1)

 if digitchr$ = "." then

   pause blinkgap

 else

   if digitchr$ = "0" then digit = 10 else digit = val(digitchr$)

   for count = 1 to digit

     if ledoff = 0 then pin(ledpin) = 1 else pin(ledpin) = 0

     pause blinkon

     if ledoff = 0 then pin(ledpin) = 0 else pin(ledpin) = 1

     pause blinkoff

   next count

   pause blinkpause

 end if

next pos

pause blinkgap

pin(ledpin) = ledstate

return



--------------

Memory Monitor - Electroguard  11-5-19 

Shows continual ramfree memory usage, plus clock for reassurance that script is still running.
Slider allows setting a danger_level memory threshold anywhere between 10K min to 35K max.
If ramfree drops below the threshold, it causes a jump to danger: offering opportunity to take pre-emptive evasive actions before script unavoidably halts from 'out of memory' error.
Moving slider towards max will set threshold higher than available ramfree and therefore trigger a jump to danger.
When triggered, shows the final low memory value and time it happened.
Can be use 'as-is' to develop and test pre-emptive actions, then can be stripped down to essentials for including pre-emptive evasive actions into scripts...such as saving important data to non-volatile file then triggering the module to reboot and autorun before it stops from 'out of memory' error.

Update:  Be aware that repeatedly calling ramfree can itself cause hanging due to memory problems.
.
'Memory Monitor
now$=time$               'current time to show script is running
free_mem$=""
danger_level=12000  'minimum memory threshold - allow enough headroom to take evasive action if necessary
min=10000                 'slider minimum memory setting
max=35000                'slider maximum memory setting
timer0 1000, ticker
cls
html$= "Memory Monitor <br>"
html$=html$+ "time: " + textbox$(now$,"time")+"<br>"
html$=html$+ "free mem: " + textbox$(free_mem$,"mem")+"<br>"
html$=html$+ str$(min) + " " + textbox$(danger_level,"thresh") + " " + str$(max) + "<br>"
html$=html$+ " adjust " + slider$(danger_level,min,max,"set")+"<br>"
html$=html$+ cssid$("time","background-color:white;border:0;color:blue;width:78px;text-align:right;")
html$=html$+ cssid$("mem","background-color:white;border:0;color:darkgreen;width:44px;text-align:right;")
html$=html$+ cssid$("set","width:100px;")
html$=html$+ cssid$("thresh","background-color:yellow;color:blue;width:100px;text-align:center;")
html html$
wait

ticker:
free_mem$=str$(ramfree)
now$=time$
if val(free_mem$)<danger_level then goto danger
refresh
return

danger:
timer0 0
wlog "DANGER - low memory = " + free_mem$
html$= cssid$("mem","color:darkred;")
html$=html$+ cssid$("time","color:magenta;")
html$=html$+ cssid$("thresh","background-color:red;color:red;")
html$=html$+ "LOW MEMORY - STOPPED<br>"
html html$
refresh
pause 100
'save important vars
'reboot (assuming module is set to autorun)
end
'-------------------- End --------------------


LED Flasher - Electroguard  12-2-20 
Uses both timers to flash at any rate or duration without blocking other activities
LEDpin=2
pin.mode LEDpin, output
LEDoff=1                                'OFF state depends on whether active low or active high
pin(LEDpin)=LEDoff                 'init led to off
LEDfrequency=1000                'flash rate
LEDduration=100                     'on time (millis)
timer0 LEDfrequency, turnON    
wait

turnON:
timer1 LEDduration, turnOFF    'enable OFF timer
pin(LEDpin)= 1- LEDoff             'turn led ON
return

turnOFF:
timer1 0                                   'disable OFF timer
pin(LEDpin)=LEDoff                   'turn led OFF
return
'-------------------- End --------------------