#include #include #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include #ifdef __AVR__ #include #endif /************************* NeoPixel ++++++++*********************************/ #define PIN 6 #define NUMPIXELS 24 /************************* WiFi Access Point *********************************/ #define WLAN_SSID "***" #define WLAN_PASS "***" /************************* MQTT Broker Setup *********************************/ const int MQTT_PORT = 1883; const char MQTT_SERVER[] PROGMEM = "192.168.*.*"; const char MQTT_CLIENTID[] PROGMEM = "NeoPixel Lamp1"; const char MQTT_USERNAME[] PROGMEM = ""; const char MQTT_PASSWORD[] PROGMEM = ""; // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient client; Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_PORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); /****************************** Feeds ***************************************/ const char NEOPIXEL_FEED[] PROGMEM = "test/rainbow"; Adafruit_MQTT_Subscribe neopixel_feed = Adafruit_MQTT_Subscribe(&mqtt, NEOPIXEL_FEED); Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); int delayval = 10; void setup() { Serial.begin(115200); delay(10); Serial.print("Setup: "); strip.begin(); // This initializes the NeoPixel library. Serial.print("Pixles Done"); Serial.println("OK."); // Connect to WiFi access point. Serial.println(); Serial.print("Connecting to "); Serial.println(WLAN_SSID); WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); mqtt.subscribe(&neopixel_feed); resetPixels(); } void loop() { MQTT_connect(); String pixelString = "0,0,0"; Adafruit_MQTT_Subscribe *subscription; while (subscription = mqtt.readSubscription(100)) { if (subscription == &neopixel_feed) { Serial.print("Got: "); pixelString = (char *)neopixel_feed.lastread; Serial.println(pixelString); int firstCommaIndex = pixelString.indexOf(','); int secondCommaIndex = pixelString.indexOf(',', firstCommaIndex + 1); String r = pixelString.substring(0, firstCommaIndex); String g = pixelString.substring(firstCommaIndex + 1, secondCommaIndex); String b = pixelString.substring(secondCommaIndex + 1); int red = r.toInt(); int green = g.toInt(); int blue = b.toInt(); for (int pixel = 0; pixel < NUMPIXELS; pixel++) { strip.setPixelColor(pixel, strip.Color(red, green, blue)); strip.show(); } } } delay(delayval); } void resetPixels() { for (int i = 0; i < NUMPIXELS; i++) { strip.setPixelColor(i, strip.Color(0, 0, 0)); strip.show(); } } void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected switch (ret) { case 1: Serial.println("Wrong protocol"); break; case 2: Serial.println("ID rejected"); break; case 3: Serial.println("Server unavailable"); break; case 4: Serial.println("Bad user/password"); break; case 5: Serial.println("Not authenticated"); break; case 6: Serial.println("Failed to subscribe"); break; default: Serial.print("Couldn't connect to server, code: "); Serial.println(ret); break; } Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds } Serial.println("MQTT Connected!"); }