Introduction: ESP8266 - $5 Internet Connected Switch

In this tutorial we will show how to build WiFi internet connected switch connected to EasyIoT Cloud. We will use ESP8266, solid state relay and Arduino IDE. Switch can be controlled by WEB application, native Android application or button.

Improved version of this switch can be found here:

https://www.instructables.com/id/ESP8266-5-Internet-Connected-Switch-Improved/

This internet switch is "plug and play" - it will automatically set all EasyIoT Cloud settings, so no configuration in EasyIoT Cloud is needed.

Material for switch cost about 5$.

WARNING!! You will play with LIVE MAINS!! Deadly zone!!
If you don't have any experience and are not qualified for working with MAINS power I will not ecourage you to play arround! Do NOT use it without proper Knowledge about MAINS circuits ! Do NOT use it without a proper FUSE on MAINS line! Max current for solid state in this tutorial is 2A - suitable for room light only.

Step 1: Material

Materials:

  • ESP8266 WiFi module
  • Solid State Relay 2A 240V
  • 3.3V 600mA AC-DC step down module
  • Push Button switch
  • NPN Transistor TO-92 2N2222
  • 1000uF electrolytic capacitor
  • resistor, 1K, 47K

Click to buying guide for ESP8266 internet connected swith.

Step 2: EasyIoT Cloud Registration

Register to EasyIoT Cloud. You will need username and password later in program. Also use username and password to access EasyIoT Cloud and control your device from computer or mobile phone. Also EasyIoT Cloud Android application is available.

Step 3: Program

Program is written in Arduino ESP8266 IDE. See Arduino ESP8266 IDE tutorial how to connect ESP8266 module to computer to upload program. Program can be downloaded from GitHub. You will also need MQTT client library. Add this library to library folder in Arduino IDE. Program uses EasyIoT Cloud MQTT API.

In program change following lines to set access point username and password and your EasyIoT Cloud username and password:

#define AP_SSID "xxx"
#define AP_PASSWORD "xxx"

#define EIOTCLOUD_USERNAME "xxx"

#define EIOTCLOUD_PASSWORD "xxx"

Step 4: Hardware

In our case we use ESP8266 01, but you can use any other type of ESP8266. GPIO2 is connected to NPN transistor to control SSR. Max current for our type of solid state relay is 2A - this is suitable for room light and not for applications which consume more power - for example heater.

For power supply we use 3.3V step down module. It's very important to add 1000uF capacitor to 3.3V power line - in our case switch didn't work if we skip this capacitor. GPIO0 is connected to push button for local control of internet switch.

After power on, switch will be automatically added to EasyIoT Cloud and it will be visible in WEB interface or Android application where you can control your device from remote locations.

Comments

author
juaalta made it! (author)2017-04-10

Where can I get the enclosure? It's not listed in your buying guide.

author
djelincic made it! (author)2015-11-30

How can 3.3V output from ESP8266 trigger 5V relay?

author
BallscrewBob made it! (author)BallscrewBob2015-12-04

In my mock up the 3.3 triggers an opto switch that is part of a chinese relay module which allows the 5V to actually operate the relay.

In this instance it is triggering a transistor as a switch to allow a larger load to operate the relay.

Sort of like a relay to operate a bigger relay / larger load

See the link for a little more explanation.

http://www.hobbytronics.co.uk/arduino-tutorial9-po...

author
djelincic made it! (author)djelincic2015-12-22

Great, that makes sense! Thank you for explanation.

author
MarkS654 made it! (author)MarkS6542016-11-09

It doesn't make sense, I'm afraid - there are traces of a couple of misunderstandings there. The ESP cannot supply enough current to drive a relay, and that's what you need a driver (e.g. the transistor) for, but in this circuit that does NOT increase the voltage - in fact (again, in THIS circuit) it decreases it. In the circuit as drawn, the relay is going to see a voltage of about 2.7v: it's being powered by a 3.3v supply, and you're going to lose about 0.6v of that across the transistor. Now, perhaps the relay will still work at 2.7v - I haven't tried - though it's not guaranteed. Really you either need a lower voltage relay, or a higher voltage supply for the relay. A supply giving 5v for the relay and 3.3v for the ESP would do nicely.

author
darcicidade made it! (author)2016-10-19

Is it possible not to use the automatic registration of parameters?

and, register manually the parameters!

// Create Sensor.Parameter1

#ifdef DEBUG

Serial.println ( "/" + String (storage.moduleId) + "/Sensor.Parameter1/NewParameter");

#endif

myMqtt.subscribe ( "/" + String (storage.moduleId) + "/Sensor.Parameter1/NewParameter");

waitOk ();

author
NikkiS8 made it! (author)2015-11-22

Thanks for nice tutorial. How can I use it behind the proxy server ( my institute have proxy server with id and user name)

author
IshuJ1 made it! (author)IshuJ12016-08-18

Did you solve the proxy problem ? The same condition is in my institute also , I have also contacted various forums but they couldn't solve the problem.

author
JasonG123 made it! (author)2016-07-11

follow instructions and you will solve your error

author
JasonG123 made it! (author)2016-03-25

I receive this error .... seems im not the only one?

ESP MQTT\mqtt\mqtt.c.o:(.bss.default_private_key_len+0x0): multiple definition of `default_private_key_len'

ESP8266WiFi\WiFiClientSecure.cpp.o:(.bss.default_private_key_len+0x0): first defined here

ESP MQTT\mqtt\mqtt.c.o:(.bss.default_certificate_len+0x0): multiple definition of `default_certificate_len'

ESP8266WiFi\WiFiClientSecure.cpp.o:(.bss.default_certificate_len+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

Error compiling

author
ChứcN made it! (author)ChứcN2016-04-16

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0ahUKEwiY5cPPwZTMAhUG5aYKHRCeBvsQtwIIIzAB&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DC1XSj9YvZtA&usg=AFQjCNG2YpTCfuuqjAY4yPFkmhzMrVZfxA&sig2=4Uhsl1oD2lu2ZBpp1PFAdw

author
miguelgarciabs made it! (author)miguelgarciabs2016-05-11

Hi ChứcN

I have exactly the same problem as JasonG123 .

Supposedly the link you posted has the answer to him but, when I copied it and pasted it into my web browser address bar, I get:

Redirect Notice

The previous page is sending you to an invalid url.

If you do not want to visit that page, you can return to the previous page.

Can you please tell me how to correctly reach this webpage, supposedly with the answer to the JasonG123 (and also mine) problem ?

Lots of thanks in advance

author
miguelgarciabs made it! (author)miguelgarciabs2016-05-11

Hi ChứcN

I have exactly the same problem as JasonG123 .

Supposedly the link you posted has the answer to him but, when I copied it and pasted it into my web browser address bar, I get:

Redirect Notice

The previous page is sending you to an invalid url.

If you do not want to visit that page, you can return to the previous page.

Can you please tell me how to correctly reach this webpage, supposedly with the answer to the JasonG123 (and also mine) problem ?

Lots of thanks in advance

author
zikzak1 made it! (author)2015-11-16

I just use my firewall/router. Log into it from anywhere. Added bonus, I can shut a single LAN IP address down, or all, or check traffic, or redirect, etc. Your device could come in handy for a few other things I can think of though!

author
iftikhar1 made it! (author)iftikhar12016-04-08

can you please shear your work.?i want to control esp8266 pins from global network.

author
Pekkis made it! (author)2016-03-31

I have same problem than JasonG123

author
ChứcN made it! (author)2016-03-08

thank you very much your share

author
aegis21 made it! (author)2016-01-16

Any help on the enclosure would be appreciated, great work!

author
Solominator made it! (author)2016-01-13

...Aehm... It look like that you have voltage on the lamp even when it's not on...

this can be dangerous.

I think it would be better, when you connect the relais first to the power line and then the lamp... then you don't have voltage on the lamp, when the relais is off.

author
Arty Marty made it! (author)2015-12-13

Just wondering if this relay would also work or be better.
http://pages.ebay.com/link/?nav=item.view&id=351542314917&alt=web
It can be triggered by 3v and looks like it can handle 240v+ at 20A+ so could be used to operate Hi-Fi amp etc.
I want to be able to turn my Stereo on/off with my phone to control music through the house.
Would this work with this relay?

author
adillbeck made it! (author)2015-12-02

What I'd really love would be to be able to make a 3 pole switch, so I could put it in line with a normal 3 pole wall switch in order to control lights remotely while still being able to control them locally too...

author
BallscrewBob made it! (author)BallscrewBob2015-12-04

I found that I can do just that. I can trigger my relay manually and latch it.

Seems that easyIOT does not check the state of the relay it just sends a command.

I can power mine down even and the easyiot does not know about it but will send a signal and tell me its on when its actually off. Most of the cheap chinese relay module have both an NC and an NO connection so they are pretty versatile.

It would be nice if the IOT could register the state so I would know of power failures etc.

author
BallscrewBob made it! (author)2015-11-28

Update;

The 1000uF was to much and I was getting random drop outs. Went down to a 0.1uF electrolytic in my test rig as close to the ESP power and gnd pins as I could and it seems to have cured it.

author
BallscrewBob made it! (author)2015-11-28

I have to say I am very impressed.

This is the first Instructable for the ESP that worked right out of the box.

Built a mock up and its off to the races.

Did have to change the logic as I wanted my relay module LED to be OFF when the switch was off as I was using those cheap Arduino relay modules until my stuff from EBay arrives.

Do have a question. If I set up an MQTT side server on my real server here could I use that and NRF2401's to keep the RF devices slightly isolated from regular WiFi and still pass info back to IOT ?

author
AledM made it! (author)2015-11-16

Nice build!

Can I ask for more details on the little 3.3V power supply you used? Where did you buy this and how much did it cost?

author
EasyIoT made it! (author)EasyIoT2015-11-16

Hi, it it cost 2$.

All material is listed on following link:

http://iot-playground.com/2-uncategorised/77-esp82...

author
pmshah made it! (author)pmshah2015-11-27

I have the same question as asgari. The housing is not listed. Did you salvage it from some kind of remote switch or power monitor? Rigging it with some other kind of setup would make is a bit too clumsy and non-mobile.

author
Nickduino made it! (author)2015-11-22

Could someone tell me why we *need* that capacitor?

author
jimvandamme made it! (author)jimvandamme2015-11-22

There's already a filter cap on the power supply, and the supply is way overkill for the load. So all I can figure is that the circuit puts a glitch on the power that messes up the chip.

author
ecochran3 made it! (author)ecochran32015-11-24

1000 uF is a rather large capacitor, so I guarantee it's for voltage filtering. (Oddly, there's no capacitor for debouncing the push button.) Depending on how sensitive the ESP8266 chip is to ripple in its input voltage, the filter capacitor in the supply may not be enough. (Check the datasheet; it may list a ripple voltage tolerance.) This cap tends to be a cut corner in cheaper power supplies; some bargain-basement power supplies may not include a filter cap at all, relying on DC averaging and the input voltage filtering implemented in the device they're feeding. (Again, on cheap electronics this may not be implemented.) Even on high-end supplies, it's nearly impossible to get rid of all ripple voltage. (The more filtering required, the bigger and more expensive the capacitor(s) will be.)

author
Umbra55 made it! (author)2015-11-23

Nice
project!

I have a
few “electronics for dummies” questions:

If you don’t
need the button, I assume that you can also remove the most leftmost resistor. Any
other components that may be removed in that case?

Since this esp8266
module has more than one GPIO, would it be possible to use a module with 6
relays and switch those independently via the smartphone app?

What’s the
advantage of EasyIot Cloud vs Blynk?

author
mid_life_crisis made it! (author)2015-11-23

The device may only handle 2 amps, but that is more than enough to control the coil on a heavier relay that will handle just about any circuit in most houses.

author
mdeudon made it! (author)2015-11-23

It is advertising !! Interessesting anyway.

author
rbright made it! (author)2015-11-23

What is the effective range within a typical house with maybe a wall or two between

author
royea made it! (author)2015-11-22

The SSR should switch the power line, not the ground line. why ?, because the lamp/bulb will always be "hot". If someone will think it is off, and will replace the bulb... surprise...

author
Dodutils made it! (author)2015-11-16

2A at ~200-240V gives you minimum of 400W which is enought for more things than light, for example a TV, radio, non video gaming computer.

author
TechPaul made it! (author)TechPaul2015-11-22

the startup surge on some devices can be large, just use a wattmeter with a Max current/power function to be sure.

author
CraigL20 made it! (author)2015-11-22

Nice write up on an interesting project.

author
gcai_fwb made it! (author)2015-11-22

except if you live in Quebec where 5$ is correct and I agree with blenderbender below

good 'ible

author
blenderbender made it! (author)2015-11-22

c'mon, cut the guy some slack... we all knew that 5$ is $5

author
MillerI made it! (author)2015-11-22

Sorry, I'm not a math professor but over here in the States that material list, not including the housing, comes to over $12.

author
EasyIoT made it! (author)2015-11-22

Thx. I will fix that. I'm not native English speaker as you may notice...

author
asgari made it! (author)2015-11-17

Where can I get the enclosure? It's not listed in your buying guide.

About This Instructable

70,875views

604favorites

License:

More by EasyIoT:ESP8266 Smart Plant Irrigation SystemESP8266 Controlling 4 RelaysESP8266 Door/window Sensor With SMS Alarm
Add instructable to: