



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.等待接收数据,并解析。
推荐阅读: