3 Steps to control NeoPixel LEDStrip with ESP8266 using MQTT
This writeup is the continuation from the previous IoT LEDStrip post. We are using the same circuit as before in the IoT LEDStrip with Blynk.
The changes in this version, is I will show you how to control the LEDStrip without using Blynk. We are utilising MQTT as the medium to transmit the message to our Internet Controlled LEDStrip. This is more fun and more education for people who like the nitty gritty and being able to have full control and make changes on how the lights behave. As you can see I am having a lot of fun in the following video.
Step 1. Build the Circuit
So let’s get on going, you will need to build the circuit as per previous post on IoT LEDStrip. But for convenience I will show it again as below:
Step 2. Load the Code
You can download the IoT NeoPixel LEDStrip code and uploaded it to the ESP8266. The code connect the ESP8266 to the Internet via your WiFi, so you will need to enter the WiFi router and the password. To modify this change the following section of the code:
/* Network settings */ const char* ssid = "yourwifissid"; // SSID - your WiFi's name const char* password = "yourwifipassword"; // Password
It will then connect to the MQTT broker to listen to the command. But you will need to configure which MQTT broker to connect to. In this example I am using the broker.shiftr.io. You can go to their website to register the free account or use the test account that is provided. To change the MQTT broker password you will need to change the following lines:
const char* thehostname = "broker.shiftr.io"; int port = 10031; const char* user = "try"; const char* user_password = "try";
Also make sure that the PIXEL_COUNT variable contain the number of LEDs that you have in your LEDStrip. In my example, I have 30 LEDs.
Step 3. Send the message through
Once the source code is loaded successfully, there are 2 ways to control the LEDStrip, one is to send the message to the MQTT message broker. This can be done using the following command in your terminal:
curl -X POST "http://try:try@broker.shiftr.io/LEDStrip"d "press"
Every time you send the “press” message the led will light up based on the sequence of colours defined in the source code. This section of the source code is shown below:
// Check if state changed from high to low (button press). if((newState == LOW) && (oldState == HIGH)) { // Short delay to debounce button. delay(20); // Check if button is still low after debounce. if(newState == LOW) { // Yes, still low if(++mode > 8) mode = 0; // Advance to next mode, wrap around after #8 switch(mode) { // Start the new animation… case 0: colorWipe(strip.Color( 0, 0, 0), 50); // Black/off break; case 1: colorWipe(strip.Color(255, 0, 0), 50); // Red break; case 2: colorWipe(strip.Color( 0, 255, 0), 50); // Green break; case 3: colorWipe(strip.Color( 0, 0, 255), 50); // Blue break; case 4: theaterChase(strip.Color(127, 127, 127), 50); // White break; case 5: theaterChase(strip.Color(127, 0, 0), 50); // Red break; case 6: theaterChase(strip.Color( 0, 0, 127), 50); // Blue break; case 7: rainbow(10); break; case 8: theaterChaseRainbow(50); break; } } newState =HIGH; }
Alternative to the above, you can also point your favourite browsers to the webserver of the ESP8266. You can find the IP address from the log shown in the message log window. Once you pointed your browser to the IP address of the ESP8266 you will see the following page, which allow you to control the LEDStrip.
That’s all, I hope you have enjoyed this post and if you like this, please make sure you subscribed for more post about IoT. If you have any questions, don’t hesitate to post it below.