Watchdog/Scheduler
Posted: Thu May 13, 2021 9:14 pm
This Watchdog/Scheduler is being published as a useful project and also to demonstrate some different examples of Annex CSS style usage.
It is highly annotated, but there's more than meets the eye which is not obvious, so here's a video which should explain it...
https://youtu.be/ScQJJRzViKA
It is highly annotated, but there's more than meets the eye which is not obvious, so here's a video which should explain it...
https://youtu.be/ScQJJRzViKA
Code: [Local Link Removed for Guests]
'Watchdog/Scheduler - by Electroguard - developed om Annex32 1.43.3
'settime 21,5,7, 16,14,54
title$="Watchdog"
showtitle = 1 'show/hide title$
showclock = 1 'show/hide the clock (and schedules)
showschedule = 1 'hide/show the scheduler
showbutton = 1 'show/hide button
showdog = 1 'show/hide watchdog info
showoptions = 1 'show/hide options
showdev = 1 'show/hide development options
scheduler = 1 'enable/disable scheduler - mutually exclusive interaction with enablestat
schedule$ = " 12:15,1 11:15,0 4:15,2 16:15,3 22:15,2 17:15,4 03:15,9" '0=OFF, 1=ON, 2=TOGGLE, 3=BOUNCE, 4=USER1, 9=REBOOT
'schedule$ = " 0:0,1 0:15,0 1:0,1 1:15,0 2:0,1 2:15,0 3:0,1 3:15,0 4:0,1 4:15,0 5:0,1 5:15,0 6:0,1 6:15,0 7:0,1 7:15,0 8:0,1 8:15,0 9:0,1 9:15,0 10:0,1 10:15,0 11:0,1 11:15,0 12:0,1 12:15,0 " '0=OFF, 1=ON, 2=TOGGLE, 3=BOUNCE, 4=USER1, 9=REBOOT
'schedule$ = " 04:15,9" '0=OFF, 1=ON, 2=TOGGLE, 3=BOUNCE, 4=USER1, 9=REBOOT
schedule$ = trim$(schedule$)
times=word.count(schedule$)
clock$ = time$
clocksize$="0.95": clockcol$="black": clockbak$="white" 'clock size, foreground and background colours
'clocksize$="1.8": clockcol$="dimgrey": clockbak$="aliceblue" 'clock size, foreground and background colours
'clocksize$="3.5": clockcol$="yellow": clockbak$="dimgrey" 'clock size, foreground and background colours
'clocksize$="4.5": clockcol$="lime": clockbak$="black" 'clock size, foreground and background colours
buttonsize$="3.3": buttonONcol$="deeppink": buttonOFFcol$ = "darkblue" 'button size, ON and OFF colours
'buttonsize$="4.2": buttonONcol$="red": buttonOFFcol$ = "green" 'button size, ON and OFF colours
butstat$=""
watchdog$ = "192.168.1.1" 'IP address of watchdog client to be pinged
watchdog = 1 '0=disable watchdog, 1=disable after lost contact, 2=continue watching even aftar lost contacts, 3 (or more) = quiet warnings
watchmode = watchdog
lost = 0 'flag=1 when watchdog communication first lost
allowping = 1 '0=disable ping for test purposes, 1=enable watchdog pings
mem=ramfree
memcol$=""
memlow=100000 'set low memory threshhold level to suit your needs '
flashlow = 170000 'set low flash threshhold level to suit your needs
relaypin=2:relayoff=0: 'relay gpio pin and its Off state
pin.mode relaypin, output: pin(relaypin)=(1 - relayoff) 'set relay On at startup
'pin.mode relaypin, output: pin(relaypin)=(relayoff) 'set relay Off at startup
buttonpin=0:pin.mode buttonpin, input, pullup
interrupt buttonpin, pressed
debounce = 100
onhtmlchange changed
onhtmlreload screen
gosub screen
timer0 1000, heartbeat
wlog " Started at " + time$ + " on " + date$
wait
screen:
cls
autorefresh 1000
a$=|<br><br>|
a$=a$ + |<div style='display: table;margin-right:auto;margin-left:auto;text-align:center;'>|
if showtitle = 1 then a$=a$+title$+"<br><br><br>"
if showclock = 1 then
a$=a$ + textbox$(clock$,"clock") + "<br><br><br>"
a$=a$ + cssid$("clock"," border-radius:.2em;background-color:"+clockbak$+";color:"+clockcol$+";font-size:"+clocksize$+"em;")
a$=a$ + cssid$("clock","width:5em;text-align:center;")
a$=a$ + cssid$("clock","font-family:arial;")
' a$=a$ + cssid$("clock","transform: scale(1,1.5);")
' a$=a$ + cssid$("clock","font-weight:bold;")
' a$=a$ + cssid$("clock","font-style:italic;")
endif
if showschedule = 1 then
a$ = a$ + |<table id='schedule' align='center' style="background:AliceBlue; border:.5px solid darkgrey; border-collapse: collapse; font-size: 90%;"><tr><td>|
for c=1 to times
a$ = a$ + " "
select case val (word$(word$(schedule$,c),2,","))
case 0: col$ = "darkgreen"
case 1: col$ = "red"
case 2: col$ = "orangered"
case 3: col$ = "magenta"
case 4: col$ = "blue"
case 9: col$ = "black"
case else: col$ = "darkgrey"
end select
a$ = a$ + |<font color="| + col$ + |">| + word$(word$(schedule$,c),1,",")
a$ = a$ + " "
next c
a$ = a$ + |</td></tr></table>|
a$ = a$ + |<font color="black">|
a$ = a$ + " Scheduler " + checkbox$(scheduler) + |<br><br><br>|
if (scheduler = 1) then css cssid$("schedule","opacity:1;") else css cssid$("schedule","opacity:0.3;")
endif
if showbutton=1 then
a$=a$ + button$("0/1", toggle, "button") + |<br><br><br>|
if pin(relaypin)=relayoff then butstat$=buttonOFFcol$ else butstat$=buttonONcol$
a$=a$ + cssid$("button"," font-size:1em; padding:.5em;color:white;background:"+butstat$+";")
a$=a$ + cssid$("button","height:" + buttonsize$ + "em;width:" + buttonsize$ + "em;border-style:solid;border-radius:10%; border-width:4px;border-color:black;")
' a$=a$ + cssid$("button","height:" + buttonsize$ + "em;width:" + buttonsize$ + "em;border-style:solid;border-radius:50%; border-width:6px;border-color:silver;")
endif
if showdog = 1 then
a$=a$ + "Ping: " + textbox$(watchdog$,"dog") + " " + checkbox$(watchmode) + |<br><br>|
a$=a$ + cssid$("dog", "width:120; text-align:center;")
if watchmode=0 then a$=a$ + cssid$("dog","color:grey; background:GhostWhite;") else a$=a$ + cssid$("dog", "color:black; background:yellow;")
if showdev = 1 then a$=a$ + "Allow ping" + checkbox$(allowping) + |<br><br>|
endif
if showdev = 1 then
a$ = a$ + " RAM free= " + textbox$(mem,"mem") + | <br>|
a$ = a$ + cssid$("mem","color:"+memcol$+";width:56px;text-align:left;font-size:1.1em;border-style:none;")
a$ = a$ + "Flash free= " + |<b id="ff">| + str$(flashfree) + |</b><br><br>|
if flashfree < flashlow then a$=a$ + cssid$("ff","color:deeppink;") else a$=a$ + cssid$("ff","color:darkolivegreen;")
endif
a$=a$ + |<div id='magic' style='display:table;margin-right:auto;margin-left:auto;text-align:center;'>|
a$=a$ + "ShowOptions" + checkbox$(showoptions,"so2") + "</div>"
a$=a$ + "<style>"
a$=a$ + "#magic {opacity: 0;}"
a$=a$ + "#magic:hover{transition: .7s;transition-delay: .5s;opacity: 1 !important;}"
a$=a$ + "</style>"
if showoptions = 1 then
css cssid$("magic","opacity:1;")
a$=a$ + |<div style='display: table;margin-right:auto;margin-left:auto;text-align:center;font-size: 80%;'>|
a$=a$ + "ShowTitle" + checkbox$(showtitle)
a$=a$ + ", ShowClock" + checkbox$(showclock)
a$=a$ + ", ShowSchedule" + checkbox$(showschedule)
a$=a$ + ", ShowButton" + checkbox$(showbutton)
a$=a$ + ", ShowPing" + checkbox$(showdog)
a$=a$ + ", ShowDev" + checkbox$(showdev)
a$=a$ + |<br><br>|
a$=a$ + "</div>"
endif
html a$
return
changed:
refresh
ch$ = HtmlEventVar$
if (instr(ch$,"show") = 1) then gosub screen
if (scheduler = 1) then css cssid$("schedule","opacity:1;") else css cssid$("schedule","opacity:0.3;")
if ch$ = "watchmode" then
lost=0
if watchmode=0 then
css cssid$("dog","color:grey; background:GhostWhite;")
else
css cssid$("dog", "color:black; background:yellow;")
watchmode=watchdog
endif
endif
HtmlEventVar$="": ch$=""
refresh
return
heartbeat:
clock$ = time$
if scheduler = 1 then
for c=1 to times
if (val(word$(word$(schedule$,c),1,":"))=val(word$(clock$,1,":"))) and (val(word$(word$(schedule$,c),2,":"))=val(word$(clock$,2,":"))) and (val(word$(clock$,3,":"))=0) then
select case val(word$(word$(schedule$,c),2,","))
case 0: if (pin(relaypin) = (1 - relayoff)) then gosub turnOFF 'scheduled OFF
case 1: if (pin(relaypin) = relayoff) then gosub turnON 'scheduled ON
case 2: gosub toggle 'scheduled TOGGLE
case 3: gosub bounce 'scheduled BOUNCE
case 4: gosub UserSchedule 'scheduled USER1 action
case 9: Reboot 'scheduled device reboot
end select
endif
next c
endif
if (watchmode > 0) then
if (val(word$(clock$,3,":")) mod 10=0) then 'use this line to ping evry 10 seconds
' if (val(word$(clock$,3,":"))=0) then 'use this line to ping evry minute
if (allowping = 1) and (ping(watchdog$) = 1) then
css cssid$("dog", "color:black; background:springgreen;")
watchmode = watchdog
lost = 0
else
if watchmode > 2 then
css cssid$("dog", "color:black; background:yellow;")
watchmode = watchmode - 1
else
if lost = 0 then
css cssid$("dog", "color:black; background:#ff000080;")
wlog "watchdog lost contact with " + watchdog$ + " at " + clock$ + " on " + date$
lost = 1
gosub UserRecovery 'optional users recovery branch
if watchmode = 1 then watchmode = 0 'watchdog disable after lost contact, add any other required actions
endif
endif
endif
endif
endif '
mem=ramfree
if mem>memlow then css CSSID$("mem","color:darkgreen;") else css CSSID$("mem","color:orangered;")
return
UserRecovery:
wlog "users watchdog recovery code"
gosub bounce
return
UserSchedule:
wlog "scheduled user1 occured at " + time$
return
Pressed:
pause debounce
if pin(buttonpin) = 0 then gosub toggle
return
TurnON:
if pin(relaypin) = relayoff then pin(relaypin) = 1 - relayoff: css cssid$("button", "background:" + buttonONcol$ + ";")
return
TurnOFF:
if pin(relaypin) <> relayoff then pin(relaypin) = relayoff: css cssid$("button", "background:" + buttonOFFcol$ + ";")
return
Toggle:
if pin(relaypin) = relayoff then gosub TurnON else gosub TurnOff
return
Bounce:
gosub Toggle
pause 5000
gosub Toggle
return
End '----- End -----