v.0.2.0 WiFiClient HTTP request
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#include "BlinkLed.h"
|
#include "BlinkLed.h"
|
||||||
|
|
||||||
BlinkLed::BlinkLed(byte pin)
|
BlinkLed::BlinkLed(u_int8_t pin)
|
||||||
{
|
{
|
||||||
BlinkLed();
|
BlinkLed();
|
||||||
_pin = pin;
|
_pin = pin;
|
||||||
|
|||||||
@@ -2,17 +2,16 @@
|
|||||||
#define BLINKLED
|
#define BLINKLED
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
class BlinkLed
|
class BlinkLed
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
PinStatus _ledStatus;
|
PinStatus _ledStatus;
|
||||||
byte _pin;
|
u_int8_t _pin;
|
||||||
/* data */
|
/* data */
|
||||||
BlinkLed() { _ledStatus = LOW; }
|
BlinkLed() { _ledStatus = LOW; }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BlinkLed(byte pin);
|
BlinkLed(u_int8_t pin);
|
||||||
~BlinkLed() {}
|
~BlinkLed() {}
|
||||||
|
|
||||||
void on();
|
void on();
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ framework = arduino
|
|||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
build_flags =
|
build_flags =
|
||||||
-D PICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1
|
-D PICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1
|
||||||
|
lib_deps =
|
||||||
lib_deps = fastled/FastLED@^3.10.3
|
fastled/FastLED@^3.10.3
|
||||||
|
bblanchon/ArduinoJson@^7.4.2
|
||||||
; Add this to see build flags during compilation
|
|
||||||
; build_flags = -v
|
|
||||||
|
|||||||
116
src/main.cpp
116
src/main.cpp
@@ -1,7 +1,9 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <LedMatrix.h>
|
||||||
#include <FastLED.h>
|
#include <FastLED.h>
|
||||||
#include <MyClass.h>
|
#include <MyClass.h>
|
||||||
#include <BlinkInternLed.h>
|
#include <BlinkInternLed.h>
|
||||||
#include <LedMatrix.h>
|
|
||||||
#include <mathematics.h>
|
#include <mathematics.h>
|
||||||
|
|
||||||
const uint8_t MATRIX_ROWS = 64;
|
const uint8_t MATRIX_ROWS = 64;
|
||||||
@@ -91,6 +93,106 @@ void setupMatrix()
|
|||||||
matrix.show();
|
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()
|
void setup()
|
||||||
{
|
{
|
||||||
// Initialize Serial for debug output
|
// Initialize Serial for debug output
|
||||||
@@ -102,8 +204,18 @@ void setup()
|
|||||||
delay(10);
|
delay(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connect_wifi();
|
||||||
|
if (WiFi.status() == WL_CONNECTED)
|
||||||
|
{
|
||||||
|
get_json_data();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("WiFi NOT connected.");
|
||||||
|
}
|
||||||
|
|
||||||
setupMatrix();
|
setupMatrix();
|
||||||
printCharMatrixSimple('@');
|
// printCharMatrixSimple('@');
|
||||||
// number_to_bitarray_msb(0x11FF, 16, true);
|
// number_to_bitarray_msb(0x11FF, 16, true);
|
||||||
|
|
||||||
// playWithBlinkLed();
|
// playWithBlinkLed();
|
||||||
|
|||||||
Reference in New Issue
Block a user