2093 views|1 replies

85

Posts

0

Resources
The OP
 

[Zero-knowledge ESP8266 tutorial] Quick Start 18 ESP8266HTTPClient library to obtain weather requests [Copy link]

Last time, we learned to use the ESP8266 development board to create a hotspot, that is, to send radio frequency signals, just like your smartphone can open a hotspot to allow others to connect. The role of our smartphone is to send radio frequency signals outward. However, when you use your own mobile phone to connect to WiFi, the role of the mobile phone is to receive radio frequency signals.

Similarly, the Zero-Knowledge ESP8266 development board is a WiFi module. Since it has the function of sending signals (creating hotspots), it also has the function of receiving signals. This time, let's let the ESP8266 development board receive information. Let's start to implement it together.
1. Hardware
Computer, Windows system
Zero-Knowledge ESP8266 development board
micro-USB cable
2.
(1) Software library:
This example uses Zero-Knowledge-ESP8266 to obtain weather information. First, you need to install the library:

You can also download it from GitHub, but be sure to download version 5.

(2) Unzip and open the ZeroKnowledge open source software. The interface is as follows:

(3) Install to the library

You can also unzip and copy it directly to the location where your lingzhi_library is stored

This completes the installation. Remember to refresh and close the software.

3.
Reopen the ZeroKnowledge open source software and burn the following code:

/**
* Demo:
* 演示Http请求天气接口信息
* @author 云上上云
* [url=home.php?mod=space&uid=311857]@date[/url] 2019/06/01
*/
#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 = "**********"; // **********-- 使用时请修改为当前你的 wifi ssid
const char* AP_PSK = "**********"; // **********-- 使用时请修改为当前你的 wifi 密码
const char* HOST = "http://api.seniverse.com";
const char* APIKEY = "wcmquevztdy1jpca"; //API KEY
const char* CITY = "shenzhen";
const char* LANGUAGE = "zh-Hans";//zh-Hans 简体中文 会显示乱码

const unsigned long BAUD_RATE = 115200; // serial connection speed
const unsigned long HTTP_TIMEOUT = 5000; // max respone time from server

// 我们要从此网页中提取的数据的类型
struct WeatherData {
char city[16];//城市名称
char weather[32];//天气介绍(多云...)
char temp[16];//温度
char udate[32];//更新时间
};

HTTPClient http;
String GetUrl;
String response;
WeatherData weatherData;

void setup() {
// put your setup code here, to run once:
WiFi.mode(WIFI_STA); //设置esp8266 工作模式
DebugBegin(BAUD_RATE);
DebugPrint("Connecting to ");//
DebugPrintln(AP_SSID);
WiFi.begin(AP_SSID, AP_PSK); //连接wifi
WiFi.setAutoConnect(true);
while (WiFi.status() != WL_CONNECTED) {
//这个函数是wifi连接状态,返回wifi链接状态
delay(500);
DebugPrint(".");
}
DebugPrintln("");
DebugPrintln("WiFi connected");
DebugPrintln("IP address: " + WiFi.localIP());

//拼接get请求url 
GetUrl = String(HOST) + "/v3/weather/now.json?key=";
GetUrl += APIKEY;
GetUrl += "&location=";
GetUrl += CITY;
GetUrl += "&language=";
GetUrl += LANGUAGE;
//设置超时
http.setTimeout(HTTP_TIMEOUT);
//设置请求url
http.begin(GetUrl);
//以下为设置一些头 其实没什么用 最重要是后端服务器支持
http.setUserAgent("esp8266");//用户代理版本
http.setAuthorization("esp8266","yssy");//用户校验信息
}

void loop() {
//心知天气 发送http get请求
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);//每1s调用一次 
}

/**
* [url=home.php?mod=space&uid=218601]@desc[/url] 解析数据 Json解析
* 数据格式如下:
* {
* "results": [
* {
* "location": {
* "id": "WX4FBXXFKE4F",
* "name": "北京",
* "country": "CN",
* "path": "北京,北京,中国",
* "timezone": "Asia/Shanghai",
* "timezone_offset": "+08:00"
* },
* "now": {
* "text": "多云",
* "code": "4",
* "temperature": "23"
* },
* "last_update": "2017-09-13T09:51:00+08:00"
* }
* ]
*}
*/
bool parseUserData(String content, struct WeatherData* weatherData) {
// -- 根据我们需要解析的数据来计算JSON缓冲区最佳大小
// 如果你使用StaticJsonBuffer时才需要
// const size_t BUFFER_SIZE = 1024;
// 在堆栈上分配一个临时内存池
// StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
// -- 如果堆栈的内存池太大,使用 DynamicJsonBuffer jsonBuffer 代替
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;
}

// 打印从JSON中提取的数据
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. Verify and upload the program.

4. Click "Debug" to see the results, as shown below:

This post is from stm32/stm8

Latest reply

Thanks for sharing!   Details Published on 2020-11-3 21:32
 

2

Posts

0

Resources
2
 

Thanks for sharing!

This post is from stm32/stm8
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list