当前位置:网站首页>ESP8266-NodeMCU——从苏宁API获取实时天气
ESP8266-NodeMCU——从苏宁API获取实时天气
2022-07-23 12:33:00 【嵌入式创客工坊】
前言
本篇介绍如何使用ESP8266-NodeMCU从苏宁API获取实时天气,苏宁API:点击跳转
其显示如下:

其中我们要抓取的是红线部分的内容,并通过串口打印。当然,这部分也可以用来显示在OLED上,我之前就是这么玩。
在正式开始前,需要了解使用WiFiManager库连接wifi的过程,下文代码中的wifi连接用了WiFiManager库,没有将要连接的wifi名称和密码直接写入代码,具体操作可以跳转参考: ESP8266-NodeMCU——使用WiFiManager库连接wifi
一、软件准备
(1)Arduino IDE
二、硬件准备
(1)ESP8266-NodeMCU

(2)一根可以传输数据的micro USB线

三、实现代码
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
char Json_Time[36]; //从苏宁API获取的Json数据(已解析后)的数组
const String timeUrl = "http://quan.suning.com/getSysTime.do"; //苏宁API地址
String timeMsg=""; //抓取时间部分
String timeAfterParse=" "; //Json解析后的有效数据缓存字符串
char flag;
String callHttp(String httpUrl); //抓取时间网站的Json数据
String parseJsonTime(String tjson); //解析Json数据
void setup()
{
Serial.begin(115200); //串口波特率设置为115200
connectWifi(); //连接WiFi,连接后才会继续往下运行
}
void loop()
{
timeMsg = callHttp(timeUrl); //获取JSon数据
if(timeMsg!="" ) //如果成功获取数据
{
timeAfterParse = parseJsonTime(timeMsg); //解析Json数据
timeAfterParse.toCharArray(Json_Time,timeAfterParse.length() + 1); //Json_Time会变成字符串数组,内容和timeAfterParse一样
Serial.println(Json_Time);
}
delay(900);
}
//================================================连接WiFi==================================================================
void connectWifi(){
WiFiManager wifiManager;
wifiManager.autoConnect("ESP8266-NodeMCU");
}
//=================================================解析Json数据为字符串=====================================================
String parseJsonTime(String tjson){
const size_t capacity =96;
DynamicJsonDocument tdoc(capacity);
deserializeJson(tdoc, tjson); // 反序列化数据
String datetime = tdoc["sysTime2"].as<String>(); // 获取解析后的数据信息
return datetime;
}
//=================================================抓取时间网站的Json数据=====================================================
String callHttp(String httpUrl) {
HTTPClient hClient;
String message;
hClient.begin(httpUrl); //连接API地址
int httpCode = hClient.GET(); //请求获取Json数据
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = hClient.getString(); //数据转移
message = payload;
}
} else {
message = "[1-HTTP]failed, error:" + String(hClient.errorToString(httpCode).c_str()); //获取失败就报错
}
hClient.end(); //关闭API
return message; //返回信息
}
四、实现效果

网络不好可能效果没有那么理想,可能不能每秒都抓取到,不过我在测试的时候,好像是API接口本身就卡,用电脑打开有时候也显示超时,之前玩的时候还是挺流畅的
边栏推荐
猜你喜欢

W3C introduces decentralized identifier as web standard

Unity Metaverse(一)、Ready Player Me & Blender 自定义你的Avatar虚拟人

7月HCIP Datacom认证考试通过

The competition boss is in Huawei: network experts are from Stanford physics department, and some people "work as much as reading a doctoral degree"

Ali Er Mian: when does MySQL use table locks and row locks?

lc marathon 7.23

移动端H5 - 手撸一个生命线 timeline

20220722挨揍记录

单片机内部IO口保护电路及IO口电气特性以及为什么不同电压IO之间为什么串联一个电阻?

Leetcode high frequency question: the array can be changed into a non descending state after at least several operations
随机推荐
AC automata and fail tree
Purpose of wsastartup function
[2022 freshmen learning] key points of the second week
Another award | opensca was selected as the "top ten open source software products in the world" at the China Software Expo
pytest接口自动化测试框架 | 多进程运行用例
GO语言学习——复习包、接口、文件操作
Mailbox communication-
First hello of SOC_ World experiment
移动端H5 - 手撸一个生命线 timeline
Bean validation specification ----03
大屏可视化的适配方案
mysql多表查询之_内连接_显示内连接
将.calss文件转为.jar-idea篇
Redis' expiration strategy and memory elimination mechanism. Why didn't you release memory when the key expired
阿里二面:MySQL 啥时候用表锁,啥时候用行锁?
Bean Validation入门篇----02
Bean Validation起源篇----01
IIS 部署.NetCore
CA数字证书
苹果x充电慢是什么原因_iPhone 12支持15W MagSafe无线充电,未来苹果手机的充电会发生什么?_充电器…