Introduction: ESP8266 BASIC IoT Light

Picture of ESP8266 BASIC IoT Light

This Instructables show how to use ESP8266 BASIC remote control a household light.

Step 1: Why Remote Switch?

Picture of 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

Picture of 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

Picture of 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.:

http://www.esp8266.com/wiki/doku.php?id=loading_fi...

Step 5: Preparation

Picture of 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

Picture of 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

Picture of Install 3 Pin Screw Terminal Block Connector
  1. Mark the Pin in the Socket
  2. Drill the holes
  3. 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:

http://www.esp8266basic.com/flashing-instructions....

http://recolog.blogspot.hk/2015/10/programming-esp...

Step 9: Connect and Config ESP8266 BASIC

Picture of 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:

  1. press [setting] link
  2. fill the AP name and password
  3. press save button
  4. before restart, open a serial console (Arduino have one) in the computer connected ESP through the USB FTDI programmer
  5. press restart button
  6. 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
  7. try connect to the IP address with web browser
  8. 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

Picture of 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

Picture of 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

Picture of Re-assembly the Switch Socket

Squeeze all component to the socket and then re-assembly the switch socket.

Step 14: Replace the Old Socket

Picture of 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:

  1. open the ESP IP address with the browser
  2. click [RUN] link
  3. press toggle button
  4. 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

Comments

meahdi (author)2016-11-03

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?!

陳亮 (author)meahdi2016-11-03

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

myesp8266 (author)2016-08-15

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

陳亮 (author)myesp82662016-08-15

whats mean cannot link? can you show me how you connect the USB serial cable to the ESP-12E board?

musthtc (author)2016-04-14

陳先生 你好:

我把你的程式加上一段設定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

musthtc (author)2016-04-12

陳先生你好:

很高興能看到你的大作,不過我有個建議,如果能將設定及連結WIFI AP的動作也融入程式中,然後可將分配到的IP直接傳到使用者手機端的話,這樣一般的使用者就不用再透過筆電等裝置去取得不就更方便了嗎?

陳亮 (author)musthtc2016-04-12

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?

sinzu (author)2016-03-22

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 !

陳亮 (author)sinzu2016-03-22

thx for your remind, sinzu. I am not familiar to this scope, can you suggest another material instead of hot glue?

sinzu (author)陳亮2016-04-01

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.

hydrogenhax (author)2016-03-29

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!

78squirel78 (author)2016-03-28

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

陳亮 (author)78squirel782016-03-28

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

mopowah. (author)2016-03-25

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.

diesel_travis (author)2016-03-25

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...

*relay not relat

Here is the WeMos store:

http://www.aliexpress.com/store/1331105?spm=2114.1...

Elnems (author)2016-03-25

You can do it by bluetooth in these link:
https://m.instructables.com/id/Live-Innovation-Making-DIY-Electronic/

poncos (author)2016-03-22

wow it's cool, switch only have 2 cable right ? how u add netral cable ? what component ? because i like see connector with blue cable , thanks for ur answer :)

陳亮 (author)poncos2016-03-22

sorry for misleading, all 3 wires break out from the relay are Live wires. But I don't have enough brown wire :P

poncos (author)陳亮2016-03-22

and then ? haha i don't get it -_- switch is a switch(2 cable) how can u change switch become (source(220vac) +switch) ?

陳亮 (author)poncos2016-03-22

I have mentioned at step 3, there may not have netral wire inside the socket box. You need to check this first. If it exists, you need to split a wire connect to the USB panel.

poncos (author)陳亮2016-03-22

and then ? haha i don't get it -_- switch is a switch(2 cable) how can u change switch become (source(220vac) +switch) ?

poncos (author)陳亮2016-03-22

and then ? haha i don't get it -_- switch is a switch(2 cable) how can u change switch become (source(220vac) +switch) ?

poncos (author)陳亮2016-03-22

and then ? haha i don't get it -_- switch is a switch(2 cable) how can u change switch become (source(220vac) +switch) ?

poncos (author)陳亮2016-03-22

and then ? haha i don't get it -_- switch is a switch(2 cable) how can u change switch become (source(220vac) +switch) ?

nonisnash (author)2016-03-21

What is the total cost of this project?

陳亮 (author)nonisnash2016-03-21

not over 50 CNY

CaptClaude (author)2016-03-20

Back EMF from the coil of the relay when it is switched can cause trouble in your circuit. Always use a flyback or snubber diode in parallel with the coil.
(Image borrowed from http://music-electronics-forum.com/t4456/)

陳亮 (author)CaptClaude2016-03-21

thx, updated step 11.

FelisC (author)2016-03-20

Note that the IP in step 9 doesn't always begin with 192.168, it can also begin with 10.0 or some other numbers. You can use the app Fing to check on your phone.

陳亮 (author)FelisC2016-03-20

really? but ESP8266 BASIC flashing page just said it should be 192.168.4.1

FelisC (author)陳亮2016-03-21

Some people have a different "IP Prefix" (not sure what it's actually called) but it's basically the first two numbers in the local IP of a connected device. I believe 192.168 is the most common in the USA, but mine is 10.0 and I've seen 192.174 and many others. It should always be 4.1 at the end though.

geeser (author)陳亮2016-03-20

some home networks use 10.0.... range for private IP

陳亮 (author)geeser2016-03-20

yes, geeser. 192.168.4.1 only the defaulf IP address when ESP act as an AP. Step 9 point 6 mentioned you need remember the IP address obtained show at serial console.

ssingh15 (author)2016-03-20

Can this be programmed to be used without a router ?

陳亮 (author)ssingh152016-03-20

yes, skip the setting of connect to home AP in step 9. then it keep boot as an AP.

Emmanuel2015 (author)2016-03-20

Nice project, you might use a small AC mains to DC 5V switching converter instead of using a USB plug. It depends if you need the USB feature, but if not it might be a bit less expensive and easier to install.

Maybe you could add the ground on your circuit sketch and clarify the terminal connections (the relay commands the live wire I presume ?). The relay and switch are in parallel on your sketch, I think. This means that the the switch and the relay have the same function (can switch on or off).

Anyway, this project is inspiring !

Cheers

陳亮 (author)Emmanuel20152016-03-20

hi, I agree it can be cheaper, but I am the lazy one. another reason I choose this type of socket because it has a plastic box big enough to store all the dc component.

AndrewW25 (author)Emmanuel20152016-03-20

I agree, i need to get making. The curcuit is for a basic two way switch which is spot in.

mmiscool (author)2016-03-18

This is awsome and it shows the simplicty of using ESP8266 BASIC.

Do you mind if i post this as a demo on the esp basic web site?

陳亮 (author)mmiscool2016-03-18

sure! good to see that.

theoriginalrage (author)2016-03-18

This is so cool!

Amit_Jain (author)2016-03-18

Good Work!!!! good idea to reduce size and fix everything inside such small space