ESP8266 dual relay via mqtt

thank you in advance for any help.

currently running oh2, couple week old snapshot. have mqtt server running on a rpi2. i have some esp-01 modules from a different project laying around, and a 2ch relay board.

my intention is to replace my projection screen control currently using a wifi relay board with the tcp binding. i have had reliability issues with tcp, and have seen that mqtt is very reliable reading through the posts here. i tend to make even the simpliest projects more difficult on myself, probably from the lack of understanding, but then again I’m not the tupe of person that starts from rhe beginning of the book, but skips to the chapters i need…

im going to tag @rlkoshak because i know he uses mqtt a lot.

what i am in need of is an explanation or tutorial of how to build my esp and relay board and how to make it work. i have never used mqtt before. i search the great almighty internet, but as we all know, there is a plethora of information, none that is specifically for oh2 that ive found. if someone can lead me in the direction of a good tutorial, a concise one at that, i would be very greatful.

This mqtt tutorial is quite good, and helped me a lot.


I use an esp8266 with a solid state relay to control a lamp. I set up an mDNS host name for the device and run a simple web server that accepts ON and OFF commands and returns the current status when queried. I then use the openHAB http binding to send commands and monitor the status of the lamp switch. For this use case, I think it’s a simpler approach than installing and configuring a MQTT server (although I do have an MQTT server to support periodic sensor reporting for other devices).

@steve1’s approach is a good one.

If you do go MQTT that isn’t a bad approach either. Personally, once I set up MQTT it became my default way to get things to communicate. Setup was simple, communications are secure, and I love the Last-Will-and-Testament feature where the broker will publish a message to a defined topic when a client goes offline.

Setup is pretty trivial really.

  1. Install and configure a broker. People around here like Mosquitto and that is what I use. Don’t worry about TLS or ACLs right now. Just configure with a username and password per the instructions on Mosquitto’s website.

  2. Install and configure the MQTT binding. You might also be interested in the MQTT Action. It is all documented on the wiki.

  3. I’ve no experience with MQTT on the boards but know there are libraries to install and use.

ok, thanks for that all…anyone have a good tutorial for actually bulding the devive and how to program it?

Hi *,

@Branden, what do you need exactly?
A tutorial for the OpenHab2 MQTT binding or a Arduino sketch for a small ESP8622?

have fun!


Hi Branden,

I myself use mqtt to communicate to relay’s and to get information from sensors. I used one tutorial to learn more about mqtt, and one website to learn about Arduino’s and sensors:

  1. Tutorial: makeuseof. Go to the section that starts with MQTT for OpenHAB and Internet of Things. Make sure you pay attention to the current versions of openHAB you use, and what the tutorial describes. At least it gives you a basic idea of how things could work.

  2. Read around a bit to get more info on mqtt, arduinos, actuators etc.

You will have to do some reading and putting together bits and pieces to make it all work for your use-case.
Good luck and post back your results so others can learn from it!

the hardware part, and sketch. like i said, i have an esp01, and a relay board, but dont know how to connect them and program to work.

on anorher note…there is a lot of reading, i do realize, but giving someone, i.e. me, a direct solution i believe is more helpful than just giving a reading example, not how i learn…hands on type guy…i myself have done this for people, as i inderstand that reading how to do somethinh isnt always the best learning tool for everyone…

just a note for those that DO learn by reading…

for the ESP01 you need a FDTI board.
bear in mind the ESP01 is only for 3.3 Volt

simply connect


3.3 V -> Pin 8
GND -> Pin 1
RX -> TX ( Pin 2 )
TX -> RX( Pin 7
3.3 V -> CHPD ( Pin4 )
GND -> GPIO0 ( Pin 5)

now you can upload the sketches

like they are in the examples

Since it’s not likely any of us are going to come to your home to show you how do it, aren’t you going to be learning by reading (this topic or the net) anyway and trying it yourself?

1 Like

well, yes… but if anyone has sites to visit with pictures…that is what what meant.

for the Pinout of the ESP01


sorry, did i miss examples link?

in your Arduino IDE
File -> Samples ->

ok, thx.

If I remember correctly (it’s been a while), I started with some information from this page.

This is for a solid state relay. The lamp I modified was a low voltage LED amp so I used a 3v3 voltage regulator off the lamp power supply instead of using a dedicated power supply.

I used a solid state relay because I didn’t want to hear the click of a mechanical relay. However, you should be able to find many similar pages and diagrams for ESP-01s controlling mechnical relays.

apparently I’ve ruffled some feathers here, so 1) id like to apologize if i did, and 2) clarify why i made that comment.

ive been with this community for over a year now, and usually people are very helpful. i am a member of many other diy communities as well, and on many of those, trying to get an answer to what seems a simple question is like pulling teeth…either the answer is technical, not layman, or im told to read a bunch of stuff and try to extract what i need…similar to my teacher in 3rd grade…ask how to spell a word, get told to look in the dictionary…works for some words, but definitely not for others…

so, essentially, i apologize for ruffling feathers, was not my intention…

thanks steve

It is the “give a man a fish … teach a man to fish” story. For a lot of things if we just give the answer, 30 minutes later you will be back with another somewhat related question and so on and so on. By telling you where/how to find the answer on your own you are much more likely to become self sustaining (i.e. you can answer questions like these on your own).

Then you have those people who simply will not take the effort to try and figure these things out for themselves. This becomes an exercise in futility. DIY electronics and coding is hard enough already when you are sitting in front of the computer. Try doing it third hand and hundreds to thousands of miles from the actual devices. I’ve spent hundreds of hours on this very forum trying to help people who approached this stuff with the “just tell me the answer” attitude. It doesn’t work. It just makes the asker frustrated and me angry.

You’ve been around for a good deal of time. I know you are more than willing to put in the effort to figure these things out. I’m just trying to explain why you are seeing some pushback. We don’t want to tell you how to spell the word because then we will be asked again when you encounter another word you can’t spell. It saves everyone’s time to teach you how to figure it out yourself.