#include #include #include #include #include #include #define LED_PIN 0 #define COLOR_ORDER GRB #define CHIPSET WS2811 #define BRIGHTNESS 10 #ifndef STASSID #define STASSID "FNT-Gast" #define STAPSK "Laserjet09" #endif const char* ssid = STASSID; const char* password = STAPSK; // networking and json String apiUrl; int status = WL_IDLE_STATUS; ESP8266WiFiMulti WiFiMulti; WiFiClient client; HTTPClient http; unsigned long millisTime; boolean firstIteration = true; void connectWiFi() { // We start by connecting to a WiFi network WiFi.mode(WIFI_STA); WiFiMulti.addAP(ssid, password); Serial.println(); Serial.println(); Serial.print("Wait for WiFi... "); while (WiFiMulti.run() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); delay(500); } String buildAPIUrl (double lat, double lon, const String& apiKey) { const String latStr(lat, 4); const String lonStr(lon, 4); return String("http://api.openweathermap.org/data/2.5/weather?lat=" + latStr + "&lon=" + lonStr + "&appid=" + apiKey); } // Params for width and height const uint8_t kMatrixWidth = 16; const uint8_t kMatrixHeight = 16; uint16_t XY( uint8_t x, uint8_t y) { uint16_t i; if ( x & 0x01) { i = kMatrixHeight * (kMatrixWidth - (x + 1)) + y; } else { i = kMatrixHeight * (kMatrixWidth - x) - (y + 1); } return i; } #define NUM_LEDS (kMatrixWidth * kMatrixHeight) CRGB leds_plus_safety_pixel[ NUM_LEDS + 1]; CRGB* const leds( leds_plus_safety_pixel + 1); uint16_t XYsafe( uint8_t x, uint8_t y) { if ( x >= kMatrixWidth) return -1; if ( y >= kMatrixHeight) return -1; return XY(x, y); } uint8_t sun_yx[16][16] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 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{0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, {0,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0}, {0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0}, {0,1,0,0,1,1,1,0,0,0,0,0,1,0,0,0}, {0,1,0,1,0,0,0,1,0,0,0,0,1,0,0,0}, {0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0}, {0,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0}, {0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0}, {0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} }; void DrawFrame(uint8_t arr[][16], String identifier) { FastLED.clear(); FastLED.show(); for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { uint8_t elem = arr[i][j]; if (elem == 1) { if (identifier == "Rain") { if (i >= 12) { leds[ XYsafe(i, j)] = CRGB::Blue; } else { leds[ XYsafe(i, j)] = CRGB::WhiteSmoke; } } else if (identifier == "Clouds") { leds[ XYsafe(i, j)] = CRGB::WhiteSmoke; } else if (identifier == "Sun") { leds[ XYsafe(i, j)] = CRGB::Yellow; } else if (identifier == "CloudSun") { if ((((i >= 0) && (i <= 8)) && ((j >= 0) && (j <= 2))) || (((i >= 0) && (i <= 4)) && ((j >= 0) && (j <= 15)))) { leds[ XYsafe(i, j)] = CRGB::Yellow; Serial.println("Yellow!"); } else { leds[ XYsafe(i, j)] = CRGB::WhiteSmoke; Serial.println("White!"); } } } } } FastLED.show(); } void loop() { millisTime = millis(); if (firstIteration || millisTime % 60000 == 0) { int respCode = http.GET(); JsonDocument payload; if(respCode > 0){ const String response = http.getString(); deserializeJson(payload, response); if (String("Clouds") == String(payload["weather"][0]["main"])) { if (payload["weather"][0]["id"] >= 803) { DrawFrame(cloud_yx, "Clouds"); } else { DrawFrame(cloudSun_yx, "CloudSun"); } } else if (String("Rain") == String(payload["weather"][0]["main"]) || String("Drizzle") == String(payload["weather"][0]["main"])) { DrawFrame(rain_yx, "Rain"); } else if (String("Clear") == String(payload["weather"][0]["main"])) { DrawFrame(sun_yx, "Sun"); } else { DrawFrame(cloudSun_yx, "CloudSun"); } } else { Serial.print("error "); Serial.println(respCode); } firstIteration = false; } } void setup() { Serial.begin(115200); // weather api const float lat = 48.9517; const float lon = 10.1703; const String apiKey = "e471293b25fdb6697c0a862f2695df5f"; apiUrl = buildAPIUrl(lat, lon, apiKey); connectWiFi(); if (WiFi.status() != WL_CONNECTED) { Serial.println(WiFi.status()); } http.begin(client, apiUrl); FastLED.addLeds(leds, NUM_LEDS).setCorrection(TypicalSMD5050); FastLED.setBrightness( BRIGHTNESS ); }