v.0.3.1 use of WeatherFetcher
This commit is contained in:
@@ -49,7 +49,7 @@ public:
|
||||
void clearRow(uint8_t y);
|
||||
|
||||
// Draw text string
|
||||
void drawText(int x, int y, const char *text, CRGB color);
|
||||
void drawText(int x, int y, const char *text, CRGB color = CRGB::White);
|
||||
|
||||
void drawHorizontalScrollText(uint8_t y, const char *text, CRGB color, uint8_t scrollSpeed = 100);
|
||||
};
|
||||
|
||||
@@ -41,19 +41,22 @@ Weather WeatherFetcher::actual_weather(const char *city, const char *lang, bool
|
||||
}
|
||||
|
||||
String weather_url =
|
||||
"https://api.weatherapi.com/v1/current.json?key=" + String(API_KEY) +
|
||||
"http://api.weatherapi.com/v1/current.json?key=" + String(API_KEY) +
|
||||
"&q=" + String(city) + "&aqi=yes&lang=" + String(lang);
|
||||
|
||||
Serial.println("Hole Wetterdaten von: " + weather_url);
|
||||
String msg = "Hole Wetterdaten von: " + weather_url;
|
||||
ColorSerial::info(msg.c_str());
|
||||
|
||||
String json_data = fetch_from_url(weather_url);
|
||||
|
||||
if (!json_data.isEmpty())
|
||||
{
|
||||
ColorSerial::info("data retrieved");
|
||||
weather = parse_weather_json(json_data);
|
||||
}
|
||||
else
|
||||
{
|
||||
ColorSerial::error("no data retrieved");
|
||||
Serial.println("Fehler beim Abrufen der Wetterdaten!");
|
||||
}
|
||||
|
||||
@@ -66,7 +69,7 @@ String WeatherFetcher::fetch_from_url(const String &url)
|
||||
String payload = "";
|
||||
|
||||
http.begin(url);
|
||||
http.setTimeout(10000);
|
||||
// http.setTimeout(10000);
|
||||
|
||||
int httpCode = http.GET();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef WEATHERSTRUCTS_H
|
||||
#define WEATHERSTRUCTS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <optional>
|
||||
|
||||
struct Location
|
||||
|
||||
191
src/main.cpp
191
src/main.cpp
@@ -8,6 +8,13 @@
|
||||
#include <BlinkInternLed.h>
|
||||
#include <mathematics.h>
|
||||
|
||||
#include <WeatherFetcher.h>
|
||||
|
||||
const char *api_key = "3545ce42d0ba436e8dc164532250410";
|
||||
|
||||
const char *ssid = "WOKWI-Guest";
|
||||
const char *password = "12345678";
|
||||
|
||||
const uint8_t MATRIX_ROWS = 64;
|
||||
const uint8_t MATRIX_COLS = 64;
|
||||
|
||||
@@ -21,6 +28,8 @@ const uint8_t _fontWidthPixel = 8;
|
||||
const uint8_t _fontWidthPixel = 16;
|
||||
#endif
|
||||
|
||||
WeatherFetcher fetcher(api_key);
|
||||
|
||||
void printCharMatrixSimple(char c)
|
||||
{
|
||||
#ifdef USE_FONT_5x7
|
||||
@@ -86,170 +95,26 @@ void setupMatrix()
|
||||
matrix.show();
|
||||
|
||||
// Draw static "HELLO WORLD"
|
||||
matrix.drawText(1, 1, "Hello World!", CRGB::Red);
|
||||
// matrix.drawText(1, 1, "Hello World!", CRGB::Red);
|
||||
// matrix.drawText(1, 9, "<°|`¶>", CRGB::Yellow);
|
||||
// matrix.drawText(1, 17, "Bottom", CRGB::Yellow);
|
||||
// matrix.drawText(1, 25, "ABCDEFGHIJ.", CRGB::Yellow);
|
||||
// matrix.drawText(1, 33, "A", CRGB::Yellow);
|
||||
// matrix.show();
|
||||
}
|
||||
|
||||
void displayWeatherOnMatrix(const Weather w)
|
||||
{
|
||||
setupMatrix();
|
||||
|
||||
String zeit_temp = w.location.localtime.substring(11) + " " + w.current.temp_c + "°C";
|
||||
|
||||
matrix.drawText(0, 0, w.location.name.c_str(), CRGB::Yellow);
|
||||
matrix.drawText(0, 8, zeit_temp.c_str(), CRGB::Yellow);
|
||||
|
||||
matrix.show();
|
||||
}
|
||||
|
||||
void connect_wifi()
|
||||
{
|
||||
// For Wokwi simulation - use simulated WiFi
|
||||
Serial.println("Connecting to WiFi...");
|
||||
|
||||
// In Wokwi, WiFi connects automatically in simulation
|
||||
WiFi.begin("Wokwi-GUEST", ""); // Wokwi's simulated WiFi
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(250);
|
||||
Serial.print(".");
|
||||
}
|
||||
ColorSerial::wifiStatus();
|
||||
}
|
||||
|
||||
void json_with_WiFiClient()
|
||||
{
|
||||
Serial.println("json_with_WiFiClient()");
|
||||
|
||||
WiFiClient client;
|
||||
const int timeout = 5000; // 5 second timeout
|
||||
|
||||
if (client.connect("api.open-meteo.com", 80))
|
||||
{
|
||||
Serial.println("connected to api.open-meteo.com");
|
||||
|
||||
client.println("GET /v1/forecast?latitude=52.52&longitude=13.41¤t_weather=true HTTP/1.1");
|
||||
client.println("Host: api.open-meteo.com");
|
||||
client.println("User-Agent: PicoW");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
|
||||
// Wait for response with timeout
|
||||
unsigned long startTime = millis();
|
||||
while (client.available() == 0)
|
||||
{
|
||||
if (millis() - startTime > timeout)
|
||||
{
|
||||
Serial.println(">>> Client Timeout !");
|
||||
client.stop();
|
||||
return;
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
|
||||
// JSON Response parsen
|
||||
String response = "";
|
||||
while (client.available())
|
||||
{
|
||||
String line = client.readStringUntil('\n');
|
||||
response += line + "\n";
|
||||
|
||||
// Debug: print each line as it comes
|
||||
Serial.print("> ");
|
||||
Serial.println(line);
|
||||
}
|
||||
Serial.println("=== Full Response Complete ===");
|
||||
|
||||
// JSON parsen (nach dem Header)
|
||||
int jsonStart = response.indexOf('{');
|
||||
if (jsonStart > 0)
|
||||
{
|
||||
Serial.println("jsonStart > 0");
|
||||
String json = response.substring(jsonStart);
|
||||
|
||||
// StaticJsonDocument<512> doc;
|
||||
JsonDocument doc;
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
|
||||
if (!error)
|
||||
{
|
||||
JsonObject current = doc["current_weather"];
|
||||
JsonObject units = doc["current_weather_units"];
|
||||
|
||||
float temperature = current["temperature"];
|
||||
String temp_unit = units["temperature"];
|
||||
|
||||
Serial.printf("Temperature: %.1f %s", temperature, temp_unit.c_str());
|
||||
Serial.println();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(error.c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("NOT jsonStart > 0");
|
||||
}
|
||||
|
||||
client.stop();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("NO connection to api.open-meteo.com");
|
||||
}
|
||||
}
|
||||
|
||||
void json_with_HTTPClient()
|
||||
{
|
||||
ColorSerial::debug("json_with_HTTPClient()");
|
||||
|
||||
HTTPClient http;
|
||||
|
||||
http.begin("http://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t_weather=true");
|
||||
// http.setTimeout(5000);
|
||||
|
||||
int httpCode = http.GET();
|
||||
|
||||
if (httpCode != HTTP_CODE_OK)
|
||||
{
|
||||
String errMsg = "HTTP error: GET request failed (httpCode:" + String(httpCode + ")");
|
||||
ColorSerial::error(errMsg.c_str());
|
||||
http.end();
|
||||
return;
|
||||
}
|
||||
|
||||
ColorSerial::info("connected to api.open-meteo.com");
|
||||
ColorSerial::info("=== Full Response Complete ===");
|
||||
|
||||
String payload = http.getString();
|
||||
http.end();
|
||||
|
||||
ColorSerial::debug(payload.c_str());
|
||||
|
||||
// JSON parsen
|
||||
JsonDocument doc;
|
||||
DeserializationError error = deserializeJson(doc, payload);
|
||||
|
||||
if (!error)
|
||||
{
|
||||
JsonObject current = doc["current_weather"];
|
||||
JsonObject units = doc["current_weather_units"];
|
||||
|
||||
float temperature = current["temperature"];
|
||||
String temp_unit = units["temperature"];
|
||||
|
||||
uint8_t weathercode = current["weathercode"];
|
||||
|
||||
ColorSerial::temperature(temperature, temp_unit.c_str());
|
||||
|
||||
String temp_msg = "Temperature: " + String(temperature, 1) + temp_unit.c_str();
|
||||
|
||||
ColorSerial::info(temp_msg.c_str(), "Anzeige Temperatur Wert");
|
||||
ColorSerial::weatherCondition(weathercode);
|
||||
}
|
||||
else
|
||||
{
|
||||
String errCode = error.c_str();
|
||||
String errMsg = "JSON parse failed: " + errCode;
|
||||
ColorSerial::error(errMsg.c_str());
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize Serial for debug output
|
||||
@@ -263,12 +128,9 @@ void setup()
|
||||
ColorSerial::boxHeader("PICO W WEATHER STATION");
|
||||
ColorSerial::info("System starting up", "v1.0");
|
||||
|
||||
connect_wifi();
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
if (!fetcher.connectToWiFi(ssid, password))
|
||||
{
|
||||
// json_with_WiFiClient();
|
||||
json_with_HTTPClient();
|
||||
Serial.println("Starte im Testmodus wegen WiFi-Fehler");
|
||||
}
|
||||
|
||||
setupMatrix();
|
||||
@@ -280,6 +142,9 @@ void setup()
|
||||
|
||||
void loop()
|
||||
{
|
||||
Weather weather = fetcher.actual_weather("grosshansdorf", "de", false);
|
||||
displayWeatherOnMatrix(weather);
|
||||
|
||||
// matrix.drawHorizontalScrollText(41, "scrolling...", CRGB::Wheat);
|
||||
delay(10);
|
||||
delay(1000 * 60);
|
||||
}
|
||||
Reference in New Issue
Block a user