Ver código fonte

Add networking and weather API

Anton 10 meses atrás
pai
commit
02c2122bbb
1 arquivos alterados com 46 adições e 0 exclusões
  1. 46 0
      WeatherMatrix.ino

+ 46 - 0
WeatherMatrix.ino

@@ -6,6 +6,25 @@
 #define CHIPSET     WS2811
 
 #define BRIGHTNESS 10
+ 
+// networking and json
+String apiUrl;
+int status = WL_IDLE_STATUS;
+
+int connectWiFi() {
+  WiFi.begin("Wokwi-GUEST", "", 6);
+  while (WiFi.status() != WL_CONNECTED) {
+    delay(100);
+  }
+  return WiFi.status();
+}
+
+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=" +
+                latStr + "&lon=" + lonStr + "&appid=" + apiKey);
+}
 
 // Params for width and height
 const uint8_t kMatrixWidth = 16;
@@ -100,6 +119,33 @@ void loop()
 
 void setup() {
   Serial.begin(9600);
+
+  // 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());
+  }
+  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);
+  }
+
+  
   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
   FastLED.setBrightness( BRIGHTNESS );
 }