Introduction: How to Use MQTT With the Raspberry Pi and ESP8266/sonoff

Hello All!

Today I am going to show you how to configure a raspberry pi and ESP8266 based sonoff wifi relay switch to control your home appliances in from anywhere in the world.

By end of this instructable, if u have carefully followed my instruction, you will be able to control any of your home appliance from Raspberry Pi's terminal or by using python shell.

So get ready to make your own MQTT and Rpi based Sonoff Wifi Device controller!

Before starting this instructable I would like to mention few very important things,

  1. The sonoff wifi relay switch comes with its own control firmware. once you reprogramme it, original firmware is erased permanently. So make sure of that.
  2. The sonoff wifi relay switch is directly powered through 230V AC voltage, so if you are not the expert in handling such high voltages take help of your parents or expert person.
  3. The sonoff wifi relay switch is simply based around esp8266-01 wifi chipset which is a 3.3V chip, if you accidentally apply 5V to its +V pin you will damage it for sure.
  4. Here for this instructable, I have considered that you have your raspberry pi all set-up and wifi enabled with IP address to run the mosquitto and MQTT commands in Terminal

** If you are ready with all the above 4 steps you are good to go!!

Step 1: Introduction + Supplies

Hello Guys! today we are going to build a very interesting project. Here in this project, We are going to control our Home devices such as AC lamp using MQTT broker.

  • Note: This is an IOT Project hence you can control your AC device from anywhere in the world.

MQTT ( Massage Que Telemetry Transport) is a Simple payload Transfer protocol used in a verity of Web projects. Facebook messenger also makes use of MQTT Protocol. enough of introduction lets start with Project.

What do we need for this Project?

1) Active Wifi (router preferably) connection

2) Raspberry Pi3 model which has wifi enabled on it.

3) Sonoff Wifi IOT switch with 10A 220V

4) Some connecting Wires

5) Jumper Cables

6) CP2102 USB to TTL converter

  • Note: for this project, u must have raspberry pi all setup with either monitor keyboard connection or in headless mode with your laptop(VNC connection)
  • if you don't have raspberry pi all set up with monitor or headless mode, you have to follow my other tutorial on how to connect raspberry pi in headless mode.

Step 2: Setting Up Raspberry Pi

In this Step, we will set up our raspberry pi for the Project.

in this tutorial, we are using sonoff ESP8266 as client and raspberry pi as a mosquitto broker.

  1. open your raspberry pi terminal and type
    1. sudo apt-get update
    2. sudo apt-get upgrade Once Your raspberry Pi is updated with the latest firmware you are good to go with any project on your pi
  2. Now type following commands to install Mosquitto
    1. <p>wget</p>
    2. <p>sudo apt-key add mosquitto-repo.gpg.key</p>
    3. <p>cd /etc/apt/sources.list.d/</p>
    4. <p>sudo wget</p>
    5. <p>sudo -i </p>
    6. <p>apt-get update </p>
    7. <p>apt-get install mosquitto</p>
    8. <p>apt-get install mosquitto-clients</p>
  3. Once you have successfully followed all the above instruction you have your machine ready with mosquitto broker set up and you are ready to the next step.
  4. Again in your terminal type the following commands:
    1. <p>sudo pip install paho-mqtt </p>
    2. <p>sudo pip3 install paho-mqtt</p>
  5. Now in order to check whether your mosquitto client is successfully installed or not open python2 or python3 shell and type following command
    1. <p>import paho.mqtt.client</p>
    2. this should not return anything. if returns any line or error check the above steps again and follow the steps carefully once again.

  6. Now your Raspberry Pi Part is almost done and you are good to go for Sonoff and Arduino part now

Step 3: Setting Up the Sonoff Esp8266 Device for MQTT

Now in this step, we will set up our client device which is Sonoff.

sonoff is a wifi home automation device which comes with preinstalled firmware for wifi-based home automation and android/ios application to control the appliances over wifi.

Now here is a simple hack to reprogramme it in the way we want. once reprogramme with Arduino ide you will lose the original firmware preinstalled on sonoff.

  1. first, open your sonoff device carefully and look out for the pinouts shown in the image DANGER: sonoff device works on 220V direct AC you must plug it off first before opening or programming.
  2. Now see the bellow image for its pinout, on the underside of PCB there is an esp8266 chip that you find similar to your esp8266-01 module.
  3. Now open the Arduino IDE and add the necessary Zip libraries. PUBSUB_client Library
  4. Also, you must have the esp8266 board added to your Arduino ide. if not already added follow the steps
    1. Open your Arduino IDE and click on "File -> Preferences".

    2. <p>""</p>
    3. In "Aditional Boards Manager URLs" add this above link and click on "OK"

    4. Go to "Tools -> Board -> Boards Manager", type "ESP8266" and install it.

    5. Go again to "Tools -> Board" and select "Generic ESP8266 Module".

    6. Now you have successfully added the board to your Arduino IDE.

  5. Now you have to solder one female connector to the sonoff device as shown in an image.
  6. Now take the USB to TTL CP2102 or FTDI board and make the following connection as per the Image shown above.
    1. RX of FTDI to TX of Sonoff
    2. TX of FTDI to RX of Sonoff
    3. GND of FTDI to GND of Sonoff
    4. 3.3V of FTDI to 3.3V of Sonoff
  7. NOTE: Make sure that you are not supplying the sonoff board with 5V. it is 3.3V compatible if your connect 5V to its Power pin, you may burn your sonoff device. hence better use 3.3v supply of FTDI board only.
  8. now make the connection as shown in an image.
  9. One your are done with the above steps. connect your FTDI/USB to TTL board to laptop/PC and open Arduino IDE.
  10. Copy the code.

11. Now select the proper COM port from the Tools menu and select the device as a generic ESP8266 device. Now hit the upload button. It will take around a minute or less to upload the code into the esp8266 chip of the sonoff device.

12. Now you are all set with sonoff and Arduino programming. and now you should see the led on the sonoff board blinking // as we have written 5 times blinking in the start.

13. You are good to go to the next step.

Step 4: Control Your Sonoff From Web

Now, this is the final step to our project.

  1. now open your VNC remote desktop connection of raspberry pi by entering the IP address for your Pi
  2. now open the terminal and start typing following commands
  3. here led is topic and "0" is payload
  4. change the IP address in command with your Rpi IP address
  5. And instead of "0" write "1" which will turn on your relay on the sonoff wifi switch.
mosquitto_pub -h -t led -m "0"         						

The above instruction will turn off your relay.

mosquitto_pub -h -t led -m "1"

This instruction will turn On your relay.

Now you are done with controlling your sonoff from the Rpi terminal.

you can check by connecting the sonoff wifi switch to a different wifi network and raspberry pi to a different wifi connection. You should be able to control the relay from anywhere in the world.

Now even further you want to test the ON/OFF of your sonoff automatically I have written a small python script

which sends the "1" first and after 6 seconds it sends "0" and this process repeats in a loop.

import paho.mqtt.publish as publish
import time

While True: print("Sending 1...") publish.single("ledStatus", "1", hostname="Your broker IP") time.sleep(6) print("Sending 0...") publish.single("ledStatus", "0", hostname="Your broker IP") time.sleep(3)

The above code simply turns on and off the relay on the sonoff wifi relay switch.

don't forget to replace your RaspberryPi's IP address in the above python code. now run this script by clicking on option run module and you should see on the shell

sending "1"..

sending "0"..

sending "1".. something like this.

That's it for this Instructable. see you next time with a new one.


and don't forget to follow and share this instructable and try it on your own!

any doubts post in the comment section is welcome!