wersja pod ESP32, ESP8266 (kiedyś robiłem)
dla serwera API : api.weatherapi.com
dla serwera API : api.weatherapi.com
Kod:
#include <Arduino_JSON.h>
JSONVar JSON_Air; // obiekt Powietrze
struct Air
{
double co = 0; // Carbon Monoxide (μg/m3) - Tlenek węgla (μg / m3)
double o3 = 0; // Ozone (μg/m3) - Ozon (μg / m3)
double no2 = 0; // Nitrogen dioxide (μg/m3) - Dwutlenek azotu (μg / m3)
double so2 = 0; // Sulphur dioxide (μg/m3) - Dwutlenek siarki (μg / m3)
double pm25 = 0; // PM2.5 (μg/m3) - aerozole atmosferyczne o średnicy nie większej niż 2,5 μm
double pm10 = 0; // PM10 (μg/m3) - mieszanina zawieszonych w powietrzu cząsteczek o średnicy nie większej niż 10 μm
int Defra = 0; // Air Quality Index - index jakości powietrza
int EPA = 0; // Air Qaality Index - index jakosci powietrza (standard EPA)
String opis = "NULL"; // Air Quality opis
}
powietrze;
String httpGETRequest(const char* serverName)
{
HTTPClient http;
http.begin(serverName);
int httpResponseCode = http.GET();
String bufor = "{}";
if ( httpResponseCode > 0 )
{
bufor = http.getString();
}
else
{
// error
}
http.end();
return bufor;
}
void AirQualityUpdate()
{
String PogodaToken = "twoj_token"; // token serwera pogodowego http://api.weatherapi.com
String PogodaMiasto = "twoje_miasto"; // miasto dla ścieżki HTTP http://api.weatherapi.com
String path = "http://api.weatherapi.com/v1/current.json?key=" + PogodaToken + "&q=" + PogodaMiasto + "&aqi=yes";
String jsonBuffer = httpGETRequest(path.c_str());
JSON_Air = JSON.parse(jsonBuffer);
if (JSON.typeof(JSON_Air) == "undefined")
{
// błąd parsowania
}
powietrze.co = JSON_Air["current"]["air_quality"]["co"];
powietrze.o3 = JSON_Air["current"]["air_quality"]["o3"];
powietrze.no2 = JSON_Air["current"]["air_quality"]["no2"];
powietrze.so2 = JSON_Air["current"]["air_quality"]["so2"];
powietrze.pm25 = JSON_Air["current"]["air_quality"]["pm2_5"];
powietrze.pm10 = JSON_Air["current"]["air_quality"]["pm10"];
powietrze.Defra = JSON_Air["current"]["air_quality"]["gb-defra-index"];
powietrze.EPA = JSON_Air["current"]["air_quality"]["us-epa-index"];
}