Post about your Basic project here

Moderator: Mmiscool

User avatar
By heckler
#58869 Hey group... Been a while since I have been active here.
Thought I'd share my newest project.

But first a big THANKS to Mike for ESPbasic and others who shared the basis for the code where I started from !!!!

It is code that will run on a SONOFF module to allow the user to define 2 different time events.
Each event can have it's own time duration.
So the user would click on the settings button and then enter the hours and minutes for each event to start (module ON) then the duration in minutes for the sonoff relay to be turned ON.

Currently the code is incomplete in that it is not actually turning on the relay at the specified time, (but you can toggle the relay using the buttons)
I am still developing the code to finish it up and have the relay react to the 2 defined time ON values.

I will post more when I have it.
I am using a nodeMCU module to do the development so that I have the DEBUG function available.

So if you want to give it a spin you can either load it into a sonoff or a nodemcu module.

thoughts... questions...
dwight


<EDIT>
I removed the NON-WORKING code posted here to prevent confusion.
The WORKING code is posted below
Last edited by heckler on Fri Dec 02, 2016 6:57 pm, edited 2 times in total.
User avatar
By Electroguard
#58888 hi hecklar,
just thought I'd better pipe up to point out that mmiscool has dropped var name lengths back to 10 now - which presumably applies to branches names also - so you can expect to have trouble from things like...
Code: Select allLocalButton = d3                                          'The button on the Sonoff module
goto [set.Stat.indicator]                                 'Go update the status on the web screen
User avatar
By heckler
#58980 Hey Group!!

So I have this code WORKING !!
Code: Select alltimesetup(-7,0)                                           'MST -7 and DST off 0
hh = 12
mm = 00
T1Hon = 6  'first time Hrs ON
T1Mon = 0  'first time Min ON
T1D = 0    'first time ON duration
T2Hon = 18 'second time Hrs ON
T2Mon = 0  'second time Min ON
T2D = 0    'second time ON duration
Status = "OFF"
RelayON = 0
deviceName = read("DName")                         'Sonoff device name such as "living room fan"
if deviceName == "" then deviceName = "Sonof"             'Filler
RelayPin = d6                                             'IO pin for the relay
LedPin = d7                                               'GREEN side of the bi-color LED
LocalButton = d3                                          'The button on the Sonoff module
RelayLogic = 0                                            'Means relay is ACTIVE HIGH
LedLogic = 1                                              'Means LED is ACTIVE LOW
interrupt d3, [Pressed]                                   'Hardware button pressed?  Branch
timer 60000, [NextMin]                                    '60 second timer to update minutes/hours
delay 5000
bla = time()
print bla
shh = mid(bla,12,2)         'hour  this statment extracts the hours value
smm = mid(bla,15,2)         'min   this statment extracts the minutes
hh = val(shh)               ' this converts the hours from string to numeric
mm = val(smm)               ' this converts the minutes from string to numeric

[Top]
cls
print deviceName & "  "
textbox Status
html "<BR>"
wprint "Time 1 ON: " & T1Hon & ":" & T1Mon & " for " &  T1D & " min"
html "<BR>"
wprint "Time 2 ON: " & T2Hon & ":" & T2Mon & " for " & T2D & " min"
html "<BR>"
html "<BR>"
wprint "Current device time ==> "
wprint htmlvar(hh)
wprint ":"
wprint htmlvar(mm)
html "<BR>"
wprint "Note: above module time only updates on webpage load or refresh."
html "<BR>"
html "<BR>"
Button "Toggle", [RlyToggle]                              'Web buttons pressed?  Branch
Button "ON", [RlyOn]                                          'Web buttons pressed?  Branch
Button "OFF", [RlyOff]                                        'Web buttons pressed?  Branch
html "<BR>"
html "<BR>"
button "Settings", [Settings]                              'Web buttons pressed?  Branch
button "Exit Pgm", [End]
html "<BR>"
button "get time", [SyncTime]
wait

[NextMin]                   'this sub is called once a minute to increment hh:mm
let mm = mm + 1
if mm = 60 then
   mm = 0
   hh = hh + 1
   if hh = 24 then hh = 0
end if

if RelayON > 0 then RelayON = RelayON - 1
if RelayON = 0 then goto [RlyOff]

if RelayON = -1 and T1Hon = hh and T1Mon = mm then
      RelayON = T1D
      goto [RlyOn]
end if

if RelayON = -1 and T2Hon = hh and T2Mon = mm then
      RelayON = T2D
      goto [RlyOn]
end if

[DailySync]                'check for once a day network time sync
if hh = 21 then             
  if mm = 0 then goto [SyncTime]
end if
wait

[SyncTime]                 'arrive here to re-sync clock to NTP time source
bla = time()
print bla
shh = mid(bla,12,2)         'hour  this statment extracts the hours value
smm = mid(bla,15,2)         'min   this statment extracts the minutes
hh = val(shh)               ' this converts the hours from string to numeric
mm = val(smm)               ' this converts the minutes from string to numeric
wait

[RlyOn]                                                      'Get here by wanting to turn ON
if RelayLogic == 0 then io(po,RelayPin,1) else io(po,RelayPin,0)
if LedLogic == 0 then io(po,LedPin,1) else io(po,LedPin,0)
goto [WebStat]                                 'Go update the status on the web screen

[RlyOff]                                          'Get here by wanting to turn OFF
RelayON = -1
if RelayLogic == 0 then io(po,RelayPin,0) else io(po,RelayPin,1)
if LedLogic == 0 then io(po,LedPin,0) else io(po,LedPin,1)
goto [WebStat]                                 'Go update the status on the web screen

[RlyToggle]                                                  'Get here by wanting to toggle the output
if io(laststat,RelayPin) = 0 then io(po,RelayPin,1) else io(po,RelayPin,0)
if io(laststat,LedPin) = 0 then io(po,LedPin,1) else io(po,LedPin,0)
goto [WebStat]                                 'Go update the status on the web screen


[Pressed]                                                 'Got here cuz we pressed the hardware button
if io(laststat,LocalButton) = 0 then goto [RlyToggle]        'Wait until the button is released for debounce
goto [Top]                                                'Done, so start over

[WebStat]                            'Get here because we need to update the web status
if io(laststat,d6) = 0 then Status = "OFF" else Status = "ON"      'Get the correct text in the box
wait

[Settings]                                                'Here because we want to change the settings
cls
html "Device name"
textbox deviceName
html "<BR>"
html "<BR>"
html "Enter OnTime Hrs of day (0-23)"
html "<BR>"
html "Time 1 ON (Hrs): "
textbox T1Hon
html " (Min): "
textbox T1Mon
html "  Duration (min): "
textbox T1D
html "<BR>"
html "Time 2 ON (Hrs): "
textbox T2Hon
html " (Min): "
textbox T2Mon
html "  Duration (min): "
textbox T2D
html "<BR>"
button "Save", [save.set]
wait

[save.set]                                                'To ensure we put something in here
if deviceName == "" then
   print "device name must be specified"
   wait
else
write("DName",deviceName)                                 'Write to flash
end if
goto [Top]                                                'Done, so start over

[End]
end



So a couple of instructions...
> when entering time values in the settings page... if you want the switch to turn on at 06:01 you would just enter 6 in the hours box and 1 in the minutes box.

> you then enter the number of minutes you want the module to be ON for. So for 3 hours you would enter 180 in the duration box.

> then when you save your settings and get back to the main page you will see you chosen time ON values displayed.

> the code (currently) has the ability for 2 separate time ON settings. So if you only want to have 1 time ON sequence then for the second series in the settings page just enter "0" in the second duration box.

Comming enhancements...

>> Make the code check to see that a valid time value was retrieved from the www NTP server during the TIME() function. (by looking for the year 1970 in the time() string (indicates that the module did not get a valid time from NTP)

>> Allow the user to manually set the time on the settings page. (not sure how accurate the module's internal clock is though?) This way the module could work without a connection to the www.

>> Currently the code only updates the clock to NTP once a day. This is determined in the [DailySync] section of the code and you can set it to whatever you want.

>> I chose to only sync the clock once a day (what do you all think?)

Enjoy and again Thanks to Mike, Electroguard and Ecoli-557 for sharing.
the code originally came from here...
http://www.esp8266.com/viewtopic.php?f=41&t=11302&p=52948&hilit=sonoff#p52948

I'd love to hear your thoughts and feedback.
Questions also!!
dwight
Last edited by heckler on Sun Dec 04, 2016 10:29 am, edited 4 times in total.
User avatar
By ardhuru
#58984
heckler wrote:
>> I chose to only sync the clock once a day (what do you all think?)

dwight


Once every few hours? And if that fails, continue with the existing values being updated internally?