#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define USE_SERIAL Serial
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
WiFiMulti wifiMulti;
int r=16;
int g=17;
int b=18;
const char* ssid = "Wokwi-GUEST"; // Change this to your WiFi SSID
const char* password = ""; // Change this to your WiFi password
void setup() {
USE_SERIAL.begin(115200);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
//wifiMulti.addAP("SSID", "PASSWORD");
Serial.begin(115200);
while(!Serial){delay(100);}
// We start by connecting to a WiFi network
Serial.println();
Serial.println("****************************************************");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// wait for WiFi connection
if((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
//http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
http.begin("https://data.moenv.gov.tw/api/v2/aqx_p_02?api_key=e8dd42e6-9b8b-43f8-991e-b3dee723a52d&limit=1000&sort=datacreationdate%20desc&format=JSON"); //HTTP
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// String input;
DynamicJsonDocument doc(16384);
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
for (JsonObject field : doc["fields"].as<JsonArray>()) {
const char* field_id = field["id"]; // "site", "county", "pm25", "datacreationdate", "itemunit"
const char* field_type = field["type"]; // "text", "text", "text", "text", "text"
const char* field_info_label = field["info"]["label"]; // "測站名稱", "縣市名稱", "細懸浮微粒濃度", "資料建置日期", "測項單位"
}
const char* resource_id = doc["resource_id"]; // "c1f31192-babd-4105-b880-a4c2e23a3276"
const char* extras_api_key = doc["__extras"]["api_key"]; // "e8dd42e6-9b8b-43f8-991e-b3dee723a52d"
bool include_total = doc["include_total"]; // true
const char* total = doc["total"]; // "78"
const char* resource_format = doc["resource_format"]; // "object"
const char* limit = doc["limit"]; // "1000"
const char* offset = doc["offset"]; // "0"
const char* links_start = doc["_links"]["start"];
const char* links_next = doc["_links"]["next"];
for (JsonObject record : doc["records"].as<JsonArray>()) {
const char* record_site = record["site"]; // "大城", "富貴角", "麥寮", "關山", "馬公", "金門", "馬祖", "埔里", "復興", ...
const char* record_county = record["county"]; // "彰化縣", "新北市", "雲林縣", "臺東縣", "澎湖縣", "金門縣", "連江縣", "南投縣", ...
const char* record_pm25 = record["pm25"]; // "9", "11", "18", "6", "11", "38", "17", "23", "26", "12", ...
const char* record_datacreationdate = record["datacreationdate"]; // "2023-11-27 08:00", "2023-11-27 ...
const char* record_itemunit = record["itemunit"]; // "μg/m3", "μg/m3", "μg/m3", "μg/m3", "μg/m3", ...
if(record["site"]=="臺南")
{
String s=record["site"];
Serial.print(s);
Serial.print("PM2.5=");
String p=record["pm25"];
Serial.print(p);
String u=record["itemunit"];
Serial.print(u);
Serial.print("時間=");
String t=record["datacreationdate"];
Serial.println(t);
}
if(record["site"]=="金門")
{
String s=record["site"];
Serial.print(s);
Serial.print("PM2.5=");
String p=record["pm25"];
Serial.print(p);
String u=record["itemunit"];
Serial.print(u);
Serial.print("時間=");
String t=record["datacreationdate"];
Serial.println(t);
}
if(record["site"]=="馬祖")
{
String s=record["site"];
Serial.print(s);
Serial.print("PM2.5=");
String p=record["pm25"];
Serial.print(p);
String u=record["itemunit"];
Serial.print(u);
Serial.print("時間=");
String t=record["datacreationdate"];
Serial.println(t);
}
}
//USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(5000);
}