【零知ESP8266教程】快速入门18 ESP8266HTTPClient库 获取天气请求
[复制链接]
上次我们一起学习用ESP8266开发板创建一个热点,即发送射频信号,就像自己的智能手机可以打开热点,使得他人连接,我们智能手机的角色就是向外发送射频信号,然而,用自己的手机去连接WiFi,那手机充当的角色就是接收射频信号的啦。。
同理,零知ESP8266开发板是WiFi模块,既然有发送信号的功能(创建热点),当然也有接收信号的功能。这次的分享我们来让ESP8266开发板接收信息,一起开始实现吧。
一、硬件
电脑,windows系统
零知ESP8266开发板
micro-usb线
二、
(1)软件库:
本示例使用零知-ESP8266来获取天气信息,首先需要安装库:
也可以在GitHub下载,注意要下载5.~版本
(2)解压,然后打开零知开源软件,界面如下:
(3)安装到库
也可以解压直接复制到你lingzhi_library存放的位置
这样就完成安装了,届时要记得刷新一下,关闭软件。
三、
重新打开零知开源软件,然后烧录以下代码:
-
- #include <ESP8266WiFi.h>
- #include <ArduinoJson.h>
- #include <ESP8266HTTPClient.h>
-
-
- #define DebugBegin(baud_rate) Serial.begin(baud_rate)
- #define DebugPrintln(message) Serial.println(message)
- #define DebugPrint(message) Serial.print(message)
-
- const char* AP_SSID = "**********";
- const char* AP_PSK = "**********";
- const char* HOST = "http://api.seniverse.com";
- const char* APIKEY = "wcmquevztdy1jpca";
- const char* CITY = "shenzhen";
- const char* LANGUAGE = "zh-Hans";
-
- const unsigned long BAUD_RATE = 115200;
- const unsigned long HTTP_TIMEOUT = 5000;
-
-
- struct WeatherData {
- char city[16];
- char weather[32];
- char temp[16];
- char udate[32];
- };
-
- HTTPClient http;
- String GetUrl;
- String response;
- WeatherData weatherData;
-
- void setup() {
-
- WiFi.mode(WIFI_STA);
- DebugBegin(BAUD_RATE);
- DebugPrint("Connecting to ");
- DebugPrintln(AP_SSID);
- WiFi.begin(AP_SSID, AP_PSK);
- WiFi.setAutoConnect(true);
- while (WiFi.status() != WL_CONNECTED) {
-
- delay(500);
- DebugPrint(".");
- }
- DebugPrintln("");
- DebugPrintln("WiFi connected");
- DebugPrintln("IP address: " + WiFi.localIP());
-
-
- GetUrl = String(HOST) + "/v3/weather/now.json?key=";
- GetUrl += APIKEY;
- GetUrl += "&location=";
- GetUrl += CITY;
- GetUrl += "&language=";
- GetUrl += LANGUAGE;
-
- http.setTimeout(HTTP_TIMEOUT);
-
- http.begin(GetUrl);
-
- http.setUserAgent("esp8266");
- http.setAuthorization("esp8266","yssy");
- }
-
- void loop() {
-
- int httpCode = http.GET();
- if (httpCode > 0) {
- Serial.printf("[HTTP] GET... code: %d\n", httpCode);
-
- if (httpCode == HTTP_CODE_OK) {
-
- response = http.getString();
- DebugPrintln("Get the data from Internet!");
- DebugPrintln(response);
-
- if (parseUserData(response, &weatherData)) {
-
- printUserData(&weatherData);
- }
- }
- } else {
- Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- delay(1000);
- }
-
-
- bool parseUserData(String content, struct WeatherData* weatherData) {
-
-
-
-
-
-
- DynamicJsonBuffer jsonBuffer;
-
- JsonObject& root = jsonBuffer.parseObject(content);
-
- if (!root.success()) {
- DebugPrintln("JSON parsing failed!");
- return false;
- }
-
-
- strcpy(weatherData->city, root["results"][0]["location"]["name"]);
- strcpy(weatherData->weather, root["results"][0]["now"]["text"]);
- strcpy(weatherData->temp, root["results"][0]["now"]["temperature"]);
- strcpy(weatherData->udate, root["results"][0]["last_update"]);
-
-
- return true;
- }
-
-
- void printUserData(const struct WeatherData* weatherData) {
- DebugPrintln("Print parsed data :");
- DebugPrint("City : ");
- DebugPrint(weatherData->city);
- DebugPrint(", \t");
- DebugPrint("Weather : ");
- DebugPrint(weatherData->weather);
- DebugPrint(",\t");
- DebugPrint("Temp : ");
- DebugPrint(weatherData->temp);
- DebugPrint(" C");
- DebugPrint(",\t");
- DebugPrint("Last Updata : ");
- DebugPrint(weatherData->udate);
- DebugPrintln("\r\n");
- }
2、验证,上传程序。
四、点击“调试”,就可看到结果啦,如下图:
|