// Internet Mood Light as found on projects.xief.net // Code for the ESP8266 that reads a json file from the internet and displays the colors on a Neopixel ring. // //lots of inspiration from: http://blog.nyl.io/esp8266-led-arduino/ #include #include #include #include //for the modolo function fmod // light ring setup #define NUMPIXELS 16 #define PIN 13 #define ROTSPEED 150 //delay in ms for each pixel unsigned long wheelMillis,currMillis = 0; //wheel spin timers Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); #define NUMLAG 7 //has to be the number in lag byte lag[NUMLAG] = {1, 255, 50, 30, 10, 5, 2}; byte colors[NUMPIXELS+1][3] = {}; //16 pixels, 3 colors red,green,blue struct color { byte R; byte G; byte B; }; // wifi setup const char* ssid = "YOUR SSID"; const char* password = "YOUR PASSWORD"; const char* host = "YOUR DOMAIN"; // Your domain String path = "/julia.json"; //initialization of variables #define UPDATETIMEDELTA 10000 // time in ms between checks if new color online long nextUpdate = 0; // time of last update #define FADEOUTTIME 300 //make one level darker each X ms long lastFadeOut = 0; void setup() { Serial.begin(115200); pixels.begin(); // This initializes the NeoPixel library. delay(100); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); int wifi_ctr = 0; while (WiFi.status() != WL_CONNECTED) { delay(100); //Serial.print("."); } Serial.println("WiFi connected"); Serial.println("IP address: " + WiFi.localIP()); } byte p = 0; struct color RGB, RGBOriginal; //saves the current color void loop() { if (nextUpdate < millis()) { nextUpdate = millis() + UPDATETIMEDELTA; //Serial.print("connecting to "); //Serial.println(host); WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { //Serial.println("connection failed"); return; } client.print(String("GET ") + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: keep-alive\r\n\r\n"); delay(300); // wait for server to respond // read response String section="header"; while(client.available()){ String line = client.readStringUntil('\r'); // Serial.print(line); // we’ll parse the HTML body here if (section=="header") { // headers.. //Serial.print("."); if (line=="\n") { // skips the empty space at the beginning section="json"; } } else if (section=="json") { // print the good stuff section="ignore"; String result = line.substring(1); // Parse JSON int size = result.length() + 1; char json[size]; result.toCharArray(json, size); StaticJsonBuffer<200> jsonBuffer; JsonObject& json_parsed = jsonBuffer.parseObject(json); if (!json_parsed.success()) { //Serial.println("parseObject() failed"); return; } //Serial.println("Parsing success"); // Make the decision to turn off or on the LED String red = json_parsed["red"]; String green = json_parsed["green"]; String blue = json_parsed["blue"]; // byte r = red.toInt(); byte g = green.toInt(); byte b = blue.toInt(); if (r != RGBOriginal.R || g != RGBOriginal.G || b != RGBOriginal.B) { // color changed, so feed the new one in the system RGBOriginal.R = r; RGBOriginal.G = g; RGBOriginal.B = b; RGB = RGBOriginal; // set current value to new received value // Serial.println("COLORS CHANGED"); // Serial.print("red is "); // Serial.println(RGB.R); // Serial.print("green is "); // Serial.println(RGB.G); // Serial.print("blue is "); // Serial.println(RGB.B); // Serial.println("End colors"); } } } client.stop(); // Serial.print("closing connection. "); } // faster update loop -> led ring update currMillis = millis(); if (currMillis > wheelMillis) { Serial.print("pix: "); Serial.println(p); colors[p][1] = RGB.R; // update current pixel with new color colors[p][2] = RGB.G; colors[p][3] = RGB.B; for (byte i=0;i=NUMPIXELS) p = 0; //iterate over pixels wheelMillis = currMillis + ROTSPEED; } if (lastFadeOut + FADEOUTTIME < millis()) { // Serial.print("Reducing Brightness, Current RBG: "); // Serial.print(RGB.R); // Serial.print(", "); // Serial.print(RGB.G); // Serial.print(", "); // Serial.print(RGB.B); lastFadeOut = millis(); if (RGB.R > 0) { RGB.R = RGB.R - 1;} //check for overflow, otherwise reduce by one if (RGB.G > 0) { RGB.G = RGB.G - 1;} //check for overflow, otherwise reduce by one if (RGB.B > 0) { RGB.B = RGB.B - 1;} //check for overflow, otherwise reduce by one // Serial.print("Reducing Brightness, New RBG: "); // Serial.print(RGB.R); // Serial.print(", "); // Serial.print(RGB.G); // Serial.print(", "); // Serial.print(RGB.B); } }