• ESP32_HTTP请求获取天气,含json解析


    1. //安装 ArduinoJson@6.21.3
    2. #include "WiFi.h"
    3. #include "HTTPClient.h"
    4. #include "ArduinoJson.h"
    5. //**********宏、常量********************
    6. //WIFI名和密码
    7. const char *ssid = ""; //SSID
    8. const char *password = ""; //PASSWORD
    9. //心知天气配置
    10. const char *private_key = "SJo8p9FiiIu6gDNz4"; //私钥
    11. const char *city = "guangzhou"; //查询的城市
    12. const char *day = "3"; //查询几日内的天气(免费用户最多可查3日)
    13. //**********全局变量********************
    14. String http_req; //http请求
    15. String http_rsp; //http应答
    16. HTTPClient http_client;
    17. DynamicJsonDocument doc(2048); //json
    18. //***********函数声明*******************
    19. void WIFI_Config(void); //连接WIFI
    20. void set_weather_httpclient(void);//设置获取天气的接口
    21. void anylasize_weather_json(String jsonStr);
    22. //***********初始化*********************
    23. void setup()
    24. {
    25. Serial.begin(115200);
    26. WIFI_Config();
    27. set_weather_httpclient();
    28. }
    29. //***********循环************************
    30. void loop()
    31. {
    32. int http_code = http_client.GET();
    33. if (http_code == HTTP_CODE_OK) {
    34. http_rsp = http_client.getString();
    35. Serial.println(http_rsp); //打印原始的http响应
    36. anylasize_weather_json(http_rsp);
    37. while(1);
    38. }else{
    39. Serial.printf("HTTP get code: %d\n", http_code);
    40. }
    41. delay(5000);
    42. }
    43. //***********函数实现*******************
    44. //连接WIFI
    45. void WIFI_Config()
    46. {
    47. Serial.println("connecting WIFI");
    48. WiFi.begin(ssid, password);
    49. while (!WiFi.isConnected()){ //等待连接
    50. Serial.print(".");
    51. delay(500);
    52. }
    53. Serial.println("Wifi connected\n"); //连接成功
    54. }
    55. //设置获取天气的接口
    56. void set_weather_httpclient()
    57. {
    58. http_req = (String)"https://api.seniverse.com/v3/weather/daily.json?key=" + private_key ;
    59. http_req += (String)"&location=" + city + "&language=en&unit=c&start=0&days=" + day;
    60. Serial.println(http_req);
    61. if (http_client.begin(http_req)){
    62. Serial.println("set_weather_httpclient ok.\n");
    63. }
    64. }
    65. //解析JSON字符串
    66. //{"results":[{"location":{"id":"WS0E9D8WN298","name":"Guangzhou","country":"CN","path":"Guangzhou,Guangzhou,Guangdong,China","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"daily":[{"date":"2023-10-07","text_day":"Overcast","code_day":"9","text_night":"Overcast","code_night":"9","high":"26","low":"20","rainfall":"0.00","precip":"0.00","wind_direction":"N","wind_direction_degree":"2","wind_speed":"21.06","wind_scale":"4","humidity":"73"},{"date":"2023-10-08","text_day":"Overcast","code_day":"9","text_night":"Overcast","code_night":"9","high":"25","low":"21","rainfall":"0.00","precip":"0.00","wind_direction":"NE","wind_direction_degree":"63","wind_speed":"23.94","wind_scale":"4","humidity":"71"},{"date":"2023-10-09","text_day":"Overcast","code_day":"9","text_night":"Overcast","code_night":"9","high":"26","low":"23","rainfall":"0.00","precip":"0.00","wind_direction":"NE","wind_direction_degree":"30","wind_speed":"13.46","wind_scale":"3","humidity":"71"}],"last_update":"2023-10-07T20:00:00+08:00"}]}
    67. void anylasize_weather_json(String jsonStr)
    68. {
    69. int index = jsonStr.indexOf('{'); //找到json的{
    70. jsonStr = jsonStr.substring(index);
    71. deserializeJson(doc, jsonStr); //字符串解析成json格式
    72. JsonObject root = doc.as(); //doc转换成的JsonObject对象root
    73. JsonArray results = root["results"]; //JsonObject对象root里有JsonArray数组results
    74. JsonObject location = results[0]["location"]; //results[0]中的子JsonObject对象location
    75. const char *name = location["name"]; //JsonObject对象location中取出name(城市名) Guangzhou
    76. Serial.println(name);
    77. JsonArray daily = results[0]["daily"]; //results[0]中的JsonArray数组daily
    78. const char *date = daily[0]["date"]; //JsonArray数组daily中[0]的“date”(日期) 2023-10-07
    79. Serial.println(date);
    80. const char *weather = daily[0]["text_day"]; //JsonArray数组daily中[0]的“text_day”(天气) Overcast
    81. Serial.println(weather);
    82. const char *low = daily[0]["low"]; //JsonArray数组daily中[0]的最低和最高气温 temper is 20~26 deg
    83. const char *high = daily[0]["high"];
    84. Serial.println((String)"temper is "+low+"~"+high+" deg");
    85. }

    参考:ESP32_HTTP请求获取天气,含json解析_esp32 心知天气_PWRJOY的博客-CSDN博客

  • 相关阅读:
    web前端期末大作业——基于HTML+CSS+JavaScript实现中国茶文化(30页)
    自动推理的逻辑05--谓词演算
    Echarts 教程三
    代码随想录1刷—单调栈篇
    百度地图1
    基于OpenCV的轮廓检测(2)
    Spring核心与设计思想
    58欧氏空间05——实对称矩阵的正交相似对角化
    图文并茂 —— 插入排序,希尔排序
    vue2踩坑之项目:Swiper轮播图使用
  • 原文地址:https://blog.csdn.net/weixin_42854045/article/details/133656320