Skip to content

Instantly share code, notes, and snippets.

@willhooi willhooi/MQTT-Neopixel
Last active Aug 1, 2016

Embed
What would you like to do?
#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..
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.