From b3c5cfa1e099b5d5127e4e48e23cbae716e0b3ca Mon Sep 17 00:00:00 2001 From: tiijay Date: Sat, 8 Nov 2025 13:54:31 +0100 Subject: [PATCH] v.0.2.0 WiFiClient HTTP request --- lib/BlinkLed/src/BlinkLed.cpp | 2 +- lib/BlinkLed/src/BlinkLed.h | 5 +- platformio.ini | 8 +-- src/main.cpp | 116 +++++++++++++++++++++++++++++++++- 4 files changed, 120 insertions(+), 11 deletions(-) diff --git a/lib/BlinkLed/src/BlinkLed.cpp b/lib/BlinkLed/src/BlinkLed.cpp index 504fcb0..1c7ca4e 100644 --- a/lib/BlinkLed/src/BlinkLed.cpp +++ b/lib/BlinkLed/src/BlinkLed.cpp @@ -1,6 +1,6 @@ #include "BlinkLed.h" -BlinkLed::BlinkLed(byte pin) +BlinkLed::BlinkLed(u_int8_t pin) { BlinkLed(); _pin = pin; diff --git a/lib/BlinkLed/src/BlinkLed.h b/lib/BlinkLed/src/BlinkLed.h index 698db4b..be567a0 100644 --- a/lib/BlinkLed/src/BlinkLed.h +++ b/lib/BlinkLed/src/BlinkLed.h @@ -2,17 +2,16 @@ #define BLINKLED #include - class BlinkLed { private: PinStatus _ledStatus; - byte _pin; + u_int8_t _pin; /* data */ BlinkLed() { _ledStatus = LOW; } public: - BlinkLed(byte pin); + BlinkLed(u_int8_t pin); ~BlinkLed() {} void on(); diff --git a/platformio.ini b/platformio.ini index c729259..362a9a1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,8 +15,6 @@ framework = arduino monitor_speed = 115200 build_flags = -D PICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1 - -lib_deps = fastled/FastLED@^3.10.3 - -; Add this to see build flags during compilation -; build_flags = -v \ No newline at end of file +lib_deps = + fastled/FastLED@^3.10.3 + bblanchon/ArduinoJson@^7.4.2 diff --git a/src/main.cpp b/src/main.cpp index a9c452a..44a856f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,9 @@ +#include +#include +#include #include #include #include -#include #include const uint8_t MATRIX_ROWS = 64; @@ -91,6 +93,106 @@ void setupMatrix() matrix.show(); } +void get_json_data() +{ + Serial.println("get_json_data()"); + + 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 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("."); + } + Serial.println("\nConnected to WiFi!"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); +} + void setup() { // Initialize Serial for debug output @@ -102,8 +204,18 @@ void setup() delay(10); } + connect_wifi(); + if (WiFi.status() == WL_CONNECTED) + { + get_json_data(); + } + else + { + Serial.println("WiFi NOT connected."); + } + setupMatrix(); - printCharMatrixSimple('@'); + // printCharMatrixSimple('@'); // number_to_bitarray_msb(0x11FF, 16, true); // playWithBlinkLed();