ESP8266 MQTT Success

3v3In a previous blog I was starting to get a little depressed having discovered MQTT and having figured out how to get a light to turn on and off using MQTT and Openhab, I was gutted to find it would not work when I put everything together. By everything I mean a power supply, an ESP-01 and a solid state relay. No matter what I did it would not work – I received lots of helpful advise from readers and putting everything together solved the problem.

The original idea was to use a cheap Chinese 3v3 supply – feeding the ESP8266. That in turn would feed a solid state relay.

2 mistakes:  (1) the power supply was CRAP – electrical noise, dangerous mains connection – so much noise it would not let the board work. I tried going back to the bench supply – sure enough I could talk to the board but that’s when I discovered the second error (2) – connecting the relay to ground and the output of the ESP-01 – NO.

final prototype boardThe two changes I made were as follows – reference the relay to 3v3 and the output – not ground and the output. As for power – on my second attempt I used a cheap Chinese 5v supply and used a 3v3 regulator to get rid of the noise. Works a TREAT!

So now I’m right at the beginning. I have the following setup:

Mosquitto (free – runs on a variety of platforms) on a reliable NAS unit – acting as a message broker.

OpenHab (free – runs on a variety of platforms) running on a PC, soon to run on the same reliable NAS unit, processing rules and generally holding everything together.

ESP01 as an end controller – right now it turns a light on and off – soon  it will report on temperature and humidity – and control RGB LEDs (someone else will have to write that function).

lampAnd I’m testing with mqtt-spy which is wonderful and many thanks to the designer who has made many changes at my request. The long and short of it – this all WORKS, it works reliably and it’s a practical solution with SO many possibilities and for the first time also reasonably SECURE!

Did I make my own controller with lots of power taking months to develop with networked radios etc, does it work in 3 properties utterly reliably? Is it likely to go in the bin in the next few months? Yes to all of that.

The new power supply despite being cheap from China is a different thing ALTOGETHER to the first on – you can see just looking at it – slots in the board between the two power lines, a slot between the live side and the hopefully not  live side… way better bet and it’s able to give the better part of an amp at 5v.

powerThings to do – the ESP01 connects to a single access point – it is useless when you take it to another part of the building with a different access point – it needs to have a list of access points and passwords – go get a list of what’s available and pick the strongest of the signals that is in the list I provided.  Anyone up to writing that?

Also need more functions – switching a light on and off is hardly exiting – controlling RGB LEDS on the other hand IS.

I will follow this up with more detail but I’ve had enough for one day and I thought you might like to see that this is going somewhere.


21 thoughts on “ESP8266 MQTT Success

    • That is of course possible Jimmy – but it would cause havoc with my spouse and for some people it may not be at all practical. If I knew a little more about the ESP8266 it would be possible – and indeed it would be a breeze in Lua. I’m hoping someone has already figured that out.

    • Erm…. no… one could have a list of usernames and passwords in different properties. I’m using the Mosquitto with a public address (and usernames and passwords) even though the Mosquitto installation is in the building – so IP addresses don’t really come into it. I want to take finished versions of these to my place in Spain and be able to drop them in without recompiling or fiddling with the router…

      • Ah, I understood it as it was one location that you couldn’t have only one AP to give full wifi with. But you could have the exact same SSID, password and same (NAT’ed) ip-network on completely different locations, one in Spain and one at home.

    • Look at the pictures of the 6 power supplies – DO NOT buy the little 3v3 ones they are AWFUL – horribly noisy and the mains input looks downright dangerous. The TOP RIGHTMOST is the 5v 1AMP and it is a far nicer affair altogether – that’s the one detailed in my blog. Of course this is not for beginners – anything to do with the mains is not for beginners – but if you’re well familiar with working with mains supplies then to me at least this looks like a nice and CHEAP option – the kind of thing you might fit into a plug-in-the-wall module (in the UK, Maplins do some pretty awful looking black plug in the wall empty boxes).

    • Hi

      In a minute I’ll look at the code and extract the LED bit… erm you say you need a 100p cap to ground on GPIO0… that confuses me surely that is (only marginally) encouraging the GPIO pin to be low on powerup and hence flash. Would taking that from GPIO0 to +V make more sense? Lots of UART stuff in there- want to make Sunday easy and point me to the actual bit that sends a buffer load to the WS chips? I want to incorporate this into the MQTT code… for now not bothered about individual LED control just want to set an arbitrary length strip to colour x,y,z as fast as poss..

      • You are confusing names. cnlohr mentioned the 100p cap, I didn’t. Also, the code you are referring to is cnlohr’s original one. My LED stuff is already extracted and shown inline in the link to the post I sent you.

      • Sorry Markus – I did not see inline code. I’m not at home and I’m on a tiny laptop which doesn’t help… right I’ll look again for your code. More later becasue if I understand it – I’ll have it running by tonight. AH I see you didn’t extract it in here but in the ESP8266 community site – right – I’m looking at it now – cheers.

      • Markus

        It’ll be a little while before I get in front of my dev computer – can you save me a little struggling.. I’ll be adding this to a non-lua project…

        1. Do I just need the GPIO header and do I need to set GPIO0 as an output in my init code?

        2. The code looks straightforward but clearly there is some LUA assumption in the code below – could you kindly reply with a generic version of that array handling that does not reply on Lua libraries being present? Are we lookign at a continous character array with successive R,G,B byte values.. and is that “l” supposed to be there at the start of lgpio_ws2812. the L isn’t used in your examples.. sorry to be thick.

        See below..

        static int lgpio_ws2812( lua_State* L )
        size_t length;
        const char *buffer = luaL_checklstring(L, 1, &length);

      • ad 1. No, GPIO_OUTPUT_SET(GPIO_ID_PIN(WSGPIO), 0); already sets GPIO0 as output.

        ad2. Basucally you are right, the buffer should just contain the bytes. If you read the code carefully, you see that the order is G R B.

  1. Now before anyone says anything – if you replicate anything I’ve done here and blow up your PC or yourself – I am not responsible. I’ve had a lifetime in electronics (I’m 60 and learned to solder at 10) and tend to take some things for granted. Do not touch live wires, take meticulous care to ensure there are no shorts, stray wires, bits of solder under the board, cats – and do not fasten boards to your PC if they too are attached to the mains via a cheap Chinese power supply. Keep a fire extinguisher read 🙂

  2. Just to clarify it, did you connected the relay control + to 5v and the control – to GPOI?
    when GPOI high relay close, GPOI low relay open?
    If not, then how did you managed to control the relay with only 3.3V (relay control 4-6V)?

  3. The Solid State Relay works from around 3volts or so. The + is to the ESP8266 + and the negative is to the GPIO0 line. As it’s solid state there’s no need for diodes etc. Of course GPIO0 needs to be LOW for the relay to come on but that’s fine as it’s high on powerup…

  4. Pingback: codescaling | ESP8266 – little board, lotta Wi-Fi

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s