#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define USE_SERIAL Serial
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();
WiFi.mode(WIFI_STA);
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
// wait for WiFi connection
WiFi.begin(ssid, password,7); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
pinMode(21,OUTPUT);
pinMode(19,OUTPUT);
pinMode(18,OUTPUT);
pinMode(5,OUTPUT);
pinMode(17,OUTPUT);
pinMode(16,OUTPUT);
pinMode(4,OUTPUT);
pinMode(0,OUTPUT);
pinMode(2,OUTPUT);
}
void RGB(int r,int g,int b,int n){
if(n==1){
analogWrite(21,r);
analogWrite(19,g);
analogWrite(18,b);
}
if(n==2){
analogWrite(5,r);
analogWrite(17,g);
analogWrite(16,b);
}
if(n==3){
analogWrite(4,r);
analogWrite(0,g);
analogWrite(2,b);
}
}
void loop() {
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"]; // "28", "24", "27", "6", "22", "38", "20", "2", "36", "21", ...
const char* record_datacreationdate = record["datacreationdate"]; // "2023-11-30 15:00", "2023-11-30 ...
const char* record_itemunit = record["itemunit"]; // "μg/m3", "μg/m3", "μg/m3", "μg/m3", "μg/m3", ...
String site = record["site"];
String pm25 = record["pm25"];
String datacreationdate = record["datacreationdate"];
String itemunit = record["itemunit"];
if(site == "臺南"){
USE_SERIAL.println(datacreationdate + " " + site + " pm2.5: " + pm25 + " " + itemunit);
int pm25_int = pm25.toInt();
pm25_int =100;
if(pm25_int <= 50){//綠色
RGB(0,255,0,1);
}else if(pm25_int <= 100){
RGB(255,255,0,1);//黃色
}else if(pm25_int <= 150){
RGB(255,120,0,1);//橘色
}else if(pm25_int <= 200){
RGB(255,0,0,1);//紅色
}else if(pm25_int <= 300){
RGB(128,0,128,1);//紫色
}else{
RGB(255,15,0,1);//橘紅色
}
}else if(site == "金門"){
USE_SERIAL.println(datacreationdate + " " + site + " pm2.5: " + pm25 + " " + itemunit);
int pm25_int = pm25.toInt();
pm25_int =200;
if(pm25_int <= 50){//綠色
RGB(0,255,0,2);
}else if(pm25_int <= 100){
RGB(255,255,0,2);//黃色
}else if(pm25_int <= 150){
RGB(255,120,0,2);//橘色
}else if(pm25_int <= 200){
RGB(255,0,0,2);//紅色
}else if(pm25_int <= 300){
RGB(128,0,128,2);//紫色
}else{
RGB(255,15,0,2);//橘紅色
}
}else if(site == "馬祖"){
USE_SERIAL.println(datacreationdate + " " + site + " pm2.5: " + pm25 + " " + itemunit);
int pm25_int = pm25.toInt();
pm25_int =500;
if(pm25_int <= 50){//綠色
RGB(0,255,0,3);
}else if(pm25_int <= 100){
RGB(255,255,0,3);//黃色
}else if(pm25_int <= 150){
RGB(255,120,0,3);//橘色
}else if(pm25_int <= 200){
RGB(255,0,0,3);//紅色
}else if(pm25_int <= 300){
RGB(128,0,128,3);//紫色
}else{
RGB(255,15,0,3);//橘紅色
}
}
}
//USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(5000);
}台南
金門
馬祖