当前位置:网站首页>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接口本身就卡,用电脑打开有时候也显示超时,之前玩的时候还是挺流畅的
边栏推荐
- pytest接口自动化测试框架 | 多进程运行用例
- 锁相环工作原理,比如我们8MHZ晶振如何让MCU工作在48MHZ或者72MHZ呢
- Redis' expiration strategy and memory elimination mechanism. Why didn't you release memory when the key expired
- Ora-01654 error: table space is full, insert failed
- Calendar calendar class
- [cloud native] continuous integration and deployment (Jenkins)
- Oralce中实现将指定列的指定内容替换为想要的内容
- Go interface: go deep into internal principles
- C#中单例模式的实现
- GO语言学习——复习包、接口、文件操作
猜你喜欢

JS filter / replace sensitive characters

Dark horse programmer - interface test - four day learning interface test - third day - advanced usage of postman, export and import of Newman case set, common assertions, assertion JSON data, working

MySQL soul 16 ask, how many questions can you hold on to?

How to choose fluorescent dyes in laser confocal

死锁、饥饿、死循环之间的区别

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

机器狗背冲锋枪射击视频火了,网友瑟瑟发抖:stooooooooppppp!

lc marathon 7.23

FPGA HLS multiplier (pipeline vs. ordinary simulation)

腾讯云获国际专业流媒体测评肯定:三大场景下视频编码性能全部最优
随机推荐
基于USB数据采集卡(DAQ)与IO模块的热电阻温度采集「建议收藏」
Governance and network security of modern commercial codeless development platform
封片剂 甘油封片 中性树脂封片剂型
Cover - computer knowledge guide
Packaging and use of fmdb
Go interface: go deep into internal principles
Vulnstack red sun-4
Cloud native (11) | kubernetes chapter kubernetes principle and installation
将.calss文件转为.jar-idea篇
Why does fatal always appear when using opengaussjdbc? (tag database keyword user)
20220722挨揍记录
Bean Validation核心組件篇----04
Nport serial server principle, moxa serial server nport-5130 detailed configuration
Niuke-top101-bm36
7、 Logic of JMeter sending request
Emgucv recording video
Practice code - day one
死锁的3种处理策略
Differences between LRU and LFU elimination strategies in redis
Bean Validation核心组件篇----04