Introduction: How to Use MQTT With the Raspberry Pi and ESP8266/sonoff
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,
- 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.
- 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.
- 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.
- 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.
- open your raspberry pi terminal and type
- sudo apt-get update
- 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
<p>sudo apt-key add mosquitto-repo.gpg.key</p>
<p>sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list</p>
<p>sudo -i </p>
<p>apt-get update </p>
<p>apt-get install mosquitto</p>
<p>apt-get install mosquitto-clients</p>
<p>sudo pip install paho-mqtt </p>
<p>sudo pip3 install paho-mqtt</p>
this should not return anything. if returns any line or error check the above steps again and follow the steps carefully once again.
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.
- 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.
- 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.
- Now open the Arduino IDE and add the necessary Zip libraries. PUBSUB_client Library
- Also, you must have the esp8266 board added to your Arduino ide. if not already added follow the steps
Open your Arduino IDE and click on "File -> Preferences".
In "Aditional Boards Manager URLs" add this above link and click on "OK"
Go to "Tools -> Board -> Boards Manager", type "ESP8266" and install it.
Go again to "Tools -> Board" and select "Generic ESP8266 Module".
Now you have successfully added the board to your Arduino IDE.
- RX of FTDI to TX of Sonoff
- TX of FTDI to RX of Sonoff
- GND of FTDI to GND of Sonoff
- 3.3V of FTDI to 3.3V of Sonoff
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.
- now open your VNC remote desktop connection of raspberry pi by entering the IP address for your Pi
- now open the terminal and start typing following commands
- here led is topic and "0" is payload
- change the IP address in command with your Rpi IP address
- And instead of "0" write "1" which will turn on your relay on the sonoff wifi switch.
mosquitto_pub -h 192.168.0.104 -t led -m "0"
The above instruction will turn off your relay.
mosquitto_pub -h 192.168.0.104 -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
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".. 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!