合宙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了。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值