Skip to content

Linking the ESP8266 to a Raspberry Pi through MQTT

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:

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.

5V-EU-Plug-AC-Travel-USB-Wall-font-b-Charger-b-font-for-font-b-iPhone[1]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:

power-supply

1-Channel-5V-Relay-Module-With-Optocoupler-For-PIC-ARM-AVR-DSP-SRD-05VDC-SL-C[1]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.

Guts

The eventual power strip now looks like this:

power-strip

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.

Power strip schematics

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:

Thermometer

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. 

Published inTechnoblog

24 Comments

  1. awesome project! but do be careful with high voltages. if you have exposed connections, you will need to have isolated power supplies. best way to test is to plug the plug the wrong way into the power socket and check with a test pen that the exposed ground is not live.

    • Thanks for your kind comments John! I love high voltages, they give a little bit of spark to it. Ehm.. Well.. In a good way mostly ?

      You’re right, although I’m not thoroughly seasoned with high voltage, I am taking some precautions not to electrocute myself (or others). The power supply of the power-strip is a nice example. As you can see in the pictures, there’s plenty of heat shrink on the wires, but the power supply itself is also heat shrinked, so that there’s plenty of insulation between the power supply and the other components.

      The test of plugging the plug in the wrong way is something that I have to anticipate. Plugs in The Netherlands have a horizontally, but also vertically symetrical design. This means that the wrong way, is actually a right was as well. But even if there was a right and wrong way, I think I would have designed it exactly the same way as I did. I don’t want to be dependent on a potential color-blindness or left/right-skills of the installer of my wall-sockets. In my design I have two relays, both Live and Neutral are being switched. This is just to be sure that there’s really no power on it anymore after computer says No.

      Thanks for your comments John. Appreciated!

  2. Jake Jake

    Hello.
    Is it possible to control multiple esp’s with the raspberry pi?/

    • Definitely! You could get the various esp to have their own topic, so that you can control them individually. Or you could get them to sign up to a generic topic, to control them all at the same time. Or subscribe to both a generic as well as a esp-specific topic at the same time… Endless possibilities! 🙂

  3. Doug Doug

    Does not work due to errors in mosquitto-repo.list
    [email protected] /etc/apt/sources.list.d $ sudo apt-get update
    E: Type ‘<?xml' is not known on line 1 in source list /etc/apt/sources.list.d/mosquitto-repo.list
    E: The list of sources could not be read.

    • You’re right Doug. It seems that the good people maintaining Mosquitto have made a second repository available for the new release of Debian. As Raspbian still uses wheezy, you’ll need to link the Wheezy repo in though, using:

      sudo curl -O http://repo.mosquitto.org/debian/mosquitto-wheezy.list

      And in your case, you’ll have to remove mosquitto.list of course.

      I’ve changed the blog accordingly. Thanks for letting me know Doug!

  4. Ashish Minocha Ashish Minocha

    Jan, firstly apologies for my ignorance, i am a beginner. The firmware given in the Git repository – how do we upload it to ESP?

  5. Lou Lou

    Hi Jan,

    I managed to install esp-open-sdk and compiled and flashed your project but nothing happens (it doesnt post any msg to my mosquito server installed on a rpi). Please can you give me some hints about how should I debug it?
    I use ESP8266 Sparkfun Thing (https://www.sparkfun.com/products/13231) for testing this.
    Thank you for your time and your great work!

    • Thanks Don! I have made the necessary change.

      Best regards,
      Jan Penninkhof

  6. Timo Timo

    What is that brown jumper wire for in ESP8266 ?
    Have you drawing for connections?

    • Hi Timo, the brown jumper connects 3.3v to the CH_PD (enable/power down) pin. This pin must be kept high (hence the +3.3v) for normal operation. I have added the schematics of the power strip to the blog.

  7. Thanks for the wonderful project. I am trying to get my arms around the JSON thing in the code. There is not much information on the net on JSON lib on ESP8266. The API don’t tell you how to use them and the JSON_xxxx preprocessor macros help to confuse one more than necessary. Would be kind enough to explain how to use the JSON lib? Ablog on it maybe?

    • Thanks for the nice words Ravi. Understanding the JSON library also has cost me quite a considerable amount of time, exactly because of the reasons you already stated. That’s why I was happy to include a pretty concise example in the source code of this project and hoped it would help other developers. It might indeed be good to document my conclusions in a separate blog. Will do that soon (not this week though). Cheers!

    • Hi Emil,

      Thanks for the nice words. Happy that you liked the blog post.

      As for turning Chinese iPhone chargers into power supplies; I have heard the story before, but I find it difficult to guess whether it is really that bad. I’m not so worried about the dirty output, as I always have an additional power regulator (AMS1117) pulling the voltage down to a clean 3.3V.

      Also, I feel the unsafety of these chargers may be a bit exagerated as long as you don’t ask too much of them (and an ESP is definitely not asking too much). I have held the internals of these chargers in my hand several times and everytime I had one in my hand, I couldn’t resist having a look at build quality. I don’t think it’s really that bad. Isolation is taken care of using a transformer and they’ve even separated the high voltage at one side of the board from the low-voltage parts on the other side of the board. Also, if they were really that bad, wouldn’t we hear a lot more about those “Chinese charger blew up in kid’s face” stories? Besides, I hear those stories about so-called genuine chargers as well.

      Anyhow, enough ranting. As my time is limited, and I’ve found it a bit tedious to go through the hassle of carefully disassembling those chargers, I have chosen another strategy to power ESPs. These days, I tend to use the Hi-Link HLK-PM01 for my projects. It’s a 3W power supply, but since it’s Chinese, probably good for 1~1.5W. Looks properly isolated and is also still quite affordable at a price range of 2˜2.5 euros. As it’s a 5V power supply, it still needs the AMS1117 regulator, but I’m always happy to add it, as it not only makes sure you have to right voltage, but also cleans up the signal from noise and fluctuations.

      Cheers,
      Jan

  8. Jan,

    Great work here. While the tutorial is excellent (and will save me a lot of time- I was planning on using raw tcp sockets before reading this), I am most impressed with your hardware management. Very clean, very stable, very usable. Great work.

    I thought I’d share some of my research with you, as it might enable you to do bigger and better things, which you will hopefully blog about 🙂

    1: There is an HLK-PM03, which outputs directly to 3.3v, and would eliminate the need for a step down converter. This would remove a step, as well as reducing your heat waste. Of course, if you need 5v for something else, it doesn’t make much sense to use this. Those relays, from what I understand, still actuate at 3.3v though, as I have read from multiple sources (and my brother has confirmed from personal experience, though I do not yet have them in my possession to test them myself), so you may be able to get rid of the 5v lines altogether.

    2: Your work with the ESP-01 is excellent, but you may find the ESP-12F to be more suitable for more complex projects. It has significantly more GPIO pins, including, most notably, an ADC pin, which allows for analog input. I find them to cost only slightly more than the ESP-01, and the gains are significant.

    3: In the document below I have some solid state relays listed which supposedly do not click when actuated. I haven’t tested for longevity or anything, but if they last, it would be much nicer than having your lamps click every time you turn them on, I think. The only trade-off is that they are limited to 2a, which in my usage is not even close to a problem, and they do cost a bit more than the clacky variety.

    I’ve documented prices, links, and model numbers in a google doc here: https://docs.google.com/spreadsheets/d/1-Cfhdzzg6_0ASVzCWTT20VJOd8HgSkCY5eHrvfnnc10/edit?usp=sharing

    It isn’t exactly pretty, but it has all the details on the cheapest components I could find. Note that AliExpress starts charging shipping costs (some of which are exorbitant) after the total cost of a single product exceeds around $7- it behooves you to keep an eye on the shipping costs and order separately to avoid this. If you haven’t used AliExpress before, prepare for 3-4 weeks of shipping if you live in the US- not sure about Eurasia or Oceania. It’s worth the wait, though, if you are ordering an entire house’s worth of sensors, or if you are like me and have a queue of projects flowing into your mailbox already 🙂

    I am able to put together a hall sensor for less than $5.50, and the most expensive sensor I’ll be using (which has two laser emitters and three digital photo sensors for directional movement and daylight detection) will cost less than $10. Pretty remarkable.

    Hope this is useful information for you. I would love to see more of your work- as I said before, it is excellent. Happy tinkering.

  9. Sanjeev Sharma Sanjeev Sharma

    Hello,

    Do you know how to achieve this through command line .I mean to say ESP8266 and RASPberry pi want to communicate to each other through MQTT.ESP8266 will act as a MQTT Client and raspberry will work as MQTT Broker. they both are in same network.

    I would like to know how to configure RASPberry pi so it act as a Broker.

    • Hi Sanjeev, I’ve used Mosquitto as my MQTT broker. If you follow the lines right under section “Installation of Mosquitto on the Raspberry Pi”, you’ll have a running MQTT broker on your pi. Good luck!

  10. Alex Alex

    Hello,
    I am new to the ESP8266 and Raspberry Pi. Do you know how to connect and setup the ESP8266 to the Raspberry Pi? My Raspberry Pi is collecting the data and then send the data to my PC through ESP8266.
    I would like to know. Thank you.

  11. Mika Mika

    Hi! great work I’m planning on doing the same when i receive all the necessary parts!
    But one question, is there a specific reason you use TWO relays? one for mains and one for neutral, couldnt you just use a single relay on mains or is this a safety reason im neglecting here?

    Thanks

  12. Bhushan Bhushan

    I liked this project soo much and I would like to know more about controlling the relay using push buttons so that the updated status could be seen on the server side through JSON format. So that it can be operated both manually as well as through the internet. Kindly provide me more details about this. Thank you in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *

three × 4 =