Esp Neopixel update – now with MQTT


Written by:

Exciting updates to my ESP-Neopixel-Webserver it’s not a webserver anymore I installed a MQTT service broker on one of my Raspberry PIs connect it to the network and changed the cod on the ESP8266 to exchange MQTT messages with the service broker let’s first have a look at the code ok. some of it changed. some of it stayed the same. include the library that is called PubSubClient You can find it when you search for it in the library manager search for: “pubsub” and you have the PubSubClient by Nick O’Leary so we create 2 subscriptions and the nice thing is: you don’t have to register it on the broker you just have to name it I called them “esp/1/out” for the outgoing messages and “esp/1/in” for all the incoming messages here I enter the SSID and password of my router ok. not really my credentials but I think you know what goes in there and.. the IP address of the MQTT server it’s the IP address the DNS server of the router gave to the Raspberry Pi MQTT broker we create an instance of the PubSubClient and most of it stays the same we configure the NeoPixels we configure the WiFi this time me made an extra function for it and try to connect to the MQTT server and we set the function that is called when the ESP receives a message from the server and here it is the debug messages since we want to set one color for all the NeoPixels we check the payload and create a String out of the payload and we use our known function “setNeoColor” to lighten up the NeoPixels in the loop-function we check if our PubSubClient is still connected if not… we call a function, we created to reconnect since we not only receiving messages but also sending messages we send every ten minutes or if the message changed and publish the message to the server all quite simple it only looks a bit complicated because of many, many debug output let’s see that in action we’re using the Android app MQTT Dashboard and.. and we create a new connection ClientID: let’s say “Home” the server is the IP address of the MQTT broker port is the default port the default port is 1883 and we didn’t set a username and password on the broker so, we can try to connect yeah. we are connected to the service broker now we click on publish create a colorpicker aaaand… ESPNeoPixel topic. the topic is the subscription we set it to “esp/1/in” and we send a hexadecimal value in the colorpicker we subscribe to the topic because we set the ESP to resend the selected color to the broker EspNeopixelColor the topic was “esp/1/out” so, let’s try to select a color let’s pick blue and it’s blue in our subscription we see the color that is selected let’s select another color red and our subscription changed let’s try again green. we don’t had green whoo nice and a darker blue and – of course – if we want to turn it of, we select black. Booja! if you want to create your own smart home.. ..the MQTT protocol is a nice feature it’s easy to configure and the broker software even runs on a Raspberry Pi and even those tiny little ESPs can handle these protocols if you own only one ESP and one NeoPixel it’s probably better and easier if you use the webserver code but if you don’t think it should be the end if you want to attach sensors and many more ESPs around your home and you don’t think it should stop there I think MQTT is a good start to automate your home to make your home smarter I leave a link to the modified code in the description if you like the video.. comment, like, subscribe.. .. and share it Bye!

Leave a Reply

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