合宙esp32c3,通过心知天气获取实时天气

文章是借鉴了下面这篇文章,并对内容做了一些整合。
心知天气天气状况获取,ESP32获取天气信息(含源码)
如何申请和基本使用可以看上面这个链接的文章。

CPP文件

#include "xzweather.h"

// 创建实例
HTTPClient http;

XzWearther::XzWearther(String api_key, String location)
  : _api_key(api_key), _location(location) {}

uint8_t XzWearther::parseWeather() {
  _url = "https://api.seniverse.com/v3/weather/now.json?key=" + _api_key + "&location=" + _location + "&language=zh-Hans&unit=c";
  DynamicJsonDocument doc(1024);  //分配内存,动态
  http.begin(_url);
  int httpGet = http.GET();
  if (httpGet > 0) {
    if (httpGet == HTTP_CODE_OK) {
      String json = http.getString();
      deserializeJson(doc, json);
      _location_id = doc["results"][0]["location"]["id"].as<String>();
      _location_name = doc["results"][0]["location"]["name"].as<String>();
      _location_country = doc["results"][0]["location"]["country"].as<String>();
      _location_path = doc["results"][0]["location"]["path"].as<String>();
      _location_timezone = doc["results"][0]["location"]["timezone"].as<String>();
      _location_timezone_offset = doc["results"][0]["location"]["timezone_offset"].as<String>();

      _now_text = doc["results"][0]["now"]["text"].as<String>();
      _now_code = doc["results"][0]["now"]["code"].as<String>();
      _now_temperature = doc["results"][0]["now"]["temperature"].as<String>();
    } else {
      return 1;//不等于0的返回值都可以代表一种错误,自己定义就好了
    }
  } else {
    return 2;
  }
  http.end();
  return 0;//返回0代表正确
}

void XzWearther::setLocation(String location) {
  _location = location;
}

String XzWearther::getLocationId() {
  return _location_id;
}

String XzWearther::getLocationName() {
  return _location_name;
}

String XzWearther::getLocationCountry() {
  return _location_country;
}

String XzWearther::getLocationPath() {
  return _location_path;
}

String XzWearther::getLocationtimezone() {
  return _location_timezone;
}

String XzWearther::getLocationTimezoneOffset() {
  return _location_timezone_offset;
}

String XzWearther::getNowText() {
  return _now_text;
}

String XzWearther::getNowCode() {
  return _now_code;
}

String XzWearther::getNowTemperature() {
  return _now_temperature;
}

H文件

#ifndef XZWEARHER_H
#define XZWEARHER_H

#include <Arduino.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>

class XzWearther {
public:
  XzWearther(String api_key, String location);
  
  void setLocation(String location);

  String getLocationId();
  String getLocationName();
  String getLocationCountry();
  String getLocationPath();
  String getLocationtimezone();
  String getLocationTimezoneOffset();

  String getNowText();
  String getNowCode();
  String getNowTemperature();

  uint8_t parseWeather();
private:
  String _api_key;
  String _location;
  String _url;

  String _location_id;
  String _location_name;
  String _location_country;
  String _location_path;
  String _location_timezone;
  String _location_timezone_offset;

  String _now_text;
  String _now_code;
  String _now_temperature;
};

#endif

调用

#include <WiFi.h>
#include "xzweather.h"

const char *ssid = "你的wifi名称";
const char *password = "你的wifi密码";

XzWearther weather("这里输入你自己的密钥", "合肥");//创建类

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println();
  // pinMode(12, OUTPUT);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("连接成功,3秒后将打印IP");
  delay(3000);

  Serial.print("IP address:");
  Serial.println(WiFi.localIP());

  weather.parseWeather();//发送获取天气的命令
  Serial.println(weather.getNowText());//打印天气的参数
  Serial.println(weather.getNowTemperature());//打印温度的参数
}

void loop() {
  // put your main code here, to run repeatedly:
}

  • 在使用时主要获得的参数是_now_text和_now_code。其中_now_code要参照官方的api,不同的数值代表不同的天气状态。
  • 调用时需要先输入密钥和地址。

使用

使用H和CPP文件是,需要把文件放到.ino文件同级目录下,就可以使用了。
顺带说一句,arduino不支持c语言,只支持c++,所以CPP文件的后缀不要写成.c了。

posted @   一叁五  阅读(133)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示