1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
| #include <ESP8266HTTPClient.h> #include <ArduinoJson.h> #include <ESP8266WiFi.h> #include <vector> #include <Ticker.h>
Ticker ticker;
struct Weather{ String wea=""; String temp=""; String windpower=""; String humi=""; };
const char* ssid = "xxxxx"; const char* password = "xxxxxx";
const String timeUrl = "http://quan.suning.com/getSysTime.do"; const String gaoDeUrl = "http://restapi.amap.com/v3/weather/weatherInfo?key=xxxxxx&city=610100"; String timeMsg=""; String weatherMsg=""; String timeAfterParse=""; struct Weather weatherAfterParse; String data2stm=""; String data2client="";
String callHttp(String httpUrl); String parseJsonTime(String tjson); struct Weather parseJsonWeather(String wjson); void led_on_off(); String tqzh2py(String zhtq); String windpowerFormat(String wp);
void connectWIFI(char* ssid, char* password); void NewClient(); void setLedOnOff(WiFiClient serverClient); void sendData2Client(WiFiClient serverClient, uint8_t sbuf[], int len); void receiveDataFromClient(WiFiClient serverClient); void getSerialDataAndProcess();
WiFiServer server(5045); WiFiClient serverClient;
void setup() { Serial.begin(115200); server.begin(); pinMode(2, OUTPUT); digitalWrite(2, LOW); connectWIFI(ssid, password);
timeMsg = callHttp(timeUrl); weatherMsg = callHttp(gaoDeUrl); }
String clientMsg=""; String stm32Msg=""; int n = 0;
void loop() { n++; NewClient(); setLedOnOff(serverClient);
timeMsg = callHttp(timeUrl); if(n >= 12){ weatherMsg = callHttp(gaoDeUrl); n=0; } receiveDataFromClient(serverClient);
getSerialDataAndProcess();
if(timeMsg!=""){ timeAfterParse = parseJsonTime(timeMsg); } if(weatherMsg!=""){ weatherAfterParse = parseJsonWeather(weatherMsg); }
if(timeMsg!="" && weatherMsg!=""){ data2stm = (timeAfterParse + "," + tqzh2py(weatherAfterParse.wea) + "," + weatherAfterParse.temp + "," +weatherAfterParse.humi +"," + windpowerFormat(weatherAfterParse.windpower) + "," + clientMsg + "," + "\r\n"); Serial.print(data2stm);
}
}
void led_on_off(){ static bool LEDState=0; LEDState = !LEDState; digitalWrite(LED_BUILTIN, LEDState); }
String callHttp(String httpUrl) { HTTPClient hClient; String message; hClient.begin(httpUrl); int httpCode = hClient.GET(); if (httpCode > 0) { if (httpCode == HTTP_CODE_OK) { String payload = hClient.getString(); message = payload; } } else { message = "[1-HTTP]failed, error:" + String(hClient.errorToString(httpCode).c_str()); } hClient.end(); return message; }
void getTimeMsg(){ timeMsg = callHttp(timeUrl); }
void getWeatherMsg(){ weatherMsg = callHttp(gaoDeUrl); }
String parseJsonTime(String tjson){
const size_t capacity = 96; DynamicJsonDocument tdoc(capacity); deserializeJson(tdoc, tjson); String datetime = tdoc["sysTime2"].as<String>();
return datetime; }
struct Weather parseJsonWeather(String wjson){ struct Weather weather;
const size_t capacity = 512; DynamicJsonDocument wdoc(capacity); deserializeJson(wdoc, wjson);
JsonObject lives_0 = wdoc["lives"][0]; weather.wea = lives_0["weather"].as<String>(); weather.temp = lives_0["temperature"].as<String>(); weather.humi = lives_0["humidity"].as<String>(); weather.windpower = lives_0["windpower"].as<String>(); return weather; }
String tqzh2py(String zhtq){
String zh_cn[68] = {"晴","少云","晴间多云","多云","阴","有风","平静","微风","和风","清风","强风/劲风","疾风","大风","烈风","风暴","狂爆风","飓风","热带风暴","霾","中度霾","重度霾","严重霾","阵雨","雷阵雨","雷阵雨并伴有冰雹","小雨","中雨","大雨","暴雨","大暴雨","特大暴雨","强阵雨","强雷阵雨","极端降雨","毛毛雨/细雨","雨","小雨-中雨","中雨-大雨","大雨-暴雨","暴雨-大暴雨","大暴雨-特大暴雨","雨雪天气","雨夹雪","阵雨夹雪","冻雨","雪","阵雪","小雪","中雪","大雪","暴雪","小雪-中雪","中雪-大雪","大雪-暴雪","浮尘","扬沙","沙尘暴","强沙尘暴","龙卷风","雾","浓雾","强浓雾","轻雾","大雾","特强浓雾","热","冷","未知",}; String py[68] = {"qing","shaoyun","qingjianduoyun","duoyun","yin","youfeng","pingjing","weifeng","hefeng","qingfeng","qiangfeng/jinfeng","jifeng","dafeng","liefeng","fengbao","kuangbaofeng","jufeng","redaifengbao","mai","zhongdumai","zhongdumai","yanzhongmai","zhenyu","leizhenyu","leizhenyubingbanyoubingbao","xiaoyu","zhongyu","dayu","baoyu","dabaoyu","tedabaoyu","qiangzhenyu","qiangleizhenyu","jiduanjiangyu","maomaoyu/xiyu","yu","xiaoyu-zhongyu","zhongyu-dayu","dayu-baoyu","baoyu-dabaoyu","dabaoyu-tedabaoyu","yuxuetianqi","yujiaxue","zhenyujiaxue","dongyu","xue","zhenxue","xiaoxue","zhongxue","daxue","baoxue","xiaoxue-zhongxue","zhongxue-daxue","daxue-baoxue","fuchen","yangsha","shachenbao","qiangshachenbao","longjuanfeng","wu","nongwu","qiangnongwu","qingwu","dawu","teqiangnongwu","re","leng","weizhi"}; for(int i=0; i<68; i++){ if(zh_cn[i] == zhtq) return py[i]; } return zhtq; } String windpowerFormat(String wp){ if(wp=="≤3"){ return "<=3"; } return wp; }
void connectWIFI(const char* ssid, const char* password) { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); } }
void NewClient() { uint8_t i=0; if (server.hasClient()) { if (!serverClient || !serverClient.connected()) { if (serverClient) { serverClient.stop(); } serverClient = server.available(); i=1; } if(i==1){ WiFiClient serverClient = server.available(); serverClient.stop(); } } }
void setLedOnOff(WiFiClient serverClient) { if(serverClient.connected()){ digitalWrite(2, LOW); delay(800); } else{ digitalWrite(2, HIGH); delay(400); digitalWrite(2, LOW); delay(400); } }
void receiveDataFromClient(WiFiClient serverClient) { uint8_t buffLen=0; char buff[100];
if (serverClient && serverClient.connected()) {
if(serverClient.available()){ buffLen = serverClient.available(); serverClient.readBytes(buff, buffLen); serverClient.flush(); clientMsg = ""; for(uint8_t i=2; i<buffLen; i++){ clientMsg += (buff[i]); } } } }
void sendData2Client(WiFiClient serverClient, char data[], uint8_t len) { if (serverClient && serverClient.connected()) { serverClient.write(data, len);
delay(200); } }
void getSerialDataAndProcess(){ if (Serial.available()) { size_t countBytes = Serial.available(); char sbuf[countBytes]; Serial.readBytes(sbuf, countBytes);
sendData2Client(serverClient, sbuf, countBytes); Serial.flush(); } }
|