Introduction: ESP8266 BASIC IoT Light
This Instructables show how to use ESP8266 BASIC remote control a household light.
Step 1: Why Remote Switch?
My home hall light switch hide behind the Cabinet and far from the door. It is hard to turn on the light when I came home or turn off the light when I leave. So I want to control the light remotely.
Before do that, here are some concern about the remote switch:
- other people may gain the access to control the remote switch too
- remote receiver normally consume power below 1 W, but it run in 7x24 basis, so it still use up some extra power
- it may have some risk being shocked while accessing AC power
After evaluated the concern, I still want a remote light switch, so I go to next step.
Step 2: Turn Off the Main Power Before Any AC Access
For safety reason, you should switch off and double check no AC current before access any AC wire.
Step 3: Check Wires in the Switch Socket
Same as many remote receiver, ESP8266 require a DC current to operate, so I need a AC/DC converter. However, in some case, the switch socket may only have the Live wire but did not have the Neutral wire. In this case, it cannot provide DC current in the socket.
After checked it have both Live and Neutral wire in the socket, we can keep going to the next step.
Step 4: Choosing Remote Receiver
There are many cheap remote light switch in the market, but I guess it is very easy to jam other home remote channels.
ESP8266 use WiFi connect to my own AP with encrypted signal should be good enough.
There are still many firmware support ESP8266: NodeMCU, Arduino, MicroPython, ESP8266 Basic...
ESP8266 Basic have a very good feature, once I flashed the firmware, all program work in web base. That means I can hide the board in the socket and no need to take out for reprogram. (except upgrade firmware :P)
ESP8266 firmware ref.:
Step 5: Preparation
- ESP8266, any version should be OK, this time I am using ESP-07
- A USB FTDI programmer for flashing ESP8266 Basic firmware
- A light switch socket with USB charge plug (for AC/DC conversion)
- A 10 k Ohm resistor
- A 2N2222 transistor
- A 3 V / 250 V 3 A relay
- Few wires for AC circult
- Few wires for DC circult
- A 3 pin Screw Terminal Block Connector
- A 5 V to 3.3 V DC/DC convertor
and it need some hot glue and clear tape for insulation
Step 6: Disassembly the Switch Socket
I need to connect the extra screw terminal block connector, squeeze my component in it, access the AC/DC converter and wire out the 5 V power source, so I need disassembly the switch socket first.
Step 7: Install 3 Pin Screw Terminal Block Connector
- Mark the Pin in the Socket
- Drill the holes
- use hot glue to fix the connector
Step 8: Program ESP8266 Basic
If you are using ESP-07 too, here is the connect reference:
ESP-07
- Tx -> FTDI Rx
- Rx -> FTDI Tx
- GND -> FTDI GND
- GPIO15 -> FTDI GND
- GPIO0 -> FTDI GND
- VCC -> 3.3 V
- EN (CH_PD) -> 3.3 V
After flashing ESP8266 BASIC, remember remove the GPIO0 connection and reset (simply re-plug the USB) for testing.
Ref:
Step 9: Connect and Config ESP8266 BASIC
After removing the GPIO0 connection and reset (simply re-plug the USB), you can search a WiFi AP called ESP by the mobile or notebook.
Connect to the AP ESP and open the URL http://192.168.4.1
Firstly, make it connect to the home WiFi AP:
- press [setting] link
- fill the AP name and password
- press save button
- before restart, open a serial console (Arduino have one) in the computer connected ESP through the USB FTDI programmer
- press restart button
- if everything goes right, you can see ESP connected to the home WiFi AP and show the current IP address in the serial console, remember the IP address
- try connect to the IP address with web browser
- it is recommended config the DHCP server associate fix IP address to the ESP device
Step 10: Simple Toggle Switch Code
I want the light switch can be controlled by both physical switch and web base interface, so I write a simple toggle logic to alter the GPIO output.
Here is my sample code, edit, paste and save to ESP8266 BASIC: (ESP will restart after save)
cls
let OutputPin = 5
button "Toggle" [Toggle]
wait
[Toggle]
if PinStatus == 1 then goto [On] else goto [Off]
[On]
PinStatus = 0
po OutputPin 1
wait
[Off]
PinStatus = 1
po OutputPin 0
wait
Step 11: Soldering Work
Here is the summary of the connection:
5 V to 3.3 V DC/DC convertor
- +/- IN -> USB charge panel
- +/- OUT -> ESP
ESP-07
- GPIO15 -> GND
- GND -> GND
- EN -> 3.3 V
- VCC -> 3.3 V
- GPIO5 -> 10 K Ohm resistor -> 2N2222 transistor base
2N2222 transistor
- base -> 10 K Ohm resistor -> ESP GPIO5
- collector -> relay coil terminal -> 3.3 V
- emitter -> GND
Relay
- Common terminal -> Terminal Block Connector
- Normally open terminal -> Terminal Block Connector
- Normally close terminal -> Terminal Block Connector
- Thanks to CaptClaude remind me it should put a diode parallel to the relay coil (ref.: http://music-electronics-forum.com/t4456/)
If you are using ESP-01, some points to be noted. Connect a NPN transistor to GPIO have a side effect that pull down the pin. ESP-01 only have GPIO0, GPIO2, Tx(GPIO1) and Rx(GPIO3), pull down GPIO0 or GPIO2 at power on will enter flash mode; connect 2N2222 to Tx also cannot start up normally. Hopefully it can still connect to GPIO3, everything can work fin.
Step 12: Insulation
Use hot glue to seal all AC connected parts and then use some clear tape to cover the DC/DC converter and ESP board.
Step 13: Re-assembly the Switch Socket
Squeeze all component to the socket and then re-assembly the switch socket.
Step 14: Replace the Old Socket
I want the light can control by physical switch and ESP connected relay, so it need to connect the switch and relay in serial.
Step 15: Test Connection
Ensure you mobile connected home WiFi:
- open the ESP IP address with the browser
- click [RUN] link
- press toggle button
- the light should turn on and off while you press toggle button
Step 16: Happy IoT!
This ESP8266 BASIC light actually only connected to the home LAN but not Internet, but it still can remote control the light outside the home if you have setup the home VPN.
If you really want your light become a real IoT, try Blynk, I have tested it works while research, just a little bit difficult than ESP8266 BASIC :P

Participated in the
Full Spectrum Laser Contest 2016
47 Comments
Question 8 months ago
How to use Blynk with ESPbasic?
Tip 8 months ago
Here is the ESPbasic V3 version - output @ pin 5
cls
let OutputPin = 5
button "Toggle", [Toggle]
wait
[Toggle]
if PinStatus == 1 then goto [On] else goto [Off]
[On]
PinStatus = 0
IO(po, OutputPin, 1)
wait
[Off]
PinStatus = 1
IO(po, OutputPin, 0)
wait
7 years ago
Hi
I have just purchased an ESP8266-12 and cannot link it via my USB to my pc
Can you please help , I can pick up the device on my phone via wifi and I can link to it but that is it
Reply 4 years ago
After run:
Error at line 9: Failed to reach end of input expression, likely malformed inputError at line 9: Syntax errorError at line 9: Halted
Reply 4 years ago
Good ... Now is work . Thanks
Reply 7 years ago
whats mean cannot link? can you show me how you connect the USB serial cable to the ESP-12E board?
7 years ago
Hi
I have problem
after save Toggle Switch Code, see this error
Error at line 3: The arguments must be 2!
Error at line 3: Halted
can help me?!
Reply 7 years ago
My original code line 3 is:
button "Toggle" [Toggle]
I have re-visited ESP8266 BASIC page, I guess the new version syntax should be:
button "Toggle", [Toggle]
Ref.: https://www.esp8266basic.com/gui-example.html
7 years ago
陳先生 你好:
我把你的程式加上一段設定WIFI AP的指令並進行連接,然後用"print ip()"就可以在行動裝置上看到所回傳分配到的動態IP值了,這時只要重新啟動瀏覽器並打上新的IP就可以透過WIFI Router連接上ESP8266了!
let ssid = ""
let password = ""
cls
let OutputPin = 5
button "Toggle" [Toggle]
print "SSID:"
textbox ssid
print "PASSWORD:"
textbox password
button "Connect" [connect_to_ap]
wait
[connect_to_ap]
connect ssid password
print ip()
wait
[Toggle]
if PinStatus == 1 then goto [On] else goto [Off]
[On]
PinStatus = 0
po OutputPin 1
wait
[Off]
PinStatus = 1
po OutputPin 0
wait
7 years ago
陳先生你好:
很高興能看到你的大作,不過我有個建議,如果能將設定及連結WIFI AP的動作也融入程式中,然後可將分配到的IP直接傳到使用者手機端的話,這樣一般的使用者就不用再透過筆電等裝置去取得不就更方便了嗎?
Reply 7 years ago
seems great, but how to do that? here are the steps I think:
1. connect esp default ap with mobile.
2. write code to connect home ap and save
3. run the code and show the obtained ip address on the result page
is it correct?
7 years ago
Cool inspiriting project.
My advices:
- on your transistor, you should add a pull-down resistor (~100Kohm) to avoid unwanted driving of your relay when the driving pin is in float state (can occurs when ESP82666 is starting-up / reseting or dead).
- be carefull with the duck you used. It is not an isolated duck. Also the hot glue does not have any official isolated properties. In addition, with the heat provided by the AC/DC 5V converter, the glue could warm up and melt => you will lost the isolation and cables could move and create short circuit. Keep in mind that you are working on mains !
Reply 7 years ago
thx for your remind, sinzu. I am not familiar to this scope, can you suggest another material instead of hot glue?
Reply 7 years ago
Instead of standard duck, use electrical duck or kapton duck (electrical isolated + heat resistant).
Instead of glue you could use potting to full your project box with resin. It is more expensive and does not allow modification once dry, but it is isolating and very resistant for a long life.
7 years ago
Just wondering if there's any other way to do this such that the physical light switch triggers the relay instead? And some feedback such the user can monitor whether the light is on or off. Also, what are the resistor and transistor for?
Anyway this instructable really helped me understand more about the ESP8226 and IoT. Thanks!
7 years ago
Hi,
Congratulations for this nice tutorial !!! I have 2 requests:
1/ I am looking over the internet for this Simenzis switch socket with USB charge plug, but it is just impossible to find it, even an equivalent. Do you have the reference for it ?
2/ the neutral wire is not used. What is the real issue for not using it ?
Best regards from Paris !!
Christiane
Reply 7 years ago
1/ http://b.mashort.cn/h.nk5xP?cv=AADkbqzy&sm=c38690
2/ USB panel require both Live and Neutral wire for converting 5 V DC
7 years ago
ESPBASIC is the best man gotta love it ...
I went ahead and made a dimmer switch and an android app to control everything for the living room I am very pleased with it and have shared my project as well on esp8266 website under mmiscool basic section and subsection is programming examples AC Light Dimmer....
Also I included an android application and I also wrote a small beginners guide to basic on the nodemcu/basic wifi dev kit.
Hope you all stop by the forums and check it out at esp8266 dot com.
7 years ago
I made something similar!
I just used a phone charger to power a wemos esp8266, and used the wemos relat shield. Then I crammed it into the plastic case from an old router:
I used NodeMCU/Lua to make a web server, here's the source:
https://github.com/dieseltravis/esp8266-projects/t...
Reply 7 years ago
*relay not relat
Here is the WeMos store:
http://www.aliexpress.com/store/1331105?spm=2114.1...