Post about your Basic project here

Moderator: Mmiscool

User avatar
By Ecoli-557
#52948 I have taken the work done by Electroguard, and Mike and now have a working version of Electroguard's Sonoff module working on Build 3.0.

These are inexpensive modules that can control 95-208 VAC device at a rated 10 amps - I would not try it at 10 amps, tracks are small and the solder would melt <grin>.
I would also STRONGLY suggest that you do not re-program these while connected to the grid - power them from the USB to serial (appropriate 3.3v of course) interface.
The newer modules have 1m memory which is fine for Basic 3.0

You will need to solder a single-row 0.1 inch male pins to the un-populated header next to the pushbutton switch, the square pad is 1.
Pins are: 1-Vcc, 2-TX, 3-RX, 4-Gnd, 5-SHT module.

You will need to flash the Sonoff module and to get into flashing mode you need to push the button on the Sonoff, then plug in the USB to serial converter. Then you may flash using your own local comport, memory is 1M, and flash away.

Browse to the module and setup the IP values for your own use and hit the save button on the settings screen and reset the module by power-cycling again.
Browse to the device with the new IP address and copy the code below:

Code: Select all'Sonoff code to get the device working under Build 3.0 of ESPbasic.
'Thanks to Electroguard and Mike - this would have taken much longer.
'A good starting point for some Home Automation?
deviceName = read("DName")                                 'Sonoff device has a 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

[top]
cls
print deviceName & "  "
textbox Status
interrupt d3, [Pressed]                                    'Hardware button pressed?  Branch
html "<BR>
Button "Toggle", [Toggle]                                 'Web buttons pressed?  Branch
Button "ON", [on]                                       'Web buttons pressed?  Branch
Button "OFF", [off]                                       'Web buttons pressed?  Branch
html "<BR>"
button "Settings", [settings]                              'Web buttons pressed?  Branch
wait


[on]                                                'Get here by wanting to turn ON
'io(po,d6,1)                                          'Direct way to set an output
if RelayLogic == 0 then io(po,RelayPin,1) else io(po,RelayPin,0)   'Another way
if LedLogic == 0 then io(po,LedPin,1) else io(po,LedPin,0)         'Same as above
goto [set.Stat.indicator]                                 'Go update the status on the web screen

[off]                                                'Get here by wanting to turn OFF
'io(po,d6,0)                                          'Direct way to reset an output
if RelayLogic == 0 then io(po,RelayPin,0) else io(po,RelayPin,1)   'Another way
if LedLogic == 0 then io(po,LedPin,0) else io(po,LedPin,1)         'Same as above
goto [set.Stat.indicator]                                 'Go update the status on the web screen

[Toggle]                                             'Get here by wanting to toggle the output
'if io(laststat,d6) = 0 then                              'One way to do this
'io(po,d6,1)
'else
'io(po,d6,0)
if io(laststat,RelayPin) = 0 then io(po,RelayPin,1) else io(po,RelayPin,0)   'Another way
if io(laststat,LedPin) = 0 then io(po,LedPin,1) else io(po,LedPin,0)      'Same as above
goto [set.Stat.indicator]                                 'Go update the status on the web screen
'end if

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

[set.Stat.indicator]                                    '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 on the box
wait


[settings]                                             'Here because we want to change the settings
cls
html "Device name"
textbox deviceName
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

Open the tab called EDIT and create a file called 'Sonoff.bas' by putting the words 'Sonoff' in the text bar and hit the OPEN button which will create a blank file 'Sonos'.
Paste the copied code from this example into the EDIT page, press SAVE, then press RUN.
You should have information displayed on the INPUT? page.
The buttons are pretty much self-explainitory except the 'settings' button 'names' the module to something you might remember such as Living Room Ceiling Fan instead of 10.0.1.105 which no one could keep up with after 30 or so devices.

Pressing the button on the Sonoff unit will toggle the relay status as well.

Now to see how to get this on a DIY Home Automation system which is less expensive than my Control4 system!

Regards to All.