In my previous blog post I have tried to explain why MQTT is the right protocol for IoT scenario’s. So it would be nice if the ESP8266 would be able to leverage this protocol as well. And it does! Over on the ESP forum, Tuan PM has been working on code to allow us to use the ESP boards as an MQTT client. The forum post is here and the wiki here.
To get the ESP8266 to talk to the Raspberry Pi, I flashed Tuan PM’s MQTT examples with a few changes into the ESP and installed Mosquitto on my Raspberry Pi. It was my purpose to see if I could switch a led on and off through MQTT. If I could do so, I would be able to do the same with a relay and e.g. switch a lamp on and off.
Installation of Mosquitto on the Raspberry Pi
The installation of Mosquitto on a Raspberry Pi is extremely simple. If you start with a Rasbian image on your Pi, the only thing you have to do is to pick up mosquitto from the respository of Roger Light, the creator of Mosquitto.
If you login to your Pi and execute the commands below, you’ll have a Mosquitto broker running right after that:
curl -O http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
sudo curl -O http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update
sudo apt-get install mosquitto
After executing the last line, you should eventually see “[ ok ] Starting network daemon:: mosquitto”, meaning that Mosquitto is being started.
To test if it runs properly, you could use MQTT.fx
It is very easy to setup MQTT.fx. Using the cog, you will be able to setup a connection to your Pi. Once you have setup the connection, you’ll be able to press the connect button to connect to the Mosquitto service on your Pi. Once you’re connected, you could subscribe to a test-topic and send a message to that test-topic. The message you published, should also be received back again.
To see how that goes, just have a look at the video below.
Note: for the easy of sake, I have not setup an credentials and SSL in my installation (maybe I’ll do that in a future blog post). And if you’re running the MQTT broker in your home, this wouldn’t really be necessary. However, if your broker is running at another site, I would advise you to secure your connection.
Now the MQTT server is working, it is time to connect a couple of IoT things. I have made two of them for this experiment. The first one is a MQTT Wifi controlled power-strip and the second a MQTT Wifi thermometer. I thought it would be fun to use the in conjunction, by switching on a fan when the temperature gets above a certain threshold.
MQTT Wifi Power Strip
For the Wifi power strip I have bought a power strip at the local DIY store containing a switch that controls the entire power strip. It was my plan to remove that switch and to build an ESP, a power supply and two relays at the spot I removed the switch from. Of course I wanted the ESP to be reachable, so that I could easily take it out for a firmware upgrade.
To get a pretty tiny power supply, I have taken one of those iPhone chargers that you find for less than a dollar on Aliexpresss. If you put some force on the pins, usually the whole front pops out. Once that’s out, you can just very easily slide the power supply electronics out of the casing. To gain a little extra space, I have desoldered the USB header and have replaced it with a LM1117 3.3v LDO voltage regulator, so that I’ll have both 5V and 3.3V coming out of the power supply. The 5V is necessary to power the relays, while the 3.3V is necessary for the ESP8266 chip. Because I needed to power two relays, I decided to solder two sets of wires on the 5V output pins. Those wires, and the two I needed for the ESP, resulted in a set of six wires leaving the power supply.
With some nice heat shrinks, the the power supply is ready to be hot-glued on the bottom of the power strip casing:
To switch 220V of the power strip, I have purchased two single-relay modules with optocoupler from eBay. I have bought the single relay modules, because I thought it would be easier to get them into the powerstrip than a 2 relay module. I would power the relay with the 5V originally coming from the power supply. The relay modules already switch if you set the data pin to a voltage much lower than 5V, which is perfect because that allows me to connect the data-pin straight to the GPIO0 port of the ESP8266. I attached the two relay modules to the sides inside the casing, while leaving some space for the ESP8266 in-between them.
The eventual power strip now looks like this:
You can still see the two relay modules underneath the ESP8266. The power supply is underneath those two relay modules, so you won’t be able to see it. The little white wire I soldered on the ESP8266, makes sure the chip is booted as soon as it gets power. The gap through which you can see the two relays, is reserved for an additional plate, containing a push button to toggle (override) the relay-status manually.
With that it’s getting time to load the firmware. The firmware is actually a slightly modified fork of Tuan PM’s great MQTT firmware. I’ve modified it such that it only subscribed to one topic, and listens for an on of off command. Once it receives a command, it will switch the GPIO0 pin accordingly. Please find the firmware here.
Here’s a little video of the power-strip in action:
Total cost of the improved power strip is less than €10, including the power strip itself.
Following up on the request of Timo, please find the schematics below.
MQTT Wifi Thermometer
A second project is an MQTT Wifi Thermometer. This would allow me to measure the temperature, and when the temperature is exceeding a certain threshold, the fan can be switch on automatically.
To be able to plug and forget the thermometer, I have made it based on something that plug into the wall socket. In my scrap box, I still found an old charger for a Sony Ericsson phone. It says it supplies a voltage of 5V and 500mA, which would be enough to power the ESP8266 chip and an additional DHT22 thermometer. What I needed to get rid of, is the wired coming from the charger. In turn the output DC power should be connected to a custom circuit board. The circuit board would contain:
- A LM1117 voltage regulator, to get 3.3V to power the ESP8266 chip
- A 10K pull-up resistor between the Vcc and Data-out of the DHT22.
- The DHT22 thermometer
The DHT22 is soldered on the circuit board, but its casing is not directly resting on the circuit board. This allowed me to close the charger’s casing with the DHT22 outside of the casing and the circuit board inside of it. And with that, everything is firmly kept in place as well. An additional drop of hot glue secured the DHT22 to the casing.
The ESP8266 is mechanically kept in place at the outside of the charger, similar to the power strip. To keep it in place, I made an opening in the charger’s case that’s just wide enough to fit the ESP8266’s header. By having the ESP8266 on top of the casing, it will be easy to take the ESP8266 out of the charger to upgrade its firmware.
The end-result looks like this:
An interesting feature of the charger was a LED that indicates whether the charger is still charging the phone. Once the phone doesn’t take much power anymore, the LED switches off. This same LED now blinks up when its new guts take a relatively large amount of power. This happens when the ESP8266 is taking a temperature recording, or when it is communicating through WiFi.
Please have a look at the video below to see this in action. The blue LED on the ESP8266 blinks when it is communicating over WiFi. When it does, you’ll see the “charging” LED light up at the same time as well.
Please find the firmware for the MQTT Wifi Thermometer here.
Total cost of the thermometer is less than €7, excluding the charger. One of the more expensive components is the DHT-22, which could easily be replaced by a cheaper sensor.