неделя, март 06, 2016

Smart Socket with ESP8266 EVB by Olimex and MQTT

I was fascinated by seeing tutorials about smart devices and people doing different smart setups so I decided to try it myself and have cheap and easy solution.

At the end result must be like this ( also notice how the lamp is cloud themed :) ):

1. The hardware

- The most important - relay board-  ESP8266-EVB from Olimex https://www.olimex.com/Products/IoT/ESP8266-EVB

 - Power supply adapter for the board -  you can skip that if you have 220 V to 5V 2A AC - DC power supply module transformer module, and put that directly to the wiring, but I don't have one so I am using this adapter https://www.olimex.com/Products/Power/SY0605E/

 - Contact-plug

- Junction box ( for safely placing ESP8266 inside and doing the wiring for the relay)

- Standalone outlet box - mine is with transparent cover

- Three wire electrical cable - in my case it was about 2 meters

Instead of all the above electrical parts you can always buy ready Electric power extension cord and then cut the wires

but it is up to you, I prefer to do everything myself... and something I noticed - some cheap  extension cords do not have grounding at all, for me it is important...

2. Preparing the extension socket

DANGER!!!! WATCH OUT! Exposure to high voltage (220V) can be fatal! Do not follow these steps if you are not sure what you are doing!

First, important thing is that you know meaning of colors of the cable, so you don't wire them in a wrong way.


So the yellow-green cable is Grounding!
Current will flow through Brown and Blue cables!

Ready to start?

So lets connect cables with sockets and plug:
 a) connect the plug

b) connect the socket

So you will notice that most important thing to watch out is the grounding cable.

Next prepare and connect Junction box:

I made some holes in the box, then installed the cable into two holes.
As you can see after removing the Cable Jacket I left brown cable longer than others ( pulling others from the ends to shorten them).
Then I cut the brown cable used some soldering for better connection and put some wire isolation, just for safety. 
At the end I tighten up the cable inside the box so it is not pulled out by dragging.

3. Placing the ESP8266 EVB inside the Junction box

Place the board inside the board, then connect with 5V power supply. Connect brown cables to the Relay's COM and NO ports:

And at the end Hardware part is ready!

4. Software

I am using Arduino IDE to program/flash the ESP8266 EVB board using USB to serial cables in UEXT port:

More details and tutorial, you can find here:

I decided to use MQTT protocol (http://mqtt.org/)  to send commands to my ESP8266 EVB. It is reliable and easy to use, also it is supported by libraries in Arduino IDE.

I modified example code provided here:

because it was too old and some of the import libraries already changed - I had to modify it in order to work.
You can find modified one here:

For MQTT broker I used open sourced Mosquitto :
But any other will do the work.
Don't forget to remeber the IP address of that broker.

Download the file relay-mqtt.ino, then load it inside Arduino IDE.
Update following lines with yours:

const char* ssid = "YOUR_OWN_SSID";
const char* password = "YOUR_OWN_PASSWORD";
//change with your MQTT server IP address
const char* mqtt_server = "";

Then set Arduino IDE with following board details:

where the COM port is the one where ESP8266 EVB is connected.
Then Compile and Upload the code to ESP8266 EVB.

The ESP8266 EVB will restart then and if you check the Serial Monitor you must see that ESP8266 EVB is connected to the WiFi and MQTT server.
If not - check coding for WiFi credentials or MQTT server IP address!

In order to test whether ESP8266 EVB is subscribed to the MQTT broker and listening for "demo/smart/socket/1/relay" topic, you can use extension for your browser to test the setup.
I have Chrome and there is very nice extension MQTTLens:

After installation you can run it and then connect to the MQTT broker.

Save the changes and then in Subscribe section write "demo/smart/socket/1/relay_state" inside topic input.

This is the topic which will be used by your ESP8266 EVB to send Relay status.
After a while ( max 1 min) you have to start receiving messages from subscription telling the status of your ESP8266 EVB.

If all above passed as expected, you are ready to send commands to ESP8266 EVB.

Now go the Publish section on your MQTTlens application and put in topic input "demo/smart/socket/1/relay" (without quotes!) with Message "on" (without quotes!)

When you press the Publish button you have to notice and hear that the Relay of your  ESP8266 EVB switched on. You must also receive status messages from your board telling that status is ON.

If you publish topic "demo/smart/socket/1/relay" (without quotes!) with Message "off" (without quotes!) you can switch off the Relay.

When you press the Publish button you have to notice and hear that the Relay of your  ESP8266 EVB switched OFF. You must also receive status messages from your board telling that status is OFF.

Now you are ready to try everything together and the result must be like in this video:

I am using MyMQTT application for Android to test the board.
You can install it also from here :

5. ... at the end

Of course there are a lot of things you can optimize and make better.

Let me know if you have any comments...

1 коментар:

Devanshi Srivastava каза...

Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.
Wifi Smart Plug