General area when it fits no where else

Moderator: Mmiscool

User avatar
By heckler
#58975 Thanks for your help guys (gals),

So to clarify... in my example code (which is a small section of the complete program) ALL the variables should be numeric not string.
think you should decide if RelayON is a string or a number and then let BASIC know before there are any comparisons done.

What do I need to do to let BASIC know that they are numeric?? Is it just a matter of pre-loading them with a number vlaue??

Before getting to this part of the code my program will have allready loaded hh with the current hours and mm with the current minutes. Also T1Hon should have the value of the desired hours to turn on and T1Mon should be loaded with the desired minutes to turn on.

T1Hon and T1Mon would have come from the settings web dialog. Is the problem that in entering the data from the web dialog that BASIC treats them as strings and not numbers??? If so then possibly all I need to do is to do a T1Hon = val(T1Hon) and likewise for T1Mon, in order to convert them to numbers??

Is the IF/THEN capable of handling this many nested statements?
If that is too many then is it possible to combine two of them?? something like this...
Code: Select allif RelayON = -1 then
   if T2Hon = hh AND T2Mon = mm then
         RelayON = T2D
         goto [RlyOn]
      end if


I appreciate any further guidance :)
dwight

I know Mikes time is limited and what he has done is greatly appreciated. If I had one wish it would be that the language reference was more complete and detailed with more example code for each command.
User avatar
By heckler
#58976 Here is my code so far and it is almost functional (by testing on a nodemcu (so that I have the help of the Debugger and the serial feedback on the com port.))

The final working version will be loaded on a SONOFF module.

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(only updates on webpage load or refresh) : "   
'                                   'the following lines cause the current device time to be
wprint htmlvar(hh)                  '  displayed on the web page.
wprint ":"                          '    NOTE: the device time will only
if mm < 10 then wprint "0"          '      update on initial page load or refresh
wprint htmlvar(mm)
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 minutes and hours
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 then        ' if relay off then check if time to turn on for time 1
   if T1Hon = hh then
      if T1Mon = mm then
         RelayON = T1D
         goto [RlyOn]
   end if
end if

if RelayON = -1 then        ' if relay off then check if time to turn on for time 2
   if T2Hon = hh then
      if T2Mon = mm then
         RelayON = T2D
         goto [RlyOn]
   end if
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


Note: in the code above I have removed one of the "end if" statements [near lines 76 and 85] from the two nested IF/THEN groups and now it tells me that
Error at line 69: Else / End If not found
so it dosent work either way (with or with out the extra "end if")

regards
dwight
User avatar
By heckler
#58977 Went back and studied the Language Reference again and found this...

Boolean Operators:

And boolean ‘and’ (ex. If a=10 and b=20 then ..)

so I am going to give this a try.

<EDIT>

Success!!!
this is now working!
Code: Select allif RelayON = -1 and T2Hon = hh and T2Mon = mm then
      RelayON = T2D
      goto [RlyOn]
end if


So I am guessing the current version did not like my mulit-nested IF/THEN statements.

check this thread for the (now) working code for an SONOFF module time switch.
http://www.esp8266.com/viewtopic.php?f=41&t=12721
thanks everyone for your helpful comments.
dwight

dwight