esp32联网获取时间和天气(四)

说明

本章节需要先学习之前(三)中获取当前时间方法,本文基于platformIO,需提前安装timelib库,可以参考之前(三)

代码

代码如下,需要一点http知识,可以自行百度

#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino.h>
#include <TimeLib.h>
#include <WiFiUdp.h>

void getCityCode(); // 发送HTTP请求并且将服务器响应通过串口输出
void getCityWeater();
time_t getNtpTime();
void sendNTPpacket(IPAddress &address);
/* *****************************************************************
 *  参数设置
 * *****************************************************************/
// NTP Servers:
static const char ntpServerName[] = "ntp6.aliyun.com"; // 阿里云的时间服务器
/*              NTP设置                 */
const int NTP_PACKET_SIZE = 48;     // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming & outgoing packets
const int timeZone = 8;             // 时区

WiFiUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
struct config_type
{
    char stassid[32]; // 定义配网得到的WIFI名长度(最大32字节)
    char stapsw[64];  // 定义配网得到的WIFI密码长度(最大64字节)
};
config_type wificonf = {{"PDCN"}, {"1234567890"}};

unsigned int updateweater_time = 30000;
struct Weather_Msg
{
    String cityDZ;
    String dataSK;
    String dataFC;
};
Weather_Msg weather_msg = {{""}, {""}, {""}};

uint32_t targetTime = 0;
String cityCode = "101190402"; // 天气城市代码
WiFiClient wificlient;
// 发送HTTP请求并且将服务器响应通过串口输出
void getCityCode()
{
    String URL = "http://wgeo.weather.com.cn/ip/?_=" + String(now());
    // 创建 HTTPClient 对象
    HTTPClient httpClient;

    // 配置请求地址。此处也可以不使用端口号和PATH而单纯的
    httpClient.begin(wificlient, URL);

    // 设置请求头中的User-Agent
    httpClient.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
    httpClient.addHeader("Referer", "http://www.weather.com.cn/");

    // 启动连接并发送HTTP请求
    int httpCode = httpClient.GET();
    Serial.print("Send GET request to URL: ");
    Serial.println(URL);

    // 如果服务器响应OK则从服务器获取响应体信息并通过串口输出
    if (httpCode == HTTP_CODE_OK)
    {
        String str = httpClient.getString();
        Serial.println("getCityCode中返回值:\n");
        Serial.println(str);
        int aa = str.indexOf("id=");
        if (aa > -1)
        {
            // cityCode = str.substring(aa+4,aa+4+9).toInt();
            cityCode = str.substring(aa + 4, aa + 4 + 9);
            Serial.println(cityCode);
        }
        else
        {
            Serial.println("获取城市代码失败");
        }
    }
    else
    {
        Serial.println("请求城市代码错误:");
        Serial.println(httpCode);
    }

    // 关闭ESP8266与服务器连接
    httpClient.end();
}

// 获取城市天气
void getCityWeater()
{
    // String URL = "http://d1.weather.com.cn/dingzhi/" + cityCode + ".html?_="+String(now());//新
    String URL = "http://d1.weather.com.cn/weather_index/" + cityCode + ".html?_=" + String(now()); // 原来
    // 创建 HTTPClient 对象
    HTTPClient httpClient;

    // httpClient.begin(URL);
    httpClient.begin(wificlient, URL); // 使用新方法

    // 设置请求头中的User-Agent
    httpClient.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
    httpClient.addHeader("Referer", "http://www.weather.com.cn/");

    // 启动连接并发送HTTP请求
    int httpCode = httpClient.GET();
    Serial.println("正在获取天气数据");
    Serial.println(URL);

    // 如果服务器响应OK则从服务器获取响应体信息并通过串口输出
    if (httpCode == HTTP_CODE_OK)
    {

        String str = httpClient.getString();
        Serial.println(str);
        // Serial.println("httpdata=" + str);
        int indexStart = str.indexOf("weatherinfo\":");
        int indexEnd = str.indexOf("};var alarmDZ");

        weather_msg.cityDZ = str.substring(indexStart + 13, indexEnd);
        // Serial.println(jsonCityDZ);

        indexStart = str.indexOf("dataSK =");
        indexEnd = str.indexOf(";var dataZS");
        weather_msg.dataSK = str.substring(indexStart + 8, indexEnd);
        // Serial.println(jsonDataSK);

        indexStart = str.indexOf("\"f\":[");
        indexEnd = str.indexOf(",{\"fa");
        weather_msg.dataFC = str.substring(indexStart + 5, indexEnd);
        // Serial.println(jsonFC);

        // weaterData(&jsonCityDZ, &jsonDataSK, &jsonFC);

        Serial.println("获取成功");
        Serial.println(weather_msg.cityDZ);
        Serial.println(weather_msg.dataSK);
        Serial.println(weather_msg.dataFC);
    }
    else
    {
        Serial.println("请求城市天气错误:");
        Serial.print(httpCode);
    }

    // 关闭ESP8266与服务器连接
    httpClient.end();
}
void setup()
{

    Serial.begin(115200);

    Serial.print("正在连接WIFI ");
    Serial.println(wificonf.stassid);
    WiFi.begin(wificonf.stassid, wificonf.stapsw);

    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
    Serial.print("本地IP: ");
    Serial.println(WiFi.localIP());
    Serial.println(Udp.remotePort());
    Serial.println("waiting for sync");
    setSyncProvider(getNtpTime);
    setSyncInterval(300);
}
void loop()
{
    getCityCode();
    getCityWeater();
    delay(60000);
}

time_t getNtpTime()
{
    IPAddress ntpServerIP; // NTP server's ip address

    while (Udp.parsePacket() > 0)
        ; // discard any previously received packets
    Serial.println("Transmit NTP Request");
    // get a random server from the pool
    WiFi.hostByName(ntpServerName, ntpServerIP);
    Serial.print(ntpServerName);
    Serial.print(": ");
    Serial.println(ntpServerIP);
    sendNTPpacket(ntpServerIP);
    uint32_t beginWait = millis();
    while (millis() - beginWait < 1500)
    {
        int size = Udp.parsePacket();
        if (size >= NTP_PACKET_SIZE)
        {
            Serial.println("Receive NTP Response");
            Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
            unsigned long secsSince1900;
            // convert four bytes starting at location 40 to a long integer
            secsSince1900 = (unsigned long)packetBuffer[40] << 24;
            secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
            secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
            secsSince1900 |= (unsigned long)packetBuffer[43];
            return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
        }
    }
    Serial.println("No NTP Response :-(");
    return 0; // return 0 if unable to get the time
}

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
    // set all bytes in the buffer to 0
    memset(packetBuffer, 0, NTP_PACKET_SIZE);
    // Initialize values needed to form NTP request
    // (see URL above for details on the packets)
    packetBuffer[0] = 0b11100011; // LI, Version, Mode
    packetBuffer[1] = 0;          // Stratum, or type of clock
    packetBuffer[2] = 6;          // Polling Interval
    packetBuffer[3] = 0xEC;       // Peer Clock Precision
    // 8 bytes of zero for Root Delay & Root Dispersion
    packetBuffer[12] = 49;
    packetBuffer[13] = 0x4E;
    packetBuffer[14] = 49;
    packetBuffer[15] = 52;
    // all NTP fields have been given values, now
    // you can send a packet requesting a timestamp:
    Udp.beginPacket(address, 123); // NTP requests are to port 123
    Udp.write(packetBuffer, NTP_PACKET_SIZE);
    Udp.endPacket();
}

实验结果如下,返回json格式,之后可以通过ArduinoJson库来解析需要的键值对。

获取成功
{"city":"常熟","cityname":"changshu","temp":"999","tempn":"18","weather":"阴","wd":"北风转东南风","ws":"3-4级转<3级","weathercode":"d2","weathercoden":"n2","fctime":"202404150800"}
{"nameen":"changshu","cityname":"常熟","city":"101190402","temp":"21.4","tempf":"70.5","WD":"东风","wde":"E","WS":"2级","wse":"8km\/h","SD":"73%","sd":"73%","qy":"1011","njd":"10km","time":"20:05","rain":"0","rain24h":"0","aqi":"53","aqi_pm25":"53","weather":"多云","weathere":"Cloudy","weathercode":"d01","limitnumber":"","date":"04月15日(星期一)"}
{"fa":"02","fb":"02","fc":"26","fd":"16","fe":"北风","ff":"东南风","fg":"3-4级","fh":"<3级","fk":"8","fl":"3","fm":"999.9","fn":"82.5","fi":"4\/15","fj":"今天"}

<think>好的,用户想用ESP32-CAM通过网络获取指定地址的天气信息,需要一个API示例。首先,我需要确定ESP32-CAM的网络功能,它支持Wi-Fi,所以可以使用HTTP客户端库来请求API。然后,天气API的选择很重要,常见的有OpenWeatherMap、天气等,用户可能需要注册获取API密钥。接下来要考虑如何解析返回的数据,通常是JSON格式,Arduino库中有ArduinoJson可以使用。此外,用户可能需要处理HTTPS的问题,因为有些API需要SSL,但ESP32的库可能支持。需要提醒用户注意API的请求频率限制密钥的安全存储。最后,示例代码的结构应包括连接Wi-Fi、发送HTTP请求、解析响应这几个部分。可能还需要处理错误情况,比如网络连接失败或JSON解析错误。需要确保代码中的占位符如API密钥城市名称被替换。同时,用户可能需要配置摄像头网络同时工作,但ESP32-CAM的内存有限,可能需要优化代码。另外,天气API的免费套餐可能有调用次数限制,需要提醒用户注意。</think>### ESP32-CAM获取天气信息实现方法 ESP32-CAM通过HTTP请求获取天气信息需要以下关键步骤: 1. **选择天气API服务** 推荐使用天气(免费版每日1000次调用)或OpenWeatherMap(免费版每分钟60次调用)。以下示例使用天气API: ```arduino const char* weatherAPI = "https://devapi.qweather.com/v7/weather/now?location=101010100&key=你的API密钥"; ``` 2. **配置Wi-Fi连接** ```arduino #include <WiFi.h> const char* ssid = "你的WiFi名称"; const char* password = "你的WiFi密码"; void setup() { WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } } ``` 3. **发送HTTP请求** 使用ESP32内置的HTTPClient库: ```arduino #include <HTTPClient.h> String getWeatherData() { HTTPClient http; http.begin(weatherAPI); int httpCode = http.GET(); if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); http.end(); return payload; } http.end(); return ""; } ``` 4. **解析JSON响应** 使用ArduinoJson库(需要安装v6版本): ```arduino #include <ArduinoJson.h> void parseWeather(String json){ DynamicJsonDocument doc(1024); deserializeJson(doc, json); float temp = doc["now"]["temp"]; String cond = doc["now"]["text"].as<String>(); Serial.printf("当前温度:%.1f℃ 天气状况:%s\n", temp, cond.c_str()); } ``` **完整示例流程**: ```arduino void loop() { if (WiFi.status() == WL_CONNECTED) { String response = getWeatherData(); if (response != "") { parseWeather(response); } } delay(300000); // 5分钟更新一次 } ``` **注意事项**: 1. 需要先在[天气控制台](https://dev.qweather.com/)注册获取API密钥 2. 城市代码查询接口:`https://geoapi.qweather.com/v2/city/lookup?location=北京&key=你的密钥` 3. 建议添加错误重试机制看门狗定时器 4. ESP32-CAM内存有限,建议设置ArduinoJson文档大小为实际数据大小的1.5倍
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值