For novice learners, programming the Neopixel RGB LED strip in the Arduino IDE can be rather daunting. Not anymore. Not only can we change the color easily, we can also control it wirelessly using REST API calls (web requests). Going a bit further, we can use other free web services to create some more IoT services linked to the LED strip e.g. as an alert indicator for weather forecast or notified when some event is triggered (e.g. social media pings, receive emails or calendar change).
1. Basic control of Neopixel LED stripUsing the Espert library in your Arduino sketches, you can call out the function to set the RGB LED strip to display any color that you want.
#include "ESPert.h"
ESPert espert;
void setup() {
espert.init();
espert.oled.init();
espert.neopixel.init();
}
void loop() {
/*
-- Predefine Color --
ColorRed 0xFF0000
ColorGreen 0x008000
ColorBlue 0x0000FF
ColorCyan 0x00FFFF
ColorPurple 0x800080
ColorGray 0x808080
ColorBrown 0xA52A2A
ColorSilver 0xC0C0C0
ColorViolet 0xEE82EE
ColorPink 0xFFC0CB
ColorGold 0xFFD700
ColorYellow 0xFFFF00
ColorWhite 0xFFFFFF
*/
espert.neopixel.rainbow();
delay(1000);
espert.neopixel.setColor(ColorRed);
delay(1000);
espert.neopixel.setColor(ColorGreen);
delay(1000);
espert.neopixel.setColor(ColorBlue);
delay(1000);
espert.neopixel.setPixelColor(0, ColorRed);
espert.neopixel.setPixelColor(1, ColorGreen);
espert.neopixel.setPixelColor(2, ColorBlue);
espert.neopixel.setPixelColor(3, ColorCyan);
espert.neopixel.setPixelColor(4, ColorPurple);
espert.neopixel.setPixelColor(5, ColorGray);
espert.neopixel.setPixelColor(6, ColorBrown);
espert.neopixel.setPixelColor(7, ColorGold);
espert.neopixel.show();
delay(1000);
espert.neopixel.off();
delay(1000);
}
espert.neopixel.setColor(ColorRed);
espert.neopixel.setColor(ColorGreen);
espert.neopixel.setColor(ColorBlue);
#include <ESPert.h>
ESPert espert;
const char* mqtt_server = "mqtt.espert.io";
const char* ssid = "xxxx";
const char* password = "xxxx";
//const int sleepTimeS = 600; // For deep sleep application: wakes up every 10 mins
String npxvalue = "clear";
unsigned long loopTime = 0;
String outTopic = "ESPert/" + String(espert.info.getChipId()) + "/neopixel";
String inTopic = "ESPert/" + String(espert.info.getChipId()) + "/neopixel";
void callback(char* topic, byte* payload, unsigned int length) {
String strPayload = String((char*)payload).substring(0, length);
espert.println("Receive: " + strPayload);
if (espert.json.init(strPayload)) {
if (espert.json.containsKey("color")) {
String value = espert.json.get("color");
espert.neopixel.init (12,20); //initialize pin 12 with 20 pixels
if (value == "Red" || value == "red") {
espert.neopixel.setColor(ColorRed);
delay(10000); //wait for 10 sec before turning off
espert.neopixel.off();
} else if (value == "Green" || value == "green") {
espert.neopixel.setColor(ColorGreen);
delay(10000);
espert.neopixel.off();
} else if (value == "Blue" || value == "blue") {
espert.neopixel.setColor(ColorBlue);
delay(10000);
espert.neopixel.off();
} else if (value == "Yellow" || value == "yellow") {
espert.neopixel.setColor(ColorYellow);
delay(10000);
espert.neopixel.off();
} else if (value == "Cyan" || value == "cyan") {
espert.neopixel.setColor(ColorCyan);
delay(10000);
espert.neopixel.off();
} else if (value == "White" || value == "white") {
espert.neopixel.setColor(ColorWhite);
delay(10000);
espert.neopixel.off();
} else if (value == "Pink" || value == "pink") {
espert.neopixel.setColor(ColorPink);
delay(10000);
espert.neopixel.off();
}
else {
espert.neopixel.off();
}
npxvalue = value;
}
if (millis() - loopTime >= 5000) {
loopTime = millis();
String outString = "{\"neopixel\":\"" + npxvalue + "\", ";
outString += "\"name\":\"" + String(espert.info.getId()) + "\"}";
espert.println("Send...: " + outString);
espert.mqtt.publish(outTopic, outString);
}
}
}
void setup() {
espert.init();
espert.mqtt.init(mqtt_server, 1883, callback);
espert.oled.init();
//to show that the neopixl led is initialized
espert.neopixel.init(12,20);
espert.neopixel.rainbow();
delay(1500);
espert.neopixel.off();
delay(2000);
espert.oled.clear();
espert.oled.println(espert.info.getId());
espert.oled.println();
int mode = espert.wifi.init();
WiFi.begin(ssid, password); //send WiFi credentials directly
if (mode == ESPERT_WIFI_MODE_CONNECT) {
espert.println(">>> WiFi mode: connected.");
espert.oled.println("WiFi: connected.");
espert.oled.print("IP..: ");
espert.oled.println(espert.wifi.getLocalIP());
} else if (mode == ESPERT_WIFI_MODE_DISCONNECT) {
espert.println(">>> WiFi mode: disconnected.");
espert.oled.println("WiFi: not connected.");
}
}
void loop() {
if (espert.mqtt.connect()) {
espert.println("MQTT: Connected");
espert.println("MQTT: Out Topic " + outTopic);
espert.mqtt.subscribe(inTopic);
Serial.println("MQTT: Subscribed " + inTopic);
}
delay(500);
//espert.oled.clear();
//ESP.deepSleep(sleepTimeS * 1000000); //For deep sleep application: goes to sleep zzz..
}
Essentially, the code above will connect the ESPresso Lite V2 board directly to the mqtt.espert.io cloud server. Using the MQTT Websocket Panel, you can monitor the message traffic being subscribed and published.
The Arduino sketch code is also available here.
3. Controlling device using MQTTInformation about connecting to the MQTT Websocket Panel is documented rather extensively over here. In this instance, take note that the topic subscribed takes the following format:
ESPert/xxxxx/neopixel
Where xxxxx is the unique board ID. The 'command' messages will be in JSON format:
{ "color" : yyyy }
Where yyyy denotes the basic colors e.g. "red", "white", "blue", "green" or "yellow".
In the example above, the message {"color": red} will cause the Neopixel LED to turn red (as shown below).
You can also make web requests i.e. REST API call to control the LED strips. The format of the request is shown below:
http://www.espert.io/bridge/ESPert/xxxxxx/neopixel?color=green
Where xxxxxx is the unique board ID of the ESPresso Lite V2. This of course will change the color of the LED strip to green.
Now with everything in place, IFTTT recipes like checking for weather forecast and then using it to trigger a web request can be created easily.
For example, you can create a condition e.g. rain to trigger the LED to turn e.g red. More information about setting up and creating IFTTT recipes are available here.
6. Possible IoT applicationsThis example can easily be used to create simple proof-of-concepts ideas that perhaps were works of science fiction previously.
For example, the weather forecasting umbrella (by Ambient Devices) is not the work of mischievous pranksters but concepts that are worth commercialising. You can consider building your own weather forecasting umbrella using the examples shown above.
Comments