Post about your Basic project here

Moderator: Mmiscool

User avatar
By santo
#42215 I programmed these two projects in Basic for nostalgic reasons. In the end, I had to reprogram the Router Reset Button in Arduino code in order to get it execute the code immediately after pressing the button.

There are two projects here. They both use two external services, Thingspeak and IFTTT.

Button.JPG


The first is a typical Internet enabled button that consists of a rechargeable battery, an ESP8266 module, a tri-colour LED and a switch. When the button switch is held down the battery connects to the module and the code executes. The Red LED lights. When the module connects to the local AP the Blue LED lights. A "reset" message is sent to the Thingspeak router channel requesting a reset. IFTTT is then used to notify that the request was sent. The Green LED lights when all is accomplished and the user releases the button.

Code: Select allREM Button to set the router to reset.
REM By Santo Visconti 2016   santovisconti.com
let redpin = 14
let bluepin = 12
let greenpin = 13
let redstatus = 1 ' Set LED to Red to indicate module startup
let bluestatus = 0
let greenstatus = 0
gosub [lightleds]

let relaychannel = "XXXXX" ' Replace with your own Thingspeak relay channel
let relaygetkey = "XXXXXXXXXXXXXXXX" ' Thingspeak read API key
let relaysetkey = "XXXXXXXXXXXXXXXX" ' Thingspeak write API key
let iftttkey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ' IFTTT maker key to notify that reset message was sent
let routerfailure = 0
let stopnow = 0
let setrouter = "reset" ' Router looks for reset message

[start]
let ipadd = ""
for x = 1 to 3          ' Attempt to connect 3 times with a 10 sec gap between attempts
connect         ' Connection information set in the Setup page
let ipadd = ip()
serialprintln ipadd
serialprintln x
if ipadd <> "0.0.0.0" then goto [continue]
delay 10000
next x
end

[continue]
redstatus = 0
bluestatus = 1 ' Set LED to Blue to indicate connection achieved
greenstatus = 0
gosub [lightleds]
let tssetrelay = SENDTS(relaysetkey,1,setrouter) ' Send reset message to Thingspeak channel
serialprintln tssetrelay
delay 3000
gosub [setrouter] ' Send message to IFTTT to fire notification
delay 1000
redstatus = 0
bluestatus = 0
greenstatus = 1  '  Turn LED to Green to indicate everything was done and button can be released (Module power off)
gosub [lightleds]
end

[lightleds]
if redstatus = 0 then po redpin 0 else pwo redpin 250
if bluestatus = 0 then po bluepin 0 else pwo bluepin 250
if greenstatus = 0 then po greenpin 0 else pwo greenpin 250
return

[setrouter]
let trigger = "maker.ifttt.com/trigger/set_router/with/key/" & iftttkey
let trigger = trigger & "/?value1="
let trigger = trigger & setrouter
serialprintln trigger
serialprintln wget(trigger)
return




routerfinished.jpg


The second project poles the Thingspeak router channel to see if a reset has been requested and resets the router accordingly. It also sends a timestamp every three minutes to the Thingspeak timestamp channel. The last timestamp is compared to the current time and if there is a gap greater than 5 minutes, then IFTTT fires an event notifying that there has been a service interruption.

nodemcu.jpg

relay.jpg


The code is simple and there is nothing new or special here. I might however, help someone out there.

Code: Select all
REM Program to monitor internet/power service and reset the router if requested.
REM By Santo Visconti 2016 santo.visconti.com

let relaypin = 13
po relaypin 0
let redpin = 14
let bluepin = 12
let greenpin = 13    ' We don't use the green LED. The relay is powered from this pin.
let redstatus = 1    ' Turn on Red LED to indicate module is powered up but not connected.
let bluestatus = 0
let greenstatus = 0
gosub [lightleds]
let relaychannel = "XXXXX"            ' Thingspeak Router reset channel number
let relaygetkey = "XXXXXXXXXXXXXXXX"  ' Thingspeak read API key
let relaysetkey = "XXXXXXXXXXXXXXXX"  ' Thingspeak write API key
let timechannel = "XXXXX"             ' Thingspeak timestamp channel number
let timegetkey = "XXXXXXXXXXXXXXXX"   ' Thingspeak read API key
let timesetkey = "XXXXXXXXXXXXXXXX"   ' Thingspeak write API key
let iftttkey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"  ' IFTTT maker channel key
timesetup(-5,1) ' Set up time zone

[start]
let routerfailure = 0
let stopnow = 0

[connectloop]
let ipadd = ""
for x = 1 to 3       ' Attempt to connect 3 times with one minute wait in between to give router a chance to power up after power failure
connect              ' Connection criteria set the Set up webpage
let ipadd = ip()
serialprintln ipadd
serialprintln x
if ipadd <> "0.0.0.0" then goto [continue]
delay 60000
next x

routerfailure = routerfailure + 1 ' Attempt the connect above 5 times with router reset and 2 minute wait in between.
serialprintln "failed " & routerfailure
if routerfailure > 5 then goto [stop] ' After 5 attempts stop trying.
redstatus = 1           ' Set Red LED on to show no connection
bluestatus = 0
gosub [lightleds]
po relaypin 1      ' Turn power off to the router by setting the relay pin to high (Router socket tied to NC (normally closed) connection on the relay
delay 10000        ' Keep router off for 10 seconds
po relaypin 0      ' Power up the router again
delay 120000       ' Wait 2 minutes before attempting to connect again.   
goto [connectloop]

[continue]
redstatus = 0
bluestatus = 1      ' Set Blue LED on to indicate connection achieved.
gosub [lightleds]
if routerfailure > 0 then gosub [reportfailure]    ' Report that router had to be reset to achieve connection

timer 180000 [mainloop]    ' Main timer. Pole every 3 minutes for changes in status
wait

[reportfailure]
let routerfailure = 0
let tsreadtime = READTS(timegetkey,timechannel,1) ' Get the last recorded time
gosub [gettime]                                   ' Get the current time to report failure interval
let trigger = "maker.ifttt.com/trigger/router_failure/with/key/" & iftttkey
let trigger = trigger & "/?value1="
let trigger = trigger & tsreadtime
let trigger = trigger & "_and_approximately_"
let trigger = trigger & timenow
serialprintln trigger
serialprintln wget(trigger)        ' Send event trigger to IFTTT (Triggers an SMS in my case)
return

[mainloop]
let failflag = 0   ' To monitor loss in connection to internet
let tsgetrelay = READTS(relaygetkey,relaychannel,1)  ' Get request to reset the router if set to reset
serialprintln tsgetrelay
if tsgetrelay == "reset" then goto [resetrouter]     ' If set to reset, then go do the router reset
let tsreadtime = READTS(timegetkey,timechannel,1)    ' Read the last recorded time in the Thingspeak timestamp channel
serialprintln tsreadtime             
gosub [gettime]                    ' Read the current time
gosub [checkdates]                                   ' Check to see if times are within 5 min of each other.
let tssettime = SENDTS(timesetkey,1,timenow)         ' Send the current timestamp to the timestamp channel
serialprintln tssettime
stopnow = stopnow + failflag
if stopnow > 3 then goto [start]                     ' If we have had 3 failed attempts to access the internet
wait

[gettime]                         ' Get the current time and replace spaces with _ for web requests   
let timenow = time()
let timenow = replace(timenow," ","_")
let timenow = left(timenow,24)
serialprintln timenow
return

[checkdates]                     ' Check to see if times are within 5 min of each other   
let rollover = 0                                     ' Checks to see if we have rolled over to new day or hour
let reportdiff = 0                                   ' Checks to see if there is a big time difference and reports it
let oldyear = right(tsreadtime,4)                    ' Break up the dates into Year, date, hour, and min
let olddate = left(tsreadtime,10)
let oldhour = mid(tsreadtime,12,2)
let oldmin = mid(tsreadtime,15,2)
let oldmin = int(oldmin)                             ' Convert the minutes to integer
serialprintln oldyear
serialprintln olddate
serialprintln oldhour
serialprintln oldmin
let newyear = right(timenow,4)
let newdate = left(timenow,10)
let newhour = mid(timenow,12,2)
let newmin = mid(timenow,15,2)
let newmin = int(newmin)
serialprintln newyear
serialprintln newdate
serialprintln newhour
serialprintln newmin
if newyear == "1970" then let failflag = 1         ' Time returns a date of Jan 1, 1970 when it can't connect to internet 
if newyear == "1970" then return                      ' No point in going on if we are not connected
if oldhour == "23" then gosub [rolloveroldmin]        ' If it is 11 p.m. then check to see if we are going to rollover to the next day
debug = "rollover " & rollover
serialprintln debug
if rollover = 1 then return                           ' If we have rolled over to the next day then don't bother checking further
if oldyear <> newyear then gosub [reportdiff]         ' If the years don't match then we have a distruption in service that needs to be reported
serialprintln "reportdiff1 "
if reportdiff = 1 then return                         ' Service interruption reported. No need to carry on.
if olddate <> newdate then gosub [reportdiff]         ' If the dates don't match then we have had a disruption in service
serialprintln "reportdiff2 "
if reportdiff = 1 then return
if oldmin > 54 then gosub [rollovernewmin]            ' If we are close to the end of the hour then we might be rolling over to next hour
if rollover = 1 then return                           
if oldhour <> newhour then gosub [reportdiff]         ' If the hours don't match then we have had a service interruption.
serialprintln "reportdiff3 "
if reportdiff = 1 then return
let diff = newmin - oldmin                            ' Check the difference in the minutes of the old time with that of the new time.
debug = "mindiff " & diff
serialprintln debug
if diff > 5 then gosub [reportdiff]                   ' Times should be updated every 3 minutes. So if the difference is more than 5 minutes
return                                                ' we have had a service interruption.

[rolloveroldmin]
if oldmin > 54 then gosub [rollovernewhour]
return

[rollovernewhour]
if newhour == "00" then gosub [rollovernewmin]
return

[rollovernewmin]
if newmin < 5 then let rollover = 1
return

[reportdiff]                                         ' Triggers IFTTT notification of a service interruption
let reportdiff = 1
let trigger = "maker.ifttt.com/trigger/service_interrupt/with/key/" & iftttkey
let trigger = trigger & "/?value1="
let trigger = trigger & tsreadtime
let trigger = trigger & "_and_approximately_"
let trigger = trigger & timenow
serialprintln trigger
serialprintln wget(trigger)
return

[lightleds]                                                        ' Light up the LEDs according to their status
if redstatus = 0 then po redpin 0 else pwo redpin 250
if bluestatus = 0 then po bluepin 0 else pwo bluepin 250
rem if greenstatus = 0 then po greenpin 0 else pwo greenpin 250    ' We don't use the green LED
return

[resetrouter]                       ' Reset the router
timer 0                             ' Turn the timer off. Router will be reset.
let setrouter = "normal"            ' Send normal to the Thingspeak router channel to cancel the request to reset
redstatus = 1                       ' Turn the Red LED on to indicate loss of connection to router
bluestatus = 0
gosub [lightleds]
gosub [gettime]                     ' Get the current time
let tssetrelay = SENDTS(relaysetkey,1,setrouter)     ' Send normal to Thingspeak channel to cancel reset request
serialprintln tssetrelay
delay 5000                          ' wait 5 sec to register the reqest
let trigger = "maker.ifttt.com/trigger/router_reset/with/key/" & iftttkey
let trigger = trigger & "/?value1="
let trigger = trigger & timenow
serialprintln trigger
serialprintln wget(trigger)         ' Trigger IFTTT notification that router is being reset
delay 10000                         ' Wait 10 seconds for the trigger to happen
po relaypin 1             ' Activate relay to cut the power to the router   
delay 10000                         ' Keep router off for 10 seconds
po relaypin 0                       ' Start the router up again
delay 60000                         ' Wait a minute to get it to startup
goto [start]                        ' Re start the program
 
[stop]               
timer 0
redstatus = 0
bluestatus = 0
gosub [lightleds]           ' Turn off the LEDS to indicate program is not active.
end

You do not have the required permissions to view the files attached to this post.
User avatar
By Mmiscool
#42280 This is awesome. Thanks for sharing.

The pictures are especially helpful in understanding a project like this.