利用Arduino Esp8266 心知天气API 获取天气预报信息(修改后可以DIY一个小型的桌面气象台)

前期准备:

注册心知天气,获取API密钥  https://www.seniverse.com

生成API请求地址,北京今天和未来4天的预报请求地址如下:

https://api.seniverse.com/v3/weather/daily.json?key=your_API
_key&location=beijing&language=zh-Hans&unit=c&start=0&days=5

需要的库:

// Libraries
#include
#include  

ArduinoJson提供的在线助手很好用,帮助生成很多解析代码。https://arduinojson.org/v5/assistant/

程序流程如下:

利用Arduino Esp8266 心知天气API 获取天气预报信息(修改后可以DIY一个小型的桌面气象台)_第1张图片

代码如下:

// Libraries
#include 
#include  //使用ArduinoJson库
// WiFi settings
const char* ssid     = "***";
const char* password = "***";
// API server
const char* host = "api.seniverse.com";
void setup() {
  // Serial
  Serial.begin(115200);
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to WIFI");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void loop() {

  // Connect to API
  Serial.print("connecting to XinZhi");
  Serial.println(host);
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  // We now create a URI for the request
  String url = "/v3/weather/daily.json?key=your-key&location=shanghai&language=zh-Hans&unit=c&start=0&days=3";
  Serial.print("Requesting URL: ");
  Serial.println(url);
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(100);
  // Read all the lines of the reply from server and print them to Serial
  String weather_data;
  while (client.available()) {
    String line = client.readStringUntil('\r');
    weather_data += line;
  }

  client.stop();
  Serial.println();
  Serial.println("closing connection");

  // Process weather_json
  Serial.println();
  //Serial.println("weather_data: ");
  // Serial.println(weather_data);

  // Convert to JSON
  String json_weather_data;
  int jsonIndex;

  for (int i = 0; i < weather_data.length(); i++) {
    if (weather_data[i] == '{') {
      jsonIndex = i;
      break;
    }
  }

  // Get JSON data
  json_weather_data = weather_data.substring(jsonIndex);
  Serial.println();
  Serial.println("json_weather_data: ");
  Serial.println(json_weather_data);


  //利用arduinoJson库解析心知返回的json天气数据
  //可以利用 https://arduinojson.org/v5/assistant/ Arduinojson助手生成相关json解析代码  很方便!!!
  const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 3 * JSON_OBJECT_SIZE(12) + 700;
  DynamicJsonBuffer jsonBuffer(capacity);

  const char* json = "{\"results\":[{\"location\":{\"id\":\"WTW3SJ5ZBJUY\",\"name\":\"上海\",\"country\":\"CN\",\"path\":\"上海,上海,中国\",\"timezone\":\"Asia/Shanghai\",\"timezone_offset\":\"+08:00\"},\"daily\":[{\"date\":\"2019-02-11\",\"text_day\":\"多云\",\"code_day\":\"4\",\"text_night\":\"阴\",\"code_night\":\"9\",\"high\":\"7\",\"low\":\"2\",\"precip\":\"\",\"wind_direction\":\"东北\",\"wind_direction_degree\":\"45\",\"wind_speed\":\"10\",\"wind_scale\":\"2\"},{\"date\":\"2019-02-12\",\"text_day\":\"中雨\",\"code_day\":\"14\",\"text_night\":\"小雨\",\"code_night\":\"13\",\"high\":\"9\",\"low\":\"4\",\"precip\":\"\",\"wind_direction\":\"东\",\"wind_direction_degree\":\"90\",\"wind_speed\":\"10\",\"wind_scale\":\"2\"},{\"date\":\"2019-02-13\",\"text_day\":\"小雨\",\"code_day\":\"13\",\"text_night\":\"中雨\",\"code_night\":\"14\",\"high\":\"10\",\"low\":\"7\",\"precip\":\"\",\"wind_direction\":\"东北\",\"wind_direction_degree\":\"45\",\"wind_speed\":\"10\",\"wind_scale\":\"2\"}],\"last_update\":\"2019-02-11T08:00:00+08:00\"}]}";

  JsonObject& root = jsonBuffer.parseObject(json_weather_data);

  JsonObject& results_0 = root["results"][0];

  JsonObject& results_0_location = results_0["location"];
  const char* results_0_location_id = results_0_location["id"]; // "WTW3SJ5ZBJUY"
  const char* results_0_location_name = results_0_location["name"]; // "上海"
  const char* results_0_location_country = results_0_location["country"]; // "CN"
  const char* results_0_location_path = results_0_location["path"]; // "上海,上海,中国"
  const char* results_0_location_timezone = results_0_location["timezone"]; // "Asia/Shanghai"
  const char* results_0_location_timezone_offset = results_0_location["timezone_offset"]; // "+08:00"

  JsonArray& results_0_daily = results_0["daily"];

  JsonObject& results_0_daily_0 = results_0_daily[0];
  const char* results_0_daily_0_date = results_0_daily_0["date"]; // "2019-02-11"
  const char* results_0_daily_0_text_day = results_0_daily_0["text_day"]; // "多云"
  const char* results_0_daily_0_code_day = results_0_daily_0["code_day"]; // "4"
  const char* results_0_daily_0_text_night = results_0_daily_0["text_night"]; // "阴"
  const char* results_0_daily_0_code_night = results_0_daily_0["code_night"]; // "9"
  const char* results_0_daily_0_high = results_0_daily_0["high"]; // "7"
  const char* results_0_daily_0_low = results_0_daily_0["low"]; // "2"
  const char* results_0_daily_0_precip = results_0_daily_0["precip"]; // ""
  const char* results_0_daily_0_wind_direction = results_0_daily_0["wind_direction"]; // "东北"
  const char* results_0_daily_0_wind_direction_degree = results_0_daily_0["wind_direction_degree"]; // "45"
  const char* results_0_daily_0_wind_speed = results_0_daily_0["wind_speed"]; // "10"
  const char* results_0_daily_0_wind_scale = results_0_daily_0["wind_scale"]; // "2"
  JsonObject& results_0_daily_1 = results_0_daily[1];
  const char* results_0_daily_1_date = results_0_daily_1["date"]; // "2019-02-12"
  const char* results_0_daily_1_text_day = results_0_daily_1["text_day"]; // "中雨"
  const char* results_0_daily_1_code_day = results_0_daily_1["code_day"]; // "14"
  const char* results_0_daily_1_text_night = results_0_daily_1["text_night"]; // "小雨"
  const char* results_0_daily_1_code_night = results_0_daily_1["code_night"]; // "13"
  const char* results_0_daily_1_high = results_0_daily_1["high"]; // "9"
  const char* results_0_daily_1_low = results_0_daily_1["low"]; // "4"
  const char* results_0_daily_1_precip = results_0_daily_1["precip"]; // ""
  const char* results_0_daily_1_wind_direction = results_0_daily_1["wind_direction"]; // "东"
  const char* results_0_daily_1_wind_direction_degree = results_0_daily_1["wind_direction_degree"]; // "90"
  const char* results_0_daily_1_wind_speed = results_0_daily_1["wind_speed"]; // "10"
  const char* results_0_daily_1_wind_scale = results_0_daily_1["wind_scale"]; // "2"
  JsonObject& results_0_daily_2 = results_0_daily[2];
  const char* results_0_daily_2_date = results_0_daily_2["date"]; // "2019-02-13"
  const char* results_0_daily_2_text_day = results_0_daily_2["text_day"]; // "小雨"
  const char* results_0_daily_2_code_day = results_0_daily_2["code_day"]; // "13"
  const char* results_0_daily_2_text_night = results_0_daily_2["text_night"]; // "中雨"
  const char* results_0_daily_2_code_night = results_0_daily_2["code_night"]; // "14"
  const char* results_0_daily_2_high = results_0_daily_2["high"]; // "10"
  const char* results_0_daily_2_low = results_0_daily_2["low"]; // "7"
  const char* results_0_daily_2_precip = results_0_daily_2["precip"]; // ""
  const char* results_0_daily_2_wind_direction = results_0_daily_2["wind_direction"]; // "东北"
  const char* results_0_daily_2_wind_direction_degree = results_0_daily_2["wind_direction_degree"]; // "45"
  const char* results_0_daily_2_wind_speed = results_0_daily_2["wind_speed"]; // "10"
  const char* results_0_daily_2_wind_scale = results_0_daily_2["wind_scale"]; // "2"
  const char* results_0_last_update = results_0["last_update"]; // "2019-02-11T08:00:00+08:00"   
  // Test if parsing succeeds.
  if (!root.success()) {
    Serial.println("parseObject() failed");
    return;
  }

  // .
  Serial.println();
  Serial.print("上海今天天气: ");
  Serial.println(results_0_daily_0_text_day);
  Serial.print("上海明天天气: ");
  Serial.println(results_0_daily_1_text_day);

  // Wait 5 seconds
  delay(5000);
}

后续增加:

利用液晶屏显示天气信息

你可能感兴趣的:(嵌入式开发)