v.0.2.1 ColorSerial
This commit is contained in:
86
src/main.cpp
86
src/main.cpp
@@ -1,6 +1,8 @@
|
||||
#include <WiFi.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <LedMatrix.h>
|
||||
#include <ColorSerial.h>
|
||||
#include <FastLED.h>
|
||||
#include <MyClass.h>
|
||||
#include <BlinkInternLed.h>
|
||||
@@ -93,9 +95,26 @@ void setupMatrix()
|
||||
matrix.show();
|
||||
}
|
||||
|
||||
void get_json_data()
|
||||
void connect_wifi()
|
||||
{
|
||||
Serial.println("get_json_data()");
|
||||
// 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 json_with_WiFiClient()
|
||||
{
|
||||
Serial.println("json_with_WiFiClient()");
|
||||
|
||||
WiFiClient client;
|
||||
const int timeout = 5000; // 5 second timeout
|
||||
@@ -176,21 +195,54 @@ void get_json_data()
|
||||
}
|
||||
}
|
||||
|
||||
void connect_wifi()
|
||||
void json_with_HTTPClient()
|
||||
{
|
||||
// For Wokwi simulation - use simulated WiFi
|
||||
Serial.println("Connecting to WiFi...");
|
||||
Serial.println("json_with_HTTPClient()");
|
||||
|
||||
// In Wokwi, WiFi connects automatically in simulation
|
||||
WiFi.begin("Wokwi-GUEST", ""); // Wokwi's simulated WiFi
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
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)
|
||||
{
|
||||
delay(250);
|
||||
Serial.print(".");
|
||||
Serial.printf("HTTP error: %d\n", httpCode);
|
||||
http.end();
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.println("connected to api.open-meteo.com :-)");
|
||||
Serial.println("=== Full Response Complete ===");
|
||||
|
||||
String payload = http.getString();
|
||||
http.end();
|
||||
|
||||
Serial.println(payload);
|
||||
|
||||
// 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"];
|
||||
|
||||
Serial.printf("Temperature: %.1f %s", temperature, temp_unit.c_str());
|
||||
Serial.println();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("JSON parse failed");
|
||||
Serial.println(error.c_str());
|
||||
|
||||
return;
|
||||
}
|
||||
Serial.println("\nConnected to WiFi!");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void setup()
|
||||
@@ -200,14 +252,16 @@ void setup()
|
||||
|
||||
// Wait for Serial to be ready (important for some boards)
|
||||
while (!Serial)
|
||||
{
|
||||
delay(10);
|
||||
}
|
||||
|
||||
ColorSerial::header("PICO W WEATHER STATION");
|
||||
ColorSerial::info("System starting up", "v1.0");
|
||||
|
||||
connect_wifi();
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
get_json_data();
|
||||
json_with_WiFiClient();
|
||||
json_with_HTTPClient();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user