10/10/2019

5 Steps to create Internet controlled LEDStrip using ESP8266 and MQTT

By snorlaxprime

I have been busy for a couple of weeks and didn’t get around to post. But, I know that shouldn’t be an excuse. So here are an upgrade to the previous post about WiFi controlled LEDStrip.

Using the same material as previously, if you have manage to create the WiFi controlled LEDStrip using the previous post, this will be an easy upgrade to allow the LEDStrip to be controlled via Internet. We are using the MQTT protocol from shiftr.io. One thing to note, for some reason I am not able to get the code to work if we are also using the OTA (on the air) update. So for this purpose, I have removed all the code related to OTA.

Step 1. Include the Library & Definition

We need to include the MQTT library, as shown in the following code snippets:

#include <MQTT.h> 

...
// MQTT info
 WiFiClient net;
 MQTTClient client;
const char* thehostname = "broker.shiftr.io";
 int port = 10031;
 const char* user = "try";
 const char* user_password = "try";

Please note that the code above is using the public test account, and it is not secure at all. You should use your own account to ensure that it is more secure. You can register the free account from http://shiftr.io

Step 2. Setup() section

We need to connect to the MQTT broker as defined in the previous step. So you will need to add the following code in the Setup section:

//MQTT code
   client.begin(thehostname, net);
   client.onMessage(messageReceived);
   connect();  // wait until connected to WiFi

The code above will connect the client to broker.shiftr.io and set the call back function to call the messageReceived function when a message is being posted to the topic that we are subscribed to. You will notice that I am calling the connect() function to connect to the WiFi and the MQTT broker.

Step 3. Subscribe to the topic

The subscription is done in the connect() function, here are the code:

void connect() {
   Serial.print("checking wifi…");
   while (WiFi.status() != WL_CONNECTED) {
     Serial.print(".");
     delay(1000);
   }
   Serial.println("\nconnected to:");
   Serial.println(ssid);
   Serial.print("IP address: ");
   Serial.println(WiFi.localIP());
 Serial.print("\nconnecting to MQTT…");
   while (!client.connect(device_name, user, user_password)) {
     Serial.print(".");
     delay(1000);
   }
 client.subscribe("/LEDStrip");
 }

As you can see the code will wait until we are connected to the WiFi and then display the IP address and then wait until we are connected to the MQTT broker. Once we are connected, it will then subscribed to the “/LEDStrip” topic. When there is a message being post the the “/LEDStrip” topic, the call back will happen.

Step 4. Setup the call back function

Here are the how the callback function looks like:

//MQTT callback
 void messageReceived(String &topic, String &payload) {
   Serial.println("incoming: " + topic + " - " + payload);
   if (topic == "/LEDStrip"){
     if (payload == "red") {createColor.red();}
     if (payload == "green") {createColor.green();}
     if (payload == "blue") {createColor.blue();}
     if (payload == "orange") {createColor.orange();}
     if (payload == "yellow") {createColor.yellow();}
     if (payload == "indigo") {createColor.indigo();}
     if (payload == "cyan") {createColor.cyan();}
     if (payload == "magenta") {createColor.magenta();}
     if (payload == "white") {createColor.white();}
     if (payload == "off") {createColor.off();}
     if (payload == "pink") {createColor.pink();}
     if (payload == "rainbow") {createColor.rainbow();}
   }
 }

As you can see, when a message is received in the /LEDStrip topic, this callback function will be called. For simplicity, I only check 12 different message, red, green, blue, orange, indigo, cyan, magenta, white, pink and rainbow. You can extend this to do more colours to your heart’s content.

Step 5. Post the message to the topic you are monitoring

If all goes well, then all you have to do is post the message to the correct topic. In this example, I am using the simple terminal curl command. You can do this easily if you are using a linux or Mac. For Windows user you might need to use javascript or other method supported by shiftr.io.

curl -X POST "http://try:try@broker.shiftr.io/LEDStrip" -d "red"

As you can see in the image above, I am posting various message to change the colours, you can see the results in the following youtube video:

And Voila, you now have an internet controlled LEDStrip that you can change colours to your heart’s content from anywhere in the world. Notify your kids when you are about to come home from work, they surely excited. Here is the link to the source code. Please subscribe if you like this post and drop me a line. And I will see you in the next IoT project.

Please note, that the code is not secure as it is using the public test account, you will need to get your own account so that it is more secure.