



3.API。有了這個私鑰,我們就可以通過API來獲取天氣預報了,相關的API說明可以在這里查詢:
https://seniverse.yuque.com/books/share/f4f9bf1a-d3d9-4a68-8996-950f8c88400e/sl6gvt#d29hl
先用網絡調試助手測試一下:

void Weather_JSON( ){char *data;cJSON *root;cJSON *results;cJSON *last_update;cJSON *loc_json, *daily_json;cJSON *forecast_json;char *loc_tmp, *weather_tmp;int i = 0;data = (char*)(Uart1.RxBuf);//接受到的數據root = cJSON_Parse(data);if(root){//printf("JSON格式正確:\n%s\n\n",cJSON_Print(root)); //輸出json字符串results = cJSON_GetObjectItem(root, "results");results = cJSON_GetArrayItem(results,0);if(results){loc_json = cJSON_GetObjectItem(results, "location"); //得到location鍵對應的值,是一個對象loc_tmp = cJSON_GetObjectItem(loc_json, "id") -> valuestring;//printf("城市ID:%s\n",loc_tmp);loc_tmp = cJSON_GetObjectItem(loc_json, "name") -> valuestring;memset(loc_str,0,20);memcpy(loc_str,loc_tmp,strlen(loc_tmp));loc_tmp = cJSON_GetObjectItem(loc_json, "timezone") -> valuestring;//printf("城市時區:%s\n\n",loc_tmp);daily_json = cJSON_GetObjectItem(results, "daily");if(daily_json){Weather_Dat[0].Flag = 1;for(i = 0; i < 3; i++){forecast_json = cJSON_GetArrayItem(daily_json, i);//weather_tmp = cJSON_GetObjectItem(forecast_json, "date") -> valuestring;//日期weather_tmp = cJSON_GetObjectItem(forecast_json, "code_day") -> valuestring;//白天天氣代碼Weather_Dat[i].Weathcode = atoi(weather_tmp);//weather_tmp = cJSON_GetObjectItem(forecast_json, "code_night") -> valuestring;//晚上天氣代碼weather_tmp = cJSON_GetObjectItem(forecast_json, "high") -> valuestring;//最高溫度memset(Weather_Dat[i].HighT,0,4);memcpy(Weather_Dat[i].HighT,weather_tmp,strlen(weather_tmp));weather_tmp = cJSON_GetObjectItem(forecast_json, "low") -> valuestring;//最低溫度memset(Weather_Dat[i].LowT,0,4);memcpy(Weather_Dat[i].LowT,weather_tmp,strlen(weather_tmp));weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_direction") -> valuestring;//風向memset(Weather_Dat[i].Wind_Dir,0,10);memcpy(Weather_Dat[i].Wind_Dir,weather_tmp,strlen(weather_tmp));weather_tmp = cJSON_GetObjectItem(forecast_json, "wind_scale") -> valuestring;//風力memset(Weather_Dat[i].WindScale,0,4);memcpy(Weather_Dat[i].WindScale,weather_tmp,strlen(weather_tmp));weather_tmp = cJSON_GetObjectItem(forecast_json, "humidity") -> valuestring;//濕度memset(Weather_Dat[i].Humi,0,4);memcpy(Weather_Dat[i].Humi,weather_tmp,strlen(weather_tmp));}}else{//printf("daily json格式錯誤\r\n");Weather_Dat[0].Flag = 0;????????????}}else{Weather_Dat[0].Flag = 0;//printf("results格式錯誤:%s\r\n", cJSON_GetErrorPtr());}}else{Weather_Dat[0].Flag = 0;//printf("JSON格式錯誤\r\n");}cJSON_Delete(root);}
總結一下,獲取天氣預報的步驟如下:
1.注冊賬號。
2.獲取私鑰。
3.連接服務器。
????AT+CIPSTART="TCP”,” api.seniverse.com”,80
4..打開傳透模式。
??? AT+CIPSEND
5..收到‘>’符號后,發送GET請求
GET https://api.seniverse.com/v3/weather/daily.json?key=your_key&location=ip&language=zh-Hans&unit=c&start=0&days=3
6.等待接收數據,并解析。
推薦閱讀: