Browse Source

feat(core): connect api with graphics

sezuisa 10 months ago
parent
commit
5dbbd30b31
1 changed files with 141 additions and 32 deletions
  1. 141 32
      WeatherMatrix/WeatherMatrix.ino

+ 141 - 32
WeatherMatrix/WeatherMatrix.ino

@@ -1,4 +1,9 @@
 #include <FastLED.h>
+#include <ArduinoJson.h>
+#include <ESP8266HTTPClient.h>
+#include <ESP8266WiFi.h>
+#include <ESP8266WiFiMulti.h>
+#include <WiFiClient.h>
 
 #define LED_PIN  0
 
@@ -6,23 +11,53 @@
 #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;
 
-int connectWiFi() {
-  WiFi.begin("Wokwi-GUEST", "", 6);
-  while (WiFi.status() != WL_CONNECTED) {
-    delay(100);
+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);
   }
-  return WiFi.status();
+
+  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("https://api.openweathermap.org/data/2.5/weather?lat=" +
+  return String("http://api.openweathermap.org/data/2.5/weather?lat=" +
                 latStr + "&lon=" + lonStr + "&appid=" + apiKey);
 }
 
@@ -90,35 +125,123 @@ uint8_t cloud_yx[16][16] = {
   {0, 0, 0, 0, 0, 0, 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,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) {
-        leds[ XYsafe(i, j)]  = CRGB::Yellow;
+        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()
 {
-  DrawFrame(sun_yx);
-  FastLED.show();
-  delay(3000);
-  FastLED.clear();
-  DrawFrame(cloud_yx);
-  FastLED.show();
-  delay(3000);
-  FastLED.clear();
+  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(9600);
+  Serial.begin(115200);
 
   // weather api
   const float lat = 48.9517;
@@ -130,21 +253,7 @@ void setup() {
   if (WiFi.status() != WL_CONNECTED) {
     Serial.println(WiFi.status());
   }
-  HTTPClient http;
-  http.begin(apiUrl);
-  int respCode = http.GET();
-
-  JsonDocument payload;
-
-  if(respCode > 0){
-    const String response = http.getString();
-    deserializeJson(payload, response);
-    Serial.println(payload.size());
-  } else {
-    Serial.print("error ");
-    Serial.println(respCode);
-  }
-
+  http.begin(client, apiUrl);
   
   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
   FastLED.setBrightness( BRIGHTNESS );