本帖最後由 C_Law 於 2021-6-13 02:35 編輯
師兄請教一下,小弟正想試下整個香港地區溫度,用你上面個天文台api。
請問有冇參考連結例程可以用到香港 ...
royc 發表於 2021/6/10 11:10 
以下係我玩緊個鐘的部份 Code,可以用 rhrread 搵到:
閃電
分區氣溫
天文台濕度
以及用 warnsum 搵到
風球
紅黑黃雨
雷暴警告
寒冷/酷熱警告
紅/黃火災警告
上載到 ESP8266 前要先修改 PortNo, WiFi 的 SSID/PASSWORD。
Oooops~~~~這裡貼 Code 自動幫我刪除了一部份斜楝!照抄以下出來用係會錯,等我諗諗再 Post 過。
(好似改返正)- #ifndef UNIT_TEST
- #include <Arduino.h>
- #endif
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <WiFiClient.h>
- #include <ESP8266HTTPClient.h>
- // some parameters used for WiFi
- const unsigned int PortNo = 80;
- const char* SSID1 = "yourSSID1"; // SSID and Password for WiFi connection
- const char* PW1 = "yourPASSWORD1";
- const char* SSID2 = "yourSSID2";
- const char* PW2 = "yourPASSWORD2";
- const char* SSID3 = "yourSSID3";
- const char* PW3 = "yourPASSWORD3";
- const uint16_t LED = 2; // OnChipLED(Pull_Up)=D4 for D1 mini
- const bool LEDon = false; // false for Pull_Up, otherwise true
- // some variables for weather
- String weatherJSON;
- String TempC;
- String MaxMinTempC;
- String HumPercent;
- String WTCSGNL; // Tropical Cyclone Warning Signal: None, TC1, TC3, TC8xx, TC9, TC10
- bool Lightning;
- bool WTS = false; //Thunderstorm warning
- int wFire; // Fire warning: 0,1,2=Normal, Yellow, Red
- int wCH; // Cold/Hot warning: 0,1,2,=Normal,Cold,Hot
- int wRAIN; // Rain warning: 0,1,2,3=None,Amber,Red,Black
- ESP8266WiFiMulti wifiMulti;
- ESP8266WebServer server(PortNo);
- void setup() {
- pinMode(LED, OUTPUT);
- digitalWrite(LED, LEDon);
-
- Serial.begin(115200);
- Serial.println("");
-
- WiFi.mode(WIFI_STA);
- wifiMulti.addAP(SSID1, PW1);
- wifiMulti.addAP(SSID2, PW2);
- wifiMulti.addAP(SSID3, PW3);
-
- // waiting for WiFi connection
- while (wifiMulti.run() != WL_CONNECTED) {
- delay(500);
- }
-
- Serial.println("");
- Serial.print("Connected to: ");
- Serial.println(WiFi.SSID());
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP().toString());
- Serial.print("Port no.: ");
- Serial.println(PortNo);
- digitalWrite(LED, !LEDon);
-
- server.on("/", handleRoot);
- server.on("/rhrread", handleRhrread);
- server.on("/warnsum", handleWarnSum);
- server.on("/rhrread2", handleRhrread2);
- server.on("/warnsum2", handleWarnSum2);
- server.onNotFound(handleNotFound);
- server.begin();
- }
- void loop() {
- server.handleClient();
- }
- void handleRoot() {
- String message = "";
- message = "<HTML>\n" \
- " <HEAD>\n" \
- " <TITLE>Get weather information by ESP8266</TITLE>\n" \
- " </HEAD>\n" \
- " <body>\n" \
- " Get weather information by ESP8266<p>\n";
- message += " </body>\n" \
- "</HTLM>";
- server.send(200, "text/html", message);
- }
- void handleRhrread() {
- String message = "";
- if (GetJson("rhrread")){
- int StartPos = 0;
- int EndPos = weatherJSON.length() - 1;
- int ResultStart = 0;
- int ResultEnd = EndPos;
- String sTemp;
- String Location;
- message = weatherJSON;
-
- // Get Lightning warning for "Hong Kong and Kowloon"
- Location = "Hong Kong and Kowloon";
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- ResultStart = StartPos;
- ResultEnd = EndPos;
- Lightning = false;
- if (GetJsonRange("\"lightning\":{", "]", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"" + Location + "\",\"occur\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- Lightning = (weatherJSON.substring(ResultStart, ResultEnd) == "true");
- }
- }
- message += "\n\rLightning=";
- if (Lightning) message += "true";
- else message += "false";
-
- // Get "Kowloon City" temperture
- Location = "Kowloon City";
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- TempC = "";
- if (GetJsonRange("\"temperature\":{\"data\":[", "]", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"" + Location + "\",\"value\":", ",", StartPos, EndPos, &ResultStart, &ResultEnd)){
- TempC = weatherJSON.substring(ResultStart, ResultEnd) + "C";
- }
- }
- message += "\n\rTempC=" + TempC;
-
- // Get Humidity
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- ResultStart = StartPos;
- ResultEnd = EndPos;
- if (GetJsonRange("\"humidity\":{", "]", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"value\":", ",", StartPos, EndPos, &ResultStart, &ResultEnd)){
- HumPercent = weatherJSON.substring(ResultStart, ResultEnd) + "%";
- }
- }
- message += "\n\rHumidity=" +HumPercent;
- }
-
- server.send(200, "text/plain", message);
- }
- void handleRhrread2() {
- String message = "";
- if (true){
- weatherJSON = "{\"lightning\":{\"data\":[{\"place\":\"Lantau\",\"occur\":\"true\"},{\"place\":\"Hong Kong and Kowloon\",\"occur\":\"true\"}],\"startTime\":\"2021-06-11T18:45:00+08:00\",\"endTime\":\"2021-06-11T19:45:00+08:00\"},\"rainfall\":{\"data\":[{\"unit\":\"mm\",\"place\":\"Central & Western District\",\"max\":9,\"min\":1,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Eastern District\",\"max\":8,\"min\":1,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Kwai Tsing\",\"max\":2,\"min\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Islands District\",\"max\":12,\"min\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"North District\",\"max\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Sai Kung\",\"max\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Sha Tin\",\"max\":1,\"min\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Southern District\",\"max\":15,\"min\":7,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Tai Po\",\"max\":1,\"min\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Tsuen Wan\",\"max\":2,\"min\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Tuen Mun\",\"max\":3,\"min\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Wan Chai\",\"max\":10,\"min\":5,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Yuen Long\",\"max\":2,\"min\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Yau Tsim Mong\",\"max\":4,\"min\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Sham Shui Po\",\"max\":2,\"min\":1,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Kowloon City\",\"max\":1,\"min\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Wong Tai Sin\",\"max\":0,\"main\":\"FALSE\"},{\"unit\":\"mm\",\"place\":\"Kwun Tong\",\"max\":1,\"min\":0,\"main\":\"FALSE\"}],\"startTime\":\"2021-06-11T18:45:00+08:00\",\"endTime\":\"2021-06-11T19:45:00+08:00\"},\"icon\":[64],\"iconUpdateTime\":\"2021-06-11T20:25:00+08:00\",\"uvindex\":\"\",\"updateTime\":\"2021-06-11T20:26:00+08:00\",\"temperature\":{\"data\":[{\"place\":\"King's Park\",\"value\":27,\"unit\":\"C\"},{\"place\":\"Hong Kong Observatory\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Wong Chuk Hang\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Ta Kwu Ling\",\"value\":29,\"unit\":\"C\"},{\"place\":\"Lau Fau Shan\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Tai Po\",\"value\":29,\"unit\":\"C\"},{\"place\":\"Sha Tin\",\"value\":29,\"unit\":\"C\"},{\"place\":\"Tuen Mun\",\"value\":27,\"unit\":\"C\"},{\"place\":\"Tseung Kwan O\",\"value\":27,\"unit\":\"C\"},{\"place\":\"Sai Kung\",\"value\":29,\"unit\":\"C\"},{\"place\":\"Cheung Chau\",\"value\":26,\"unit\":\"C\"},{\"place\":\"Chek Lap Kok\",\"value\":27,\"unit\":\"C\"},{\"place\":\"Tsing Yi\",\"value\":29,\"unit\":\"C\"},{\"place\":\"Shek Kong\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Tsuen Wan Ho Koon\",\"value\":26,\"unit\":\"C\"},{\"place\":\"Tsuen Wan Shing Mun Valley\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Hong Kong Park\",\"value\":27,\"unit\":\"C\"},{\"place\":\"Shau Kei Wan\",\"value\":27,\"unit\":\"C\"},{\"place\":\"Kowloon City\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Happy Valley\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Wong Tai Sin\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Stanley\",\"value\":27,\"unit\":\"C\"},{\"place\":\"Kwun Tong\",\"value\":27,\"unit\":\"C\"},{\"place\":\"Sham Shui Po\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Kai Tak Runway Park\",\"value\":28,\"unit\":\"C\"},{\"place\":\"Yuen Long Park\",\"value\":29,\"unit\":\"C\"},{\"place\":\"Tai Mei Tuk\",\"value\":29,\"unit\":\"C\"}],\"recordTime\":\"2021-06-11T20:00:00+08:00\"},\"warningMessage\":[\"The Tropical Cyclone Signal No. 1 has been issued.\",\"The Amber Rainstorm Warning Signal was issued at 8:25 p.m.\",\"The Thunderstorm Warning has been issued. It will remain effective until 11:00 p.m. today. Occasional thunderstorms are expected to occur over Hong Kong.\"],\"mintempFrom00To09\":\"\",\"rainfallFrom00To12\":\"\",\"rainfallLastMonth\":\"\",\"rainfallJanuaryToLastMonth\":\"\",\"tcmessage\":[\"Here is the information on Tropical Depression:\nLocation: 18.3 degrees north, 112.3 degrees east; Maximum sustained wind near its centre: 45 km\/h.\"],\"humidity\":{\"recordTime\":\"2021-06-11T20:00:00+08:00\",\"data\":[{\"unit\":\"percent\",\"value\":88,\"place\":\"Hong Kong Observatory\"}]}}";
- int StartPos = 0;
- int EndPos = weatherJSON.length() - 1;
- int ResultStart = 0;
- int ResultEnd = EndPos;
- String sTemp;
- String Location;
- message = weatherJSON;
-
- // Get Lightning warning for "Hong Kong and Kowloon"
- Location = "Hong Kong and Kowloon";
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- ResultStart = StartPos;
- ResultEnd = EndPos;
- Lightning = false;
- if (GetJsonRange("\"lightning\":{", "]", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"" + Location + "\",\"occur\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- Lightning = (weatherJSON.substring(ResultStart, ResultEnd) == "true");
- }
- }
- message += "\n\rLightning=";
- if (Lightning) message += "true";
- else message += "false";
-
- // Get "Kowloon City" temperture
- Location = "Kowloon City";
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- TempC = "";
- if (GetJsonRange("\"temperature\":{\"data\":[", "]", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"" + Location + "\",\"value\":", ",", StartPos, EndPos, &ResultStart, &ResultEnd)){
- TempC = weatherJSON.substring(ResultStart, ResultEnd) + "C";
- }
- }
- message += "\n\rTempC=" + TempC;
-
- // Get Humidity
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- ResultStart = StartPos;
- ResultEnd = EndPos;
- if (GetJsonRange("\"humidity\":{", "]", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"value\":", ",", StartPos, EndPos, &ResultStart, &ResultEnd)){
- HumPercent = weatherJSON.substring(ResultStart, ResultEnd) + "%";
- }
- }
- message += "\n\rHumidity=" +HumPercent;
- }
-
- server.send(200, "text/plain", message);
- }
- void handleWarnSum() {
- String message ="";
- if (GetJson("warnsum")) {
- int StartPos = 0;
- int EndPos = weatherJSON.length() - 1;
- int ResultStart = 0;
- int ResultEnd = EndPos;
- String sTemp;
-
- // Check WRAIN
- wRAIN = 0;
- if (GetJsonRange("\"WRAIN\":{", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- sTemp = weatherJSON.substring(ResultStart, ResultEnd);
- if ((sTemp != "CANCEL") && (GetJsonRange("\"code\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd))){
- sTemp = weatherJSON.substring(ResultStart, ResultEnd);
- if (sTemp == "WRAINA") wRAIN = 1;
- else if (sTemp == "WRAINR") wRAIN = 2;
- else if (sTemp == "WRAINB") wRAIN = 3;
- }
- }
- }
- message = "\n\r\n\rwRAIN=";
- message += wRAIN;
-
- // Check Thunderstorm warning WTS
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- WTS = false;
- if (GetJsonRange("\"WTS\":{", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
-
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- sTemp = weatherJSON.substring(ResultStart, ResultEnd);
- if (sTemp != "CANCEL") WTS = true;
- }
- }
- message += "\n\rWTS=";
- if (WTS) message += "true";
- else message += "false";
-
- // Check Tropical Cyclone Warning Signal WTCSGNL
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- WTCSGNL = "None";
- if (GetJsonRange("\"WTCSGNL\":{\"", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- if (weatherJSON.substring(ResultStart, ResultEnd) != "CANCEL") {
- if (GetJsonRange("\"code\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- WTCSGNL = weatherJSON.substring(ResultStart, ResultEnd);
- }
- }
- }
- }
- message += "\n\rWTCSGNL=" + WTCSGNL;
-
- // Check WHOT
- wCH = 0;
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- if (GetJsonRange("\"WHOT\":{\"", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- if (weatherJSON.substring(ResultStart, ResultEnd) != "CANCEL") wCH = 2;
- }
- }
-
- // Check WCOLD
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- // wCH already reset before WHOT check, no need to reset in here
- if (GetJsonRange("\"COLD\":{\"", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- if (weatherJSON.substring(ResultStart, ResultEnd) != "CANCEL") wCH = 1;
- }
- }
- message += "\n\rwCH=";
- message += wCH;
-
- // Get WFIRE
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- wFire = 0;
- if (GetJsonRange("\"WFIRE\":{\"", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- if (weatherJSON.substring(ResultStart, ResultEnd) != "CANCEL") {
- if (GetJsonRange("\"code\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- sTemp = weatherJSON.substring(ResultStart, ResultEnd);
- if (sTemp == "WFIREY") wFire = 1; // yellow
- else if (sTemp == "WFIRER") wFire = 2; // red
- }
- }
- }
- }
- message += "\n\rwFire=";
- message += wFire;
- }
-
- message = weatherJSON + message;
- server.send(200, "text/plain", message);
- }
- void handleWarnSum2() {
- String message ="";
- if (true) {
- weatherJSON = "{\"WRAIN\":{\"name\":\"Rainstorm Warning Signal\",\"code\":\"WRAINA\",\"type\":\"Amber\",\"actionCode\":\"ISSUE\",\"issueTime\":\"2021-06-11T20:25:00+08:00\",\"updateTime\":\"2021-06-11T20:25:00+08:00\"},\"WTS\":{\"name\":\"Thunderstorm Warning\",\"code\":\"WTS\",\"actionCode\":\"UPDATE\",\"issueTime\":\"2021-06-11T18:35:00+08:00\",\"expireTime\":\"2021-06-11T23:00:00+08:00\",\"updateTime\":\"2021-06-11T21:10:00+08:00\"},\"WTCSGNL\":{\"name\":\"Tropical Cyclone Warning Signal\",\"code\":\"TC1\",\"actionCode\":\"ISSUE\",\"type\":\"Standby Signal No. 1\",\"issueTime\":\"2021-06-11T16:15:00+08:00\",\"updateTime\":\"2021-06-11T16:15:00+08:00\"}}";
- int StartPos = 0;
- int EndPos = weatherJSON.length() - 1;
- int ResultStart = 0;
- int ResultEnd = EndPos;
- String sTemp;
-
- // Check WRAIN
- wRAIN = 0;
- if (GetJsonRange("\"WRAIN\":{", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- sTemp = weatherJSON.substring(ResultStart, ResultEnd);
- if ((sTemp != "CANCEL") && (GetJsonRange("\"code\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd))){
- sTemp = weatherJSON.substring(ResultStart, ResultEnd);
- if (sTemp == "WRAINA") wRAIN = 1;
- else if (sTemp == "WRAINR") wRAIN = 2;
- else if (sTemp == "WRAINB") wRAIN = 3;
- }
- }
- }
- message = "\n\r\n\rwRAIN=";
- message += wRAIN;
-
- // Check Thunderstorm warning WTS
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- WTS = false;
- if (GetJsonRange("\"WTS\":{", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
-
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- sTemp = weatherJSON.substring(ResultStart, ResultEnd);
- if (sTemp != "CANCEL") WTS = true;
- }
- }
- message += "\n\rWTS=";
- if (WTS) message += "true";
- else message += "false";
-
- // Check Tropical Cyclone Warning Signal WTCSGNL
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- WTCSGNL = "None";
- if (GetJsonRange("\"WTCSGNL\":{\"", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- if (weatherJSON.substring(ResultStart, ResultEnd) != "CANCEL") {
- if (GetJsonRange("\"code\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- WTCSGNL = weatherJSON.substring(ResultStart, ResultEnd);
- }
- }
- }
- }
- message += "\n\rWTCSGNL=" + WTCSGNL;
-
- // Check WHOT
- wCH = 0;
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- if (GetJsonRange("\"WHOT\":{\"", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- if (weatherJSON.substring(ResultStart, ResultEnd) != "CANCEL") wCH = 2;
- }
- }
-
- // Check WCOLD
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- // wCH already reset before WHOT check, no need to reset in here
- if (GetJsonRange("\"COLD\":{\"", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- if (weatherJSON.substring(ResultStart, ResultEnd) != "CANCEL") wCH = 1;
- }
- }
- message += "\n\rwCH=";
- message += wCH;
-
- // Get WFIRE
- StartPos = 0;
- EndPos = weatherJSON.length() - 1;
- wFire = 0;
- if (GetJsonRange("\"WFIRE\":{\"", "}", StartPos, EndPos, &ResultStart, &ResultEnd)){
- StartPos = ResultStart;
- EndPos = ResultEnd;
- if (GetJsonRange("\"actionCode\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- if (weatherJSON.substring(ResultStart, ResultEnd) != "CANCEL") {
- if (GetJsonRange("\"code\":\"", "\"", StartPos, EndPos, &ResultStart, &ResultEnd)){
- sTemp = weatherJSON.substring(ResultStart, ResultEnd);
- if (sTemp == "WFIREY") wFire = 1; // yellow
- else if (sTemp == "WFIRER") wFire = 2; // red
- }
- }
- }
- }
- message += "\n\rwFire=";
- message += wFire;
- }
-
- message = weatherJSON + message;
- server.send(200, "text/plain", message);
- }
- void handleNotFound() {
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET)?"GET":"POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i = 0; i < server.args(); i++)
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- server.send(404, "text/plain", message);
- }
- bool GetJson(String dataType){
- /*
- dataType can be: rhrread, warnsum, fnd...etc
- */
- bool Result = false;
- HTTPClient http;
- WiFiClientSecure client;
- client.setInsecure(); // use with caution
- weatherJSON = ""; // clear previous JSON
-
- http.begin(client, "https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType=" + dataType + "&lang=en");
- if (http.GET() == HTTP_CODE_OK) {
- weatherJSON = http.getString();
- Result = true;
- }
- return Result;
- }
- bool GetJsonRange(String StartString, String EndString, int StartPos, int EndPos, int *SectionStart, int *SectionEnd) {
- bool Good = true;
- int ResultStart = StartPos;
- int ResultEnd = EndPos;
- String message = "";
- // initial check
- if ((StartString == "") || (EndString =="")) Good = false;
- if (!Good || (StartPos < 0) || (StartPos > weatherJSON.length() - 1)) Good = false;
- if (!Good || (EndPos <= StartPos) || (EndPos > weatherJSON.length() -1)) Good = false;
-
- if (Good) {
- ResultStart = weatherJSON.indexOf(StartString, StartPos);
- if (ResultStart < 0) Good =false;
- else {
- ResultStart += StartString.length();
- if (ResultStart > EndPos) Good =false;
- else {
- ResultEnd = weatherJSON.indexOf(EndString, ResultStart);
- if (ResultEnd > EndPos) Good = false;
- }
- }
- }
- if (Good) {
- *SectionStart = ResultStart;
- *SectionEnd = ResultEnd;
- }
- return Good;
- }
複製代碼 .
好似寫了好多行,實際有百幾行係重複 ,handleRhrread 和 handleWarnsum 以及 handleRhrread2 和 handleWarnsum2 基本上係一樣,分別只係前者會即時上天文台下載資料,而後者是我用來做測試,用了昨晚又一號風球、又黃雨、又雷暴……etc 的時候下載資料。
上載及連到 WiFi 後(LED 會熄),可以用以用 Web browser 經以下網址讀取資料:
http://ip-address:PortNo/xxxx
ip-address 係 Router 派給 ESP8266 的 ip
PortNo 係程式開頭由你自定的 Port number
xxxx 可以係 rhrread 或 warnsum。(以及 rhrread2 或 warnsum2)
主要係經 GetJson() 去下載天文台的 JSON 檔。
至於由 JSON 搵出想要的資料,應該係有 Library 去解讀內容,但我懶得睇 Library 的說明,寧願自己寫,我用 GetJsonRange() 去搵我想要的數值。
例如搵九龍城氣溫,氣溫必定係以下一段之內:
"temperature":{"data":[{...},{...},...{九龍城資料},...{...}]}
我先用 GetJsonRange() 去搵 "temperature":{"data":[ 以及 ] 的位置(ResultStart, ResultEnd)。
然後係上次搵到的範圍內再搵九龍城資料,這資料會類似係:
{"place":"Kowloon City","value":28,"unit":"C"}
而溫度就夾係 "place":"Kowloon City","value": 同後面的逗號之間,可以再用 GetJsonRange() 去搵這兩個位置,最後就可以用substring 從 weatherJSON 之中抽出溫度數值(String)。
不過有些資料,例如暴雨警告,除了搵 WRAIN 同個 code 之外,還是睇埋 actionCode 是否 CANCEL。 |