loading

In this tutorial I'm going to show you how I made a wifi switch from scratch (or almost) using the well known ESP8266.

The idea is the following: I want to be able to turn on/off several lights or devices connected to the power supply from my phone. I don't want the switch to rely on a battery. The switch will need to connect to 1 of 2 preprogrammed wifi. I want to be able to make groups of switch, and turn them all on/off by the push of 1 button. I want to be able to turn it on/off at a specific day and time using a timer. And I don't want to pay more than a total of $30 per switch, since I want to make a dozen of them.

The components won't be put on a breadboard, but rather soldered on a PCB, in order to make the switch as small as possible, and to be able to handle the high power.

The interface between the switch and the phone will be managed by the Blynk app.

The end result is shown above: you simply connect the male plug to the wall socket, your device that you want to control to the female plug, and voilà! You can control it with your phone.

Since this is one of my first projects of this kind, I made a few mistakes that I realized afterwards. I'll note the improvements that can be made in this tutorial, along with the version 2 of the PCB. Please note that I haven't printed the version 2, so I have no idea if it actually works.

I'd like to point that I'm new to this field, so I might not use the correct terms: be gentle!

Step 1: Scheme and Components

I used the 2 following well-explained tutorials before doing this project:

Ok so I decided to use the ESP8266-01 for its cheap price and its small size. It has 2 digital outputs, we're only going to use 1 of them (GPIO2):

http://www.ebay.ch/itm/Orig-AI-Thinker-ESP8266-ESP...

For the relay, I've used a single relay board:

http://www.ebay.ch/itm/5V-1-Channel-Relay-Board-Mo...

The ESP8266 uses 3.3V, and the relay needs a 5V input to work. In order to convert the power to 5V, I've used the HLK-PM01:

http://www.ebay.ch/itm/HLK-PM01-AC-DC-220V-to-5V-S...

It takes 100 - 240VAC and converts it to 5VDC. In order to protect it from overheat, high voltage and high current I've used a 73°C thermal fuse, a 250VAC varistance and a 315mA fuse:

Then to convert 5VDC to 3.3VDC I used the voltage regulator LD1117V33:

http://www.distrelec.ch/fr/regulateur-de-tension-l...

On top of that I've used the following parts:

The scheme and the board were made using the free version of Eagle. 2 layers were used for the board, though only 1 could have been used. The board was pannelized and 20 copies were printed for about $100 by Gold Phoenix.

If the ESP8266 was soldered on the board, it would need to be desoldered to reprogram it. So instead I soldered 2 rows of female headers and plugged the ESP8266 on it. (36-pin 0.1" Female header)

  • Improvement 1: The ESP8266 plugged in the headers is quite elevated on the board, this means the 2 capacitors could have been turned 90° and bent under the ESP8266 to gain some room. This has been corrected in version2.
  • Improvement 2: The 315mA fuse drawn on the board is square-shaped, but I could only find round-shaped fuse for that current (the pitch is the same so it's fine). Corrected on version2.
  • Improvement 3: The 3.5mm screw terminal chosen are really too small to put the power cable in, especially that I needed to put 2 wires in each hole to connect the relay in parallel. A bigger, 5mm screw terminal has been chosen for version 2 (https://www.sparkfun.com/products/8432).

Step 2: Assembling

After burning 2 thermal fuses when soldering them, I found the following trick to solder the next ones succesfully:

  • Place the thermal fuse first on the board, before any other component
  • Bend it so it will be placed close to the HLK-PM01
  • Turn the board over and place it on a cup of water. The body of the fuse has to be submerged so the temperature increase will be slowed down, but the board should not touch the water, otherwise the soldering would be impossible.
  • Solder the fuse by being carefull not to heat the part too much or too long.

The other components presented no particular problem.

The ESP8266 is then programmed using Arduino IDE (see next step for the program). The scheme to program it is the one presented in this tutorial. An FT232RL USB to TTL Converter Module was used: http://www.ebay.com/itm/302036807558

Before uploading, the ESP has to reset with the GPIO0 to the ground.

The ESP8266 is then plugged on the board, and the relay plugged in the jumper wires.

The power lines were connected according to the scheme and pictures above.

The whole thing was put in a plastic case with a hole on each side, and then a male and female plug were connected.

Step 3: Blynk App and Programming

The Blynk interface

My Blynk interface has 4 tabs:

  • The 1st tab controls switch 1 to 10 individually using buttons. The buttons send a high(1) or low(0) value to the virtual pin V0. Next to each button, an led is used as a feedback. It reads the virtual pin V1. If the relay is closed, V1 is high, and the led lights up.
  • The 2nd tab also controls switch 1 to 10 but by groups: the first button controls all the lights of bedroom1, the second one controls all the lights of bedroom2, etc...). These buttons use the virtual pin V2. For this, tags had to be set to group several switches on the same button.
  • The 3rd and 4th tabs control switch 11 to 15 individually. They can be controlled directly with a button via virtual pin V0, or by a timer. The start/stop time and weekday are set using the Time Input widget via virtual pin V3. The timer functionality can be turned on/off using the last button, via V4. The ESP8266 knows the date and time by Blynk, through the RTC widget.

Note1: Unchecking all weekdays in the time input widget does not turn the timer off. It actually turns it on everyday!

Note2: In order to use the RTC widget with the ESP8266, you need to have the latest app version, and at least library version 0.4.4 (at the time of writing, version 0.4.4 is not available yet in the arduino IDE and you need to manually download it here).

The software

The code I wrote is presented in the GitHub repository below. I think it should be well-enough commented to understand it.

https://github.com/vjuvet/ESP8266-wifi-switch

Step 4: Improvements

Some improvements can be brought to this project and are corrected in the version 2 of the GitHub repository.

  • The rectangular TE5 amp fuse is replaced by a round TR5
  • The 3.5mm screw terminal is replaced by a 5mm screw terminal. Part: https://www.sparkfun.com/products/8432
  • Capacitors are turned 90° so they can be bent under the ESP, and ESP moved up
  • More room is reserved for the relay

Once again, I haven't tried version2, and I don't plan to print it since version 1 works. So please double-check version 2 if you want to use it. And let me know if you find any mistake.

If you want to build this project or part of this project, do it at your own risk. I do not guarantee its functionality or its safety. The project is licensed under MIT License. This means you can use it for commercial purposes, distribute it or modify it, only include a copy of the license and copyright notice with it.

If you have any other suggestion, or feedback, please let me know! Looking forward to have your input.

<p>I'm wanting to do something like this, except that the 30A relay needs 12VDC to switch. Is something like this: http://www.digikey.com/product-detail/en/rohm-semiconductor/BP5037B12/BP5037B12-ND/658553 similar to the HLK-PM01? It looks like it should be able to output 12VDC from 120VAC (our local current). The HLK-PM01 looks like it may need less components to make it work, so if there is a 12V version that would be cool.</p>
<p>Never mind, it looks like the HLK-PM12 is the 12V version of this. I'm curious why I can't find these on Digikey or anywhere else besides ebay...</p>
Hello,<br>I don't know much about the BP5037B12 but it looks like it should work. Just don't forget to update the pcb since the pitch is not the same. Or the hlk-pm12 is ok too.<br>The LD1117V33 can take up to 15V dc and convert it to 3.3V dc so you're fine with this.<br>Best of luck, and please keep me updated about the result.
<p>Cool project. Some savings might be to use breadboard instead of the cost of plated PCB. Do not see need for terminal strips, just solder directly as you are not interfacing to other projects. As suggested to put IN4001 diode and maybe capacitor of small value to remove spikes, which could kill the transistor. Stripe end to positive voltage feed. If there are 2 outputs, how about adding second relay, resistors, diode and transistor to have 2 outlets thus able to control 2 lamps or devices at same time. Adding a fuse to the output to prevent overloads and for safety. If space is not a consideration, repurposing a cell phone charger works great for a source of 5 volts and also may provide a led to indicate basic power. Can the software be made for time delay? That would be useful to make it serve as a entry way lighting, or sleep timer for any room. </p>
Thank you so much for your comment.<br>I was not sure if a breadboard could be used with voltage as high as 230V.<br>Using the 2nd output of the Esp for another relay is possible and is a good idea.<br>I thought about using a battery to supply 5V, but I didn't see the utility: the 5V can be supplied by the ac/dc converter, and the 5V will continue to be supplied until there is a power failure. And if there is a power failure, I'm not going to be able to turn my light on anyway, so I don't care if I cannot control my board.<br>Yes the software was made for time delay. The rtc is provided by the blynk server, and the time information is sent from the app to the board through virtual pin v3.<br><br>Best,
Great product i make an wall outlet with arduino pro micro 2led and bluetooth hc-06 and one relais. But you idea is most powerfull. Your adition (thermal fused + 1switch to control all) is very great. Great diy with esp8266. I am debutant in arduino and electronics and the esp8266 is more complicated for me at this time. But i will made your idea after. (Eye blink on switzerland connector outlet)
I'm working on a similar design and added an override push button switch. It works but has issues I hope someone can help me with. <br>I use GPIO 2 as input to sense the override switch so GPIO0 controls the relay. However if pin 0 can see gnd at startup it goes to programming mode. If I manually disconnect it during startup an then plug it in all is fine. <br>My question is there a technique or device that would caus the pin to &quot;float&quot; for a short time ( milliseconds I'd guess) then connect?<br><br>Thanks for any advice.
<p>Hi,</p><p>This is an interesting question, I think it has been discussed on the esp8266.com forum: <a href="http://www.esp8266.com/viewtopic.php?p=1551" rel="nofollow">http://www.esp8266.com/viewtopic.php?p=1551</a></p><p>Best,</p>
Thanks!<br>That sounds like what I need.
<p>Nice little design,.. but something you may want to consider is to install a small diode ( eg. 1N4148 ) across the relay coil to capture the switching transient excursions,.. as these transients can damage other circuitry.... but just a precaution....</p><p>Rgds</p>
Thank you for your comment. I didn't know about transients. Do you think that this protection might already be included in the relay board?<br><br>Best,
You may be correct,.. but the modules I looked at do not give a simple circuit diagram to review, ( have you found one ).. but the picture does show a small diode, transistor, LED, and a resistor. I might guess the diode is there for reverse polarity protection, as it only seems to be a 1N4001 type rectifying diode,.. not fast enough for reverse transients protection.<br>I typed &quot;driving a relay diode protection&quot; into google and got loads of example circuits if you care to follow up etc.<br>Rgds
One thing I have often wondered... I love this idea, but do the lights maintain the ability to be switched on/off from a wall or local remote switch? Because, say I have family in town and they need light in the middle of the night to find the bathroom, do they(or I for that matter) need to use their phone for that?
I'm glad you asked that. The reason I did this project is because none of my lights are connected to a wall switch.<br><br>Using the phone to turn them on/off is temporary, I plan to make a few portable switch, connected to a battery to control them all. But this will be another project that I will publish.<br><br>Thank you for your interest,
<p>If you are using AC power, you can easily remove the relays and go TRIACS instead. Then everything is a lot more reliable and solid state. I actually designed three phase circuits using them. Works perfectly.</p>
<p>I'd love to hear more about how you did it with TRIACs. I had a similar situation with another circuit and someone suggested SCRs instead of TRIACs. I'm still learning and don't really know the why &quot;this is better than that&quot; sometimes. I'd also be interested to hear how you protected the circuit (fuses would be the route I would go without any other guidance). </p>
<p>I had the same idea, but i think is more convenient to buy a SONOFF for 6&euro; on Aliexpres. I tested it just yesterday, and works very well!</p>
<p>Yes I did the same from Ebay as it would be a challenge to beat that price but tinkering is a good way of learning for future ideas.</p>
<p>No contests?</p>
Just today I finished my prototype, which is based on ESPeasy firmware of ESP. Since that firmware has web interface, it is easy to setup Wi-Fi connection, and rearrange assignment of GpIOs. I plan to build smart home elements based on that setup and Domoticz smart server. Beside that, if power allows, it might be better to use solid state relay to make device act silebtly.
<p>Very impressive. I have been meaning to make a IOT switch using the same concept. I recently purchased an IRIS outlet at lowes for $25. I really like the fact I can feed it my wifi info through the app without having to connect it to a computer first. I also really like that it has a manually toggle switch so I can turn it on manually if needed. (It also has a small red &amp; blue light indicator). Those are some features you might want to look into for the next design. </p><p>I totally plan I using some of your design with my IOT switch. I recently installed lights on my shed so I can see when I am in the backyard, but they can only be turned on from the shed. At least for now. I plan to add the IOT switch which can also be manually overrided if needed. I have a Test Bed version of it working on a bread board. I will make an instructable when I am done. </p><p>I love blynk, but I really want to program my own code so I can put in schedules and what not. </p>
<p>Thank you for your suggestions.</p><p>With this project, if you need to add a wifi SSID and pass, you need to open it, take the ESP off, reprogram it, and put it back. Feeding the wifi through the app would be really convenient.</p><p>Adding a button would also be interesting, in case you don't want to take your phone out of your pocket. And it's possible to use the GPIO0.</p><p>Thanks for your interest in this project!</p>
Awesome.
<p>Really cool project! </p>

About This Instructable

32,640views

381favorites

License:

More by ValentinJ2:Wifi Camera Remote ESP8266 Wifi Switch 
Add instructable to: