ESP32 arduino获取心知天气

首先注册获取心智天气密钥:

这里采用试用版,一共有1000次访问量,需要的天气数据更完整,免费版访问量不限,但是天气数据只能获取天气,温度等,数据不齐全。

在arduino安装开发板ESP32固件库和ArduinoJson数据解析库
在这里插入图片描述
这里可以选择离线安装:https://github.com/espressif/arduinoesp32/releases/download/1.0.4/esp32-1.0.4.zip
将安装包解压在:
在这里插入图片描述

在这里插入图片描述
最后新建代码:
将WIFI名称和密码改成自己的,
再将天气密钥改成自己的,以及城市改成自己的就可以了,其他无需修改。

#include <WiFi.h>  //esp32_1.04版本aduino库
#include <ArduinoJson.h>
#include <HTTPClient.h>

const char * ID = "*****";         //WIFI名称
const char * PASSWORD = "*******";//WIFI密码
 
//天气相关参数
String API = "********";    //心知天气密钥
String WeatherURL = "";
String CITY = "武汉";                 //城市
String url_xinzhi = "";
String Weather = "0";                //天气
String Time = "no_init";             // 当前API最近更新时间
int Temp = 999;                      // 实况温度
int Feel = 999;                      // 实况体感温度
int icon= 999;                       // 当前天气状况和图标的代码
String fengxiang = "no_init";        // 实况风向
int fengli = 9;                    // 实况风力等级1-12
float Humidity = 99;                 // 实况相对湿度百分比数值0~99%
int Pressure = 99999;                // 大气压/百帕mb

String tq = "0";    //天气
String fx = "0";    //风向

String WEA="";

long sum = 0;
 
/* 创建实例 */
 
HTTPClient http;


String GitURL(String api,String city)
{
  url_xinzhi =  "https://api.seniverse.com/v3/weather/now.json?key=";//心知天气API接口网址
  url_xinzhi += api;
  url_xinzhi += "&location=";
  url_xinzhi += city;
  url_xinzhi += "&language=zh-Hans&unit=c";
  return url_xinzhi;
}
 
void ParseWeather(String url)
{  
  DynamicJsonDocument doc(1024); //分配内存,动态
  http.begin(url);
 
  int httpGet = http.GET();
  if(httpGet > 0)
  {
    //Serial.printf("HTTPGET is %d",httpGet);
    
    if(httpGet == HTTP_CODE_OK)
    {
      String json = http.getString();
      //Serial.println(json);

      
      
      deserializeJson(doc, json);
      
      
      Weather = doc["results"][0]["now"]["text"].as<String>();               //天气
      Time = doc["results"][0]["last_update"].as<String>();                  // 当前API最近更新时间
      Temp = doc["results"][0]["now"]["temperature"].as<int>();              // 实况温度
      Feel = doc["results"][0]["now"]["feels_like"].as<int>();               // 实况体感温度
      fengxiang = doc["results"][0]["now"]["wind_direction"].as<String>();   // 实况风向
      fengli = doc["results"][0]["now"]["wind_scale"].as<int>();             // 实况风力等级
      Humidity = doc["results"][0]["now"]["humidity"].as<int>();             // 实况相对湿度百分比数值
      Pressure = doc["results"][0]["now"]["pressure"].as<int>();             // 大气压
    
    }
    else
    {
      Serial.printf("ERROR1!!");
    }
  }
  else
  {
    Serial.printf("ERROR2!!");
  }
  http.end();
}
 
void setup() 
{
  Serial.begin(9600);
 
  //==================wifi连接==================
  Serial.println();
  Serial.print("WiFi:");
  Serial.println(ID);
  Serial.print("PASSWORLD:");
  Serial.println(PASSWORD);
  
  WiFi.begin(ID,PASSWORD);
  
  while(WiFi.status()!=WL_CONNECTED)
  {
    delay(500);
    Serial.println("正在连接...");
  }
 
  Serial.println("连接成功!");
  //==================wifi连接==================
  
  WeatherURL = GitURL(API,CITY); 
  
}


//天气解析
void jiexi()
{
  if(Weather=="晴")
    {
      tq="01"; // 获取实况天气状况的文字描述
    }
    else if(Weather=="多云")
    {
      tq="02"; // 获取实况天气状况的文字描述
    }
    else if(Weather=="阴")
    {
      tq="03"; // 获取实况天气状况的文字描述
    }
    else if(Weather=="小雨")
    {
      tq="04"; // 获取实况天气状况的文字描述
    }
    else if(Weather=="中雨")
    {
      tq="05"; // 获取实况天气状况的文字描述
    }
    else if(Weather=="大雨")
    {
      tq="06"; // 获取实况天气状况的文字描述
    }
    else if(Weather=="暴雨")
    {
      tq="07"; // 获取实况天气状况的文字描述
    }
    else if(Weather=="小雪")
    {
      tq="08"; // 获取实况天气状况的文字描述
    }
    else if(Weather=="大雪")
    {
      tq="09"; // 获取实况天气状况的文字描述
    }

    
    //风向解析
    if(fengxiang=="东南风")
    {
      fx="01";     // 获取实况风向
    }
    else if(fengxiang=="东北风")
    {
      fx="02";     // 获取实况风向  
    }
    else if(fengxiang=="西南风")
    {
      fx="03";     // 获取实况风向  
    }
    else if(fengxiang=="西北风")
    {
      fx="04";     // 获取实况风向  
    }
    else if(fengxiang=="东风")
    {
      fx="05";     // 获取实况风向  
    }
    else if(fengxiang=="西风")
    {
      fx="06";     // 获取实况风向  
    }
    else if(fengxiang=="南风")
    {
      fx="07";     // 获取实况风向  
    }
    else if(fengxiang=="北风")
    {
      fx="08";    // 获取实况风向  
    }

  
}
 
void loop() 
{
  //Serial.println("连接成功!");
  
  ParseWeather(WeatherURL);

  jiexi();
  WEA.concat("$:");
  WEA.concat(Time);                         //时间
  WEA.concat("$:");      
  WEA.concat(Weather);                      //天气                      
  WEA.concat("$:");
  WEA.concat(Temp);                         //温度
  WEA.concat("$:");
  WEA.concat(Feel);                         //体感
  WEA.concat("$:");
  WEA.concat(fengxiang);                    //风向
  WEA.concat("$:");
  WEA.concat(fengli);                       //风力
  WEA.concat("$:");
  WEA.concat(Humidity);                     //湿度
  WEA.concat("$:");
  WEA.concat(Pressure);                     //压力
  Serial.println(WEA);
  WEA="";                                   //清空
  //Serial.print("执行次数: ");
  //Serial.println(sum);
  sum ++;
  delay(60000);  
}

心知天气API接口提供以下数据信息;
在这里插入图片描述
你可以修改代码中

	  Weather = doc["results"][0]["now"]["text"].as<String>();               //天气
      Time = doc["results"][0]["last_update"].as<String>();                  // 当前API最近更新时间
      Temp = doc["results"][0]["now"]["temperature"].as<int>();              // 实况温度
      Feel = doc["results"][0]["now"]["feels_like"].as<int>();               // 实况体感温度
      fengxiang = doc["results"][0]["now"]["wind_direction"].as<String>();   // 实况风向
      fengli = doc["results"][0]["now"]["wind_scale"].as<int>();             // 实况风力等级
      Humidity = doc["results"][0]["now"]["humidity"].as<int>();             // 实况相对湿度百分比数值
      Pressure = doc["results"][0]["now"]["pressure"].as<int>();             // 大气压

其中下载端口设置:
在这里插入图片描述

编译下载,完成,串口打印,波特率为9600

在这里插入图片描述

  • 6
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
你可以使用 ESP8266 来获取心知天气的数据。以下是一些步骤供参考: 1. 首先,你需要在心知天气官网上注册并获取 API 密钥。这个密钥将用于访问心知天气的 API 接口。 2. 确保你的 ESP8266 设备已经连接到互联网,并且你已经在 Arduino IDE 中安装了 ESP8266 的开发环境。 3. 在 Arduino IDE 中创建一个新的项目,并添加 ESP8266 相关的库。 4. 在代码中引入必要的库,并定义你的 Wi-Fi SSID 和密码,以及心知天气的 API 密钥。 ```cpp #include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include <ArduinoJson.h> const char* ssid = "YourWiFiSSID"; const char* password = "YourWiFiPassword"; const char* apiKey = "YourHeartknowWeatherAPIKey"; ``` 5. 在 `setup()` 函数中,连接到 Wi-Fi 网络。 ```cpp void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } ``` 6. 在 `loop()` 函数中,使用 HTTPClient 库发送 GET 请求获取心知天气的数据。 ```cpp void loop() { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; String url = "https://api.seniverse.com/v3/weather/now.json?key=" + String(apiKey) + "&location=your_location"; http.begin(url); int httpCode = http.GET(); if (httpCode > 0) { if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); // 在这里解析返回的 JSON 数据并提取相应的天气信息 // 使用 ArduinoJson 库进行 JSON 解析 StaticJsonDocument<200> doc; deserializeJson(doc, payload); JsonObject weather = doc["results"][0]["now"]; String weatherText = weather["text"]; int temperature = weather["temperature"]; Serial.print("Weather: "); Serial.println(weatherText); Serial.print("Temperature: "); Serial.println(temperature); } } else { Serial.println("Error on HTTP request"); } http.end(); delay(60000); // 每隔一分钟获取一次天气数据 } } ``` 请注意,上述代码中的 `your_location` 应替换为你想要获取天气信息的目标位置。 这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Richer Fan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值