-->
Page 1 of 3

Aquarium light timer

PostPosted: Sun Jun 12, 2016 3:45 pm
by russm
This is a simple on/off control for aquarium lights. I'm using the Electrodragon Wifi IoT Relay Board http://www.electrodragon.com/product/wifi-iot-relay-board-based-esp8266/.
It is currently running under V3.0 Alpha 8
It exercises timers, io pins, writing to files, use of time command (which still confuses me as DST does not seem to work as I would expect so I manually tweak it).

Here is my code so far. Basics work but sometimes it crashes, sometimes so bad it decides to delete default.bas so save often to notepad, etc.

Eventually I may set up a ramp timer for LED lighting. Made one using PIC but it's kind of nice to have a web interface and NTP for time!

Code: Select all'******************************************************************************
'*  Aquarium Light Timer for controlling two lights/devices                   *
'*  By Russ McIntire 6/11/16                                                  *
'*  Electrodragon Wifi IoT Relay Board                                        *
'*  http://www.electrodragon.com/product/wifi-iot-relay-board-based-esp8266/  *
'*  GPIO 12 and 13 are relay controlled                                       *
'*  GPIO14 is for DHT                                                         *
'*                                                                            *
'*  Not optimized, may not work crossing 24 HR boundary                       *
'******************************************************************************

memclear

DST = read.val(DaylightSavings)
timezone = read.val(TZ)

if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif

timesetup(tz,DST)
delay 5000


let relay1 = 12  'pin for relay1
let relay2 = 13  'pin for relay2


Relay1on = read(relay1ontime)
Relay1off = read(relay1offtime)
Relay2on = read(relay2ontime)
Relay2off = read(relay2offtime)


cls
wprint "Aquarium Light Timer Running"
wprint "<br>"
wprint "<br>"
wprint "The time is currently: "
wprint time("hour:min")
wprint "<br>"

wprint "Timezone: "
textbox timezone
wprint "<br>"
wprint "DST:         "
textbox DST
wprint "<br>"
button "Save Timezone", [timezone_update]

wprint "<br>"
wprint "<br>"
wprint "Enter on/off times below and save when done: <br>"
wprint "Valid times are: 00:00 to 23:59"
wprint "<br>"
wprint "Relay 1 - 24 hour (HH:MM) ON "
textbox Relay1on
wprint "OFF "
textbox Relay1off
wprint "<br>"
wprint "Relay 2 - 24 hour (HH:MM) ON "
textbox Relay2on
wprint "OFF "
textbox Relay2off
wprint "<br>"
button "Save Set Times", [Calculate]

gosub [Calculate_Mins]
timer 30000, [CheckTime]
wait
end


[timezone_update]
write(DaylightSavings,str(DST))
write(TZ,str(timezone))
if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif
timesetup(tz,DST)
delay 5000
wprint "<br>"
wprint "Timezone Updated <br>"
wprint "The Updated time is: "
wprint time("hour:min")
wprint "<br>"
wait

[Calculate]
'Persist relay on and off times
write(relay1ontime,Relay1on)
write(relay1offtime,Relay1off)
write(relay2ontime,Relay2on)
write(relay2offtime,Relay2off)

print "Updated"
print "Relay 1 On: " & Relay1on & " Off: " & Relay1off & "<br>"
print "Relay 2 On: " & Relay2on & " Off: " & Relay2off & "<br>"
gosub [Calculate_Mins]
wait

[Calculate_Mins]
r1onh = val(left(Relay1on,2))
r1onm = val(right(Relay1on,2))
r1offh = val(left(Relay1off,2))
r1offm = val(right(Relay1off,2))
r2onh = val(left(Relay2on,2))
r2onm = val(right(Relay2on,2))
r2offh = val(left(Relay2off,2))
r2offm = val(right(Relay2off,2))
r1onminutes = r1onh * 60 + r1onm
if r1onminutes > 1439 then
 r1onminutes = 1439
endif
r1offminutes = r1offh * 60 + r1offm
if r1offminutes > 1439 then
 r1offminutes = 1439
endif
r2onminutes = r2onh * 60 + r2onm
if r2onminutes > 1439 then
 r2onminutes = 1439
endif
r2offminutes = r2offh * 60 + r2offm
if r2offminutes > 1439 then
 r2offminutes = 1439
endif
return


[CheckTime]
ct = time("hour:min")
cth = val(left(ct,2))
ctm = val(right(ct,2))
currminutes = cth * 60 + ctm

'Set Relay 1 state
if r1onminutes <= currminutes then
 if currminutes < r1offminutes then
  io(po,relay1,1)
 else
  io(po,relay1,0)
 endif
else
 io(po,relay1,0)
endif

'Set Relay 2 state
if r2onminutes <= currminutes then
 if currminutes < r2offminutes then
  io(po,relay2,1)
 else
  io(po,relay2,0)
 endif
else
 io(po,relay2,0)
endif

wait

Re: Aquarium light timer

PostPosted: Mon Jun 13, 2016 8:49 pm
by russm
Updated code quite a bit. Still has the crossing day boundary issue but not a problem for an aquarium light timer which is only supposed to be on during the day!

Fixed persisting the on/off times, time zone, DST and addressed my GUI issues. Not sure how to do anything but refresh the entire GUI. Any thoughts would appreciate it.

Here's the latest code which works and does not seem to crash. Still running under V3, Alpha 8.

Code: Select all'******************************************************************************
'*  Aquarium Light Timer for controlling two lights/devices                   *
'*  By Russ McIntire 6/13/16                                                  *
'*  Electrodragon Wifi IoT Relay Board                                        *
'*  http://www.electrodragon.com/product/wifi-iot-relay-board-based-esp8266/  *
'*  GPIO 12 and 13 are relay controlled                                       *
'*  GPIO14 is for DHT                                                         *
'*                                                                            *
'*  Not optimized, may not work crossing 24 HR boundary                       *
'******************************************************************************

memclear

DST = read.val(DaylightSavings)
timezone = read.val(TZ)

if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif

timesetup(tz,DST)
delay 5000

ct = time("hour:min")
uvsr = "Running..."

let relay1 = 12  'pin for relay1
let relay2 = 13  'pin for relay2


Relay1on = read(relay1ontime)
Relay1off = read(relay1offtime)
Relay2on = read(relay2ontime)
Relay2off = read(relay2offtime)


cls
wprint |<HTML>|
wprint |<HEAD>|
wprint |<h2>Aquarium Light Timer</h2>|
wprint |</HEAD>|
wprint |<body bgcolor="grey">|
wprint "<br>"

wprint "The time is currently: "
wprint htmlvar(ct)  ' time("hour:min")
wprint "<br> <br>"

wprint "Timezone: "
textbox timezone
wprint "<br>"
wprint "DST:         "
textbox DST
wprint "<br>"
button "Save Timezone", [timezone_update]
wprint "<br>"

wprint "<br>"
wprint "Enter on/off times below and save when done: <br>"
wprint "Valid times are: 00:00 to 23:59"
wprint "<br>"
wprint "Relay 1 - 24 hour (HH:MM) ON "
textbox Relay1on
wprint "OFF "
textbox Relay1off
wprint "<br>"
wprint "Relay 2 - 24 hour (HH:MM) ON "
textbox Relay2on
wprint "OFF "
textbox Relay2off
wprint "<br>"
button "Save Set Times", [Calculate]
wprint "<br> <br>"

gosub [Calculate_Mins]
gosub [CheckTime]

wprint "Relay 1 is " & htmlvar(relayone) & "<br>"
wprint "Relay 2 is " & htmlvar(relaytwo) & "<br> <br>"

wprint htmlvar(uvsr)


timer 60000, [UpdateRelays]  'Will check time, update GUI and operate relays every minute

wait
end


[timezone_update]
'Persist timezone infomration
write(DaylightSavings,str(DST))
write(TZ,str(timezone))

if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif

timesetup(tz,DST)
delay 5000

serialprintln "Timezone Updated"
serialprintln "The Updated time is: "
serialprintln time("hour:min")

uvsr = "Updated..."
returngui
wait

[Calculate]
'Persist relay on and off times
write(relay1ontime,Relay1on)
write(relay1offtime,Relay1off)
write(relay2ontime,Relay2on)
write(relay2offtime,Relay2off)

serialprintln "On/Off times adjusted"
serialprintln "Relay 1 On: " & Relay1on & " Off: " & Relay1off
serialprintln "Relay 2 On: " & Relay2on & " Off: " & Relay2off

uvsr = "Updated..."
gosub [Calculate_Mins]
returngui
wait

[Calculate_Mins]
r1onh = val(left(Relay1on,2))
r1onm = val(right(Relay1on,2))
r1offh = val(left(Relay1off,2))
r1offm = val(right(Relay1off,2))

r2onh = val(left(Relay2on,2))
r2onm = val(right(Relay2on,2))
r2offh = val(left(Relay2off,2))
r2offm = val(right(Relay2off,2))

r1onminutes = r1onh * 60 + r1onm
if r1onminutes > 1439 then
 r1onminutes = 1439
endif
r1offminutes = r1offh * 60 + r1offm
if r1offminutes > 1439 then
 r1offminutes = 1439
endif

r2onminutes = r2onh * 60 + r2onm
if r2onminutes > 1439 then
 r2onminutes = 1439
endif
r2offminutes = r2offh * 60 + r2offm
if r2offminutes > 1439 then
 r2offminutes = 1439
endif
return


[CheckTime]
ct = time("hour:min")
cth = val(left(ct,2))
ctm = val(right(ct,2))
currminutes = cth * 60 + ctm

'Set Relay 1 state
if r1onminutes <= currminutes then
 if currminutes < r1offminutes then
  relayone = "on"
  io(po,relay1,1)
 else
  relayone = "off"
  io(po,relay1,0)
 endif
else
 relayone = "off"
 io(po,relay1,0)
endif

'Set Relay 2 state
if r2onminutes <= currminutes then
 if currminutes < r2offminutes then
  relaytwo = "on"
  io(po,relay2,1)
 else
  relaytwo = "off"
  io(po,relay2,0)
 endif
else
 relaytwo = "off"
 io(po,relay2,0)
endif
uvsr = "Running..."
returngui
return

[UpdateRelays]
gosub [CheckTime]
wait

Re: Aquarium light timer

PostPosted: Thu Jun 16, 2016 8:00 pm
by russm
Added a small tweak to highlight on the GUI if an update is made to times or timezone/DST and highlight an "ON" status for the relay.

Code: Select all'******************************************************************************
'*  Aquarium Light Timer for controlling two lights/devices                   *
'*  By Russ McIntire 6/13/16                                                  *
'*  Electrodragon Wifi IoT Relay Board                                        *
'*  http://www.electrodragon.com/product/wifi-iot-relay-board-based-esp8266/  *
'*  GPIO 12 and 13 are relay controlled                                       *
'*  GPIO14 is for DHT                                                         *
'*                                                                            *
'*  Not optimized, may not work crossing 24 HR boundary                       *
'******************************************************************************

memclear

DST = read.val(DaylightSavings)
timezone = read.val(TZ)

if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif

timesetup(tz,DST)
delay 5000

ct = time("hour:min")
uvsr = "Running..."

let relay1 = 12  'pin for relay1
let relay2 = 13  'pin for relay2


Relay1on = read(relay1ontime)
Relay1off = read(relay1offtime)
Relay2on = read(relay2ontime)
Relay2off = read(relay2offtime)


cls
wprint |<HTML>|
wprint |<HEAD>|
'wprint |<meta http-equiv='refresh'content='5;URL=/input?'>|
wprint |<h2>Aquarium Light Timer</h2>|
wprint |</HEAD>|
wprint |<body bgcolor="grey">|
wprint "<br>"

wprint "The time is currently: "
wprint htmlvar(ct)  ' time("hour:min")
wprint "<br> <br>"

wprint "Timezone: "
textbox timezone
wprint "<br>"
wprint "DST:         "
textbox DST
wprint "<br>"
button "Save Timezone", [timezone_update]
wprint "<br>"

wprint "<br>"
wprint "Enter on/off times below and save when done: <br>"
wprint "Valid times are: 00:00 to 23:59"
wprint "<br>"
wprint "Relay 1 - 24 hour (HH:MM) ON "
textbox Relay1on
wprint "OFF "
textbox Relay1off
wprint "<br>"
wprint "Relay 2 - 24 hour (HH:MM) ON "
textbox Relay2on
wprint "OFF "
textbox Relay2off
wprint "<br>"
button "Save Set Times", [Calculate]
wprint "<br> <br>"

gosub [Calculate_Mins]
gosub [CheckTime]

wprint "Relay 1 is " & htmlvar(relayone) & "<br>"
wprint "Relay 2 is " & htmlvar(relaytwo) & "<br> <br>"

'wprint "<mark>"
wprint htmlvar(uvsr)
'wprint "</mark>"

timer 60000, [UpdateRelays]

wait
end


[timezone_update]
'Persist timezone infomration
write(DaylightSavings,str(DST))
write(TZ,str(timezone))

if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif

timesetup(tz,DST)
delay 5000

serialprintln "Timezone Updated"
serialprintln "The Updated time is: "
serialprintln time("hour:min")

uvsr = "<mark> Updated... </mark>"
returngui
wait

[Calculate]
'Persist relay on and off times
write(relay1ontime,Relay1on)
write(relay1offtime,Relay1off)
write(relay2ontime,Relay2on)
write(relay2offtime,Relay2off)

serialprintln "On/Off times adjusted"
serialprintln "Relay 1 On: " & Relay1on & " Off: " & Relay1off
serialprintln "Relay 2 On: " & Relay2on & " Off: " & Relay2off

uvsr = "<mark> Updated... </mark>"
gosub [Calculate_Mins]
returngui
wait

[Calculate_Mins]
r1onh = val(left(Relay1on,2))
r1onm = val(right(Relay1on,2))
r1offh = val(left(Relay1off,2))
r1offm = val(right(Relay1off,2))

r2onh = val(left(Relay2on,2))
r2onm = val(right(Relay2on,2))
r2offh = val(left(Relay2off,2))
r2offm = val(right(Relay2off,2))

r1onminutes = r1onh * 60 + r1onm
if r1onminutes > 1439 then
 r1onminutes = 1439
endif
r1offminutes = r1offh * 60 + r1offm
if r1offminutes > 1439 then
 r1offminutes = 1439
endif

r2onminutes = r2onh * 60 + r2onm
if r2onminutes > 1439 then
 r2onminutes = 1439
endif
r2offminutes = r2offh * 60 + r2offm
if r2offminutes > 1439 then
 r2offminutes = 1439
endif
return


[CheckTime]
ct = time("hour:min")
cth = val(left(ct,2))
ctm = val(right(ct,2))
currminutes = cth * 60 + ctm

'Set Relay 1 state
if r1onminutes <= currminutes then
 if currminutes < r1offminutes then
  relayone = "<mark> ON </mark>"
  io(po,relay1,1)
 else
  relayone = "off"
  io(po,relay1,0)
 endif
else
 relayone = "off"
 io(po,relay1,0)
endif

'Set Relay 2 state
if r2onminutes <= currminutes then
 if currminutes < r2offminutes then
  relaytwo = "<mark> ON </mark>"
  io(po,relay2,1)
 else
  relaytwo = "off"
  io(po,relay2,0)
 endif
else
 relaytwo = "off"
 io(po,relay2,0)
endif
uvsr = "Running..."
returngui
return

[UpdateRelays]
gosub [CheckTime]
wait

Re: Aquarium light timer

PostPosted: Thu Jun 16, 2016 9:38 pm
by Mmiscool
This is great. Do you have any pictures of your aquarium or a video you might share? :-)