-->
Page 1 of 1

WakeUp!

PostPosted: Thu Mar 02, 2017 7:13 am
by PhilTilson
This project might provide amusement for some, as well as demonstrating one or two handy techniques in the use of ESPbasic.

I am pretty awful at getting up on dark mornings and wondered if a blue-white ceiling light might assist in getting me awake. The first job was to take one of the readily available 20cm square LED light panels and replace the internal warm white LEDs with a strip of RGB LEDs. I guess I could have used the 'programmable' Neo-type devices, but I already had quite a lot of spare strip, the type controlled by a small IR remote.

This arrangement gave me a nice blue-white light when I selected the White button and I also used a low level of an orange colour at night in an attempt to help me get ready for sleep (don't laugh - it does actually work!)

The problem was that I had to wake at 6.45am to switch the panel on at its minimum setting (which wasn't as low as I wanted) and then increment the brightness every minute or two until it was at full brightness - by which time I was awake anyway through playing with the damned remote! An ESP was obviously called for.

I first attempted to recreate the IR signals from the remote using the ESP. Despite being able to produce virtually identical scope traces of the patterns from the remote and the ESP driving an IR LED, the controller refused point blank to respond. So I decided instead to simply drive three MOSFETs directly from the outputs of the ESP and use these to control the 12V lines to the light. This works well.

This is the program I use for the task:

Code: Select allmemclear
serialprintln ramfree()

rr = 14  'red pin
gg = 12  'green pin
bb = 16  'blue pin
daylevel = 1024
nightlevel = 16
N_time_on$ = "22:30"
N_time_off$ = "00:30"
D_time_on$ = "06:45"
D_time_off$ = "07:15"
day_inc_active = 0
night_active = 0

onload [setup]

[setup]
cls

cssclass "button", "position: absolute; top: 40px; width: 100px;"
cssclass "select", "position: absolute; width: 65px;"

textbox dtg$
cssid htmlid(), "left: 0px; top: 0px;"
gosub [gettime]

button "All Off", [alloff]
cssid htmlid(), "background-color: red; color: white; left: 10px;"
button "Morning", [dayon]
cssid htmlid(), "background-color: aqua; left: 130px;"
button "Night", [nighton]
cssid htmlid(), "background-color: darkorange; left: 250px;"
button "Exit", [TestExit]
cssid htmlid(), "background-color: green; color: white; left: 370px;"

a1$ = "Select Light Levels:"
textbox a1$
cssid htmlid(), "position: absolute; color: black; left: 40px; top: 80px; width: 180px; border: none;"

dropdown daylevel, "100, 200, 400, 800, 1024"
cssid htmlid(), "left: 220px; top: 80px;"
dropdown nightlevel, " 2, 4, 8, 16, 32, 64, 128"
cssid htmlid(), "left: 300px; top: 80px;"
   
a2$ = "Select Night Times:"
textbox a2$
cssid htmlid(), "position: absolute; color: black; left: 40px; top: 140px; width: 180px; border: none;"

dropdown N_time_on$, "22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30"
cssid htmlid(), "left: 220px; top: 140px;"
dropdown N_time_off$, "23:00, 23:15, 23:30, 23:45, 00:00, 00:15, 00:30"
cssid htmlid(), "left: 300px; top: 140px;"
   
a3$ = "Select Morning Start Time:"
textbox a3$
cssid htmlid(), "position: absolute; color: black; left: 40px; top: 180px; width: 180px; border: none;"

dropdown D_time_on$, "05:15, 05:45, 06:15, 06:45, 07:15, 07:45"
cssid htmlid(), "left: 300px; top: 180px;"
   
serialprintln dtg$ & "  System started..."
io(pwo,rr,0)
io(pwo,gg,0)
io(pwo,bb,0)
timer 1000, [doit]
wait

[doit]
gosub [gettime]
curtime$ = mid(dtg$,14,5)
if curtime$ = N_time_on$ and night_active = 0 then gosub [nighton]
if curtime$ = N_time_off$ and night_active = 1 then gosub [alloff]

if curtime$ = D_time_on$ and day_inc_active = 0 then
   inclevel = 1
   gosub [dayinc]
end if

if curtime$ = D_time_off$ and day_inc_active = 1 then gosub [alloff]
if day_inc_active = 1 then gosub [dayinc]
   
wait

[gettime]
dtg$ = time("day month year  hour:min:sec")
return

[alloff]
day_inc_active = 0
inclevel = 0
night_active = 0
io(pwo,rr,0)
io(pwo,gg,0)
io(pwo,bb,0)
serialprintln dtg$ & "  All Off"
wait

[dayon]
io(pwo,rr,daylevel)
io(pwo,gg,daylevel)
io(pwo,bb,daylevel)
serialprintln dtg$ & "  Day_on: " & daylevel
wait

[nighton]
night_active = 1
io(pwo,rr,nightlevel*2)
io(pwo,gg,nightlevel/2)
io(pwo,bb,0)
serialprintln dtg$ & "  Night_on: " & nightlevel
wait

[dayinc]
serialprintln dtg$ & "  Day_inc: " & daylevel
day_inc_active = 1
if D_time_on$ = "05:15" then D_time_off$ = "05:45"
if D_time_on$ = "05:45" then D_time_off$ = "06:15"
if D_time_on$ = "06:15" then D_time_off$ = "06:45"
if D_time_on$ = "06:45" then D_time_off$ = "07:15"
if D_time_on$ = "07:15" then D_time_off$ = "07:45"
if D_time_on$ = "07:45" then D_time_off$ = "08:15"

io(pwo,rr,inclevel)
io(pwo,gg,inclevel)
io(pwo,bb,inclevel)
inclevel = inclevel * 1.008
if inclevel > daylevel then inclevel = daylevel
return

[TestExit]
end


The browser page produced by this is here:

WakeUp.jpg

The date/time updates every second to let me know that everything is running.

The select boxes let me choose the desired levels for maximum 'morning' light and for the low-level night-time light. Clicking the Morning or Night buttons turns on the panel using the current settings so that I can check that they are what I want.

The Times drop-downs hold a limited number of options, as I won't want the night-time light on at 10am, for example, but I may need to get up very early for a flight some time.

Obviously, the All Off button switches the light off and the Exit button stops the program.

It would have been nice to be able to set any time, but because there is no 'time' or 'date' variable type, the hassle of trying to format the various options didn't seem worth the effort.

One interesting section is the subroutine entitled [dayinc]. This increments the white light from nothing to full brightness over a period of 15 minutes. Initially, I just used a standard loop, but I found that this caused the brightness to increase within a few seconds from nothing to fairly bright, which was not what I wanted. Using a factor of 1.008 to multiply up the brightness level leads to a value of 1024 (maximum) in just about 15 minutes and the exponential curve that results gives a very good, slow ramp-up from dark to full brightness.

A few diagnostic messages are sent to the serial port for trouble-shooting and testing purposes.

I don't claim this is the neatest or most compact code, but at least this application is a bit different!

Phil

Re: WakeUp!

PostPosted: Thu Mar 02, 2017 9:34 am
by RichardS
Very nice, you can do a lot with ESPBASIC in very short order!

RichardS