Created
November 28, 2018 03:49
-
-
Save buzztiaan/2bcaae5def5f34ad1302204e8993aa9b to your computer and use it in GitHub Desktop.
Arduino ESP8266 Cheerlights animating implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cheerlights attempt using that mqtt service somewhere | |
// buZz NURDspace | |
// nov 2018 | |
// rebuilt from adafruits' mqtt_esp8266_callback example | |
#include <ESP8266WiFi.h> | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
#include "Adafruit_NeoPixel.h" | |
#define WLAN_SSID "xxx" | |
#define WLAN_PASS "xxx" | |
#define MQTT_SERVER "iot.eclipse.org" | |
#define MQTT_SERVERPORT 1883 | |
#define MQTT_USERNAME "" | |
#define MQTT_KEY "" | |
#define LED_PIN D2 | |
#define LED_COUNT 16 | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); | |
WiFiClient client; | |
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_USERNAME, MQTT_KEY); | |
Adafruit_MQTT_Subscribe cheerlightsfeed = Adafruit_MQTT_Subscribe(&mqtt, "cheerlightsRGB"); | |
// store the current new color | |
int newR; | |
int newG; | |
int newB; | |
void cheerlightscallback(char *data, uint16_t len) { | |
Serial.print("Hey we're in a cheerlights callback, the current colour is: "); | |
Serial.println(data); | |
// split up the color data | |
char colorvalue[7]; | |
memcpy(colorvalue, &data[1], 6); long int rgb=strtol(colorvalue, NULL, 16); | |
newR = (rgb & 0xff0000) >> 16; | |
newG = (rgb & 0x00ff00) >> 8; | |
newB = (rgb & 0x0000ff); | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
// Connect to WiFi access point. | |
Serial.println(); Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.print(WLAN_SSID); | |
Serial.print(" : "); | |
WiFi.begin(WLAN_SSID, WLAN_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.print("WiFi connected. "); | |
Serial.print("IP address: "); Serial.println(WiFi.localIP()); | |
cheerlightsfeed.setCallback(cheerlightscallback); | |
mqtt.subscribe(&cheerlightsfeed); | |
pixels.begin(); | |
} | |
void loop() { | |
MQTT_connect(); | |
// check for incoming packages for 5 seconds | |
mqtt.processPackets(5000); | |
// update the ledstrip | |
updateledstrip(); | |
pixels.show(); | |
// check if you still have a mqtt response | |
if(! mqtt.ping()) { | |
mqtt.disconnect(); | |
} | |
} | |
void updateledstrip() { | |
int targetled; | |
int done = 0; | |
int tries = 0; | |
uint32_t newcolor; | |
newcolor = newR << 16 | newG << 8 | newB; | |
while ((!done) && (tries <9)) { | |
tries++; | |
targetled = random(LED_COUNT); | |
uint32_t thatcolor; | |
thatcolor = pixels.getPixelColor(targetled); | |
if (thatcolor != newcolor) { | |
done = 1; | |
pixels.setPixelColor(targetled, pixels.Color(newR,newG,newB)); | |
} | |
} | |
} | |
void MQTT_connect() { | |
int8_t ret; | |
// Stop if already connected. | |
if (mqtt.connected()) { | |
return; | |
} | |
Serial.print("Connecting to MQTT... "); | |
uint8_t retries = 3; | |
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected | |
Serial.println(mqtt.connectErrorString(ret)); | |
Serial.println("Retrying MQTT connection in 10 seconds..."); | |
mqtt.disconnect(); | |
delay(10000); // wait 10 seconds | |
retries--; | |
if (retries == 0) { | |
// basically die and wait for WDT to reset me | |
while (1); | |
} | |
} | |
Serial.println("MQTT Connected!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment