摘要:一硬件組成用作主控。型號為一塊串口屏用作顯示。型號為轉(zhuǎn)模塊用作調(diào)試。二開發(fā)環(huán)境搭建進(jìn)入官網(wǎng)下載。
一、硬件組成
二、開發(fā)環(huán)境搭建(Arduino IDE)
????????
輸入:https://dl.espressif.com/dl/package_esp32_index.json 保存并重啟Arduino
?選擇工具/開發(fā)板管理器
搜索esp32并下載安裝
完成后可以看到esp32
三、向api接口發(fā)送HTTP請求并解析
????????
1、在Arduino中安裝所需要的庫文件:點(diǎn)擊工具/管理庫?
????????在搜索框輸入 WiFi ,選中需要安裝的庫文件?
? ? ?????????
?2、設(shè)備初始化代碼
#include //用于連接WiFi網(wǎng)絡(luò)#include //用于向服務(wù)端請求數(shù)據(jù)信息#include //用于解析返回的json數(shù)據(jù)const char *dssid = "ssid"; //你的網(wǎng)絡(luò)名稱const char *password = "password"; //你的網(wǎng)絡(luò)密碼void setup() { //做設(shè)備初始化 Serial.begin(115200); //初始化串口波特率為115200 Serial.println(""); //打印空白行 WiFi.begin(dssid,password);//連接指定wifi while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } //連接中返回..... Serial.println("WiFi connected!");//WiFi連接成功 // put your setup code here, to run once:}
3、請求數(shù)據(jù)并返回
void loop() { HTTPClient http; //建立HTTPClient對象 http.begin("https://lab.isaaclin.cn/nCoV/api/overall");//向該網(wǎng)址發(fā)送http數(shù)據(jù)請求 int httpCode = http.GET(); //保存返回的狀態(tài)碼 // put your main code here, to run repeatedly: if (httpCode > 0) // 如果狀態(tài)碼大于0說明請求過程無異常 { if (httpCode == HTTP_CODE_OK) // 請求被服務(wù)器正常響應(yīng),等同于httpCode == 200 { String payload = http.getString(); // 讀取服務(wù)器返回的響應(yīng)正文數(shù)據(jù) Serial.println(payload); //串口打印返回的數(shù)據(jù) } } else { //未被響應(yīng)返回錯誤碼 Serial.printf("[HTTP] GET... failed, error: %s/n", http.errorToString(httpCode).c_str()); } http.end(); // 結(jié)束當(dāng)前連接 delay(1200000);//兩分鐘發(fā)送一次請求,過于頻繁可能導(dǎo)致ip被接口封掉}
4、下載驗(yàn)證:開發(fā)板型號選擇:
PS:端口根據(jù)自身情況選擇
串口打印結(jié)果如下:
?
返回?cái)?shù)據(jù)為:
5、對返回?cái)?shù)據(jù)做json解析:?定義結(jié)構(gòu)體
struct ncov_data{ //該結(jié)構(gòu)體用于保存數(shù)據(jù) long currentConfirmedCount;//現(xiàn)存確診 long currentConfirmedIncr;//較昨日 long confirmedCount;//累計(jì)確診 long confirmedIncr;//較昨日 long curedCount;//累計(jì)治愈 long curedIncr;//較昨日 long seriousCount;//現(xiàn)存無癥狀 long seriousIncr;//較昨日 long deadCount;//累計(jì)死亡 long deadIncr;//較昨日 String updateTime;//更新時間};
Json初始化
ncov_data China_data; //國內(nèi)疫情數(shù)據(jù)ncov_data World_data; //國外疫情數(shù)據(jù)DynamicJsonDocument doc(1000); //創(chuàng)建DynamicJsonDocument對象deserializeJson(doc, payload); //反序列化數(shù)據(jù)JsonObject results_0 = doc["results"][0];
解析出的數(shù)據(jù)賦值
China_data.currentConfirmedCount = results_0["currentConfirmedCount"].as(); China_data.currentConfirmedIncr = results_0["currentConfirmedIncr"].as(); China_data.confirmedCount = results_0["confirmedCount"].as(); China_data.confirmedIncr = results_0["confirmedIncr"].as(); China_data.curedCount = results_0["curedCount"].as(); China_data.curedIncr = results_0["curedIncr"].as(); China_data.deadCount = results_0["deadCount"].as(); China_data.deadIncr = results_0["deadIncr"].as(); China_data.seriousCount = results_0["seriousCount"].as(); China_data.seriousIncr = results_0["seriousIncr"].as(); World_data.currentConfirmedCount = results_0["globalStatistics"]["currentConfirmedCount"].as(); World_data.currentConfirmedIncr = results_0["globalStatistics"]["currentConfirmedIncr"].as(); World_data.confirmedCount = results_0["globalStatistics"]["confirmedCount"].as(); World_data.confirmedIncr = results_0["globalStatistics"]["confirmedIncr"].as(); World_data.curedCount = results_0["globalStatistics"]["curedCount"].as(); World_data.curedIncr = results_0["globalStatistics"]["curedIncr"].as(); World_data.deadCount = results_0["globalStatistics"]["deadCount"].as(); World_data.deadIncr = results_0["globalStatistics"]["deadIncr"].as();
串口打印
Serial.println(""); Serial.println("----------------國內(nèi)疫情---------------"); Serial.print("現(xiàn)存確診: ");Serial.println(China_data.currentConfirmedCount); Serial.print("累計(jì)確診: ");Serial.println(China_data.confirmedCount); Serial.print("累計(jì)治愈: ");Serial.println(China_data.curedCount); Serial.print("現(xiàn)存無癥: ");Serial.println(China_data.seriousCount); Serial.print("累計(jì)死亡: ");Serial.println(China_data.deadCount); Serial.println("----------------國外疫情---------------"); Serial.print("現(xiàn)存確診: ");Serial.println(World_data.currentConfirmedCount); Serial.print("累計(jì)確診: ");Serial.println(World_data.confirmedCount); Serial.print("累計(jì)治愈: ");Serial.println(World_data.curedCount); Serial.print("累計(jì)死亡: ");Serial.println(World_data.deadCount);
四、發(fā)送數(shù)據(jù)到串口屏并顯示
????????1、下載USART HMI串口屏軟件,搭建自己喜歡的ui,過程可自行百度,略過。
? ? ? ? 2、Arduino代碼:串口定義
#define TJC Serial2 //串口屏收發(fā)串口定義為串口2
????????初始化
TJC.begin(115200); //初始化收發(fā)串口波特率為115200while (TJC.read() >= 0);//等待串口屏連接成功
字符串定義
char str1[20]; char str2[20]; char str3[20]; char str4[20]; char str5[20]; char str6[20]; char str7[20]; char str8[20]; char str9[20]; char str10[20];
sprintf格式拼接
sprintf(str1, "n0.val=%d/xff/xff/xff", China_data.currentConfirmedCount); sprintf(str6, "n5.val=%d/xff/xff/xff", China_data.currentConfirmedIncr); sprintf(str2, "n1.val=%d/xff/xff/xff", China_data.confirmedCount); sprintf(str7, "n6.val=%d/xff/xff/xff", China_data.confirmedIncr); sprintf(str3, "n2.val=%d/xff/xff/xff", China_data.curedCount); sprintf(str8, "n7.val=%d/xff/xff/xff", China_data.curedIncr); sprintf(str4, "n3.val=%d/xff/xff/xff", China_data.deadCount); sprintf(str9, "n8.val=%d/xff/xff/xff", China_data.deadIncr); sprintf(str5, "n4.val=%d/xff/xff/xff", China_data.seriousCount); sprintf(str10, "n9.val=%d/xff/xff/xff", China_data.seriousIncr);
數(shù)據(jù)發(fā)送
TJC.print(str1); TJC.print(str2); TJC.print(str3); TJC.print(str4); TJC.print(str5); TJC.print(str6); TJC.print(str7); TJC.print(str8); TJC.print(str9); TJC.print(str10);
3、將esp32的rx、tx引腳分別和串口屏的tx、rx引腳相連
4、參考文章 :疫情監(jiān)控三部曲——在STM32F103 MCU上實(shí)現(xiàn)(裸機(jī)版) - 云+社區(qū) - 騰訊云 (tencent.com)
五、完整代碼
#include //用于連接WiFi網(wǎng)絡(luò)#include //用于向服務(wù)端請求數(shù)據(jù)信息#include //用于解析返回的json數(shù)據(jù)#define TJC Serial2 //串口屏收發(fā)串口定義為串口2const char *dssid = "DOG"; //你的網(wǎng)絡(luò)名稱const char *password = "66666666"; //你的網(wǎng)絡(luò)密碼struct ncov_data{ //該結(jié)構(gòu)體用于保存數(shù)據(jù) long currentConfirmedCount;//現(xiàn)存確診 long currentConfirmedIncr;//較昨日 long confirmedCount;//累計(jì)確診 long confirmedIncr;//較昨日 long curedCount;//累計(jì)治愈 long curedIncr;//較昨日 long seriousCount;//現(xiàn)存無癥狀 long seriousIncr;//較昨日 long deadCount;//累計(jì)死亡 long deadIncr;//較昨日 String updateTime;//更新時間};void setup() { //做設(shè)備初始化 Serial.begin(115200); //初始化串口波特率為115200 TJC.begin(115200); //初始化收發(fā)串口波特率為115200 while (TJC.read() >= 0);//等待串口屏連接成功 Serial.println(""); //打印空白行 WiFi.begin(dssid,password);//連接指定wifi while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } //連接中返回..... Serial.println("WiFi connected!");//WiFi連接成功 // put your setup code here, to run once:}void loop() { HTTPClient http; //建立HTTPClient對象 http.begin("https://lab.isaaclin.cn/nCoV/api/overall");//向該網(wǎng)址發(fā)送http數(shù)據(jù)請求 int httpCode = http.GET(); //保存返回的狀態(tài)碼 // put your main code here, to run repeatedly: if (httpCode > 0) // 如果狀態(tài)碼大于0說明請求過程無異常 { if (httpCode == HTTP_CODE_OK) // 請求被服務(wù)器正常響應(yīng),等同于httpCode == 200 { String payload = http.getString(); // 讀取服務(wù)器返回的響應(yīng)正文數(shù)據(jù) Serial.println(payload); //串口打印返回的數(shù)據(jù) ncov_data China_data; //國內(nèi)疫情數(shù)據(jù) ncov_data World_data; //國外疫情數(shù)據(jù) DynamicJsonDocument doc(1000); //創(chuàng)建DynamicJsonDocument對象 deserializeJson(doc, payload); //反序列化數(shù)據(jù) JsonObject results_0 = doc["results"][0]; //將解析出的數(shù)據(jù)賦值給結(jié)構(gòu)體 China_data.currentConfirmedCount = results_0["currentConfirmedCount"].as(); China_data.currentConfirmedIncr = results_0["currentConfirmedIncr"].as(); China_data.confirmedCount = results_0["confirmedCount"].as(); China_data.confirmedIncr = results_0["confirmedIncr"].as(); China_data.curedCount = results_0["curedCount"].as(); China_data.curedIncr = results_0["curedIncr"].as(); China_data.deadCount = results_0["deadCount"].as(); China_data.deadIncr = results_0["deadIncr"].as(); China_data.seriousCount = results_0["seriousCount"].as(); China_data.seriousIncr = results_0["seriousIncr"].as(); World_data.currentConfirmedCount = results_0["globalStatistics"]["currentConfirmedCount"].as(); World_data.currentConfirmedIncr = results_0["globalStatistics"]["currentConfirmedIncr"].as(); World_data.confirmedCount = results_0["globalStatistics"]["confirmedCount"].as(); World_data.confirmedIncr = results_0["globalStatistics"]["confirmedIncr"].as(); World_data.curedCount = results_0["globalStatistics"]["curedCount"].as(); World_data.curedIncr = results_0["globalStatistics"]["curedIncr"].as(); World_data.deadCount = results_0["globalStatistics"]["deadCount"].as(); World_data.deadIncr = results_0["globalStatistics"]["deadIncr"].as(); //字符串定義 char str1[20]; char str2[20]; char str3[20]; char str4[20]; char str5[20]; char str6[20]; char str7[20]; char str8[20]; char str9[20]; char str10[20]; char str11[20]; char str12[20]; char str13[20]; char str14[20]; char str15[20]; char str16[20]; char str17[20]; char str18[20]; //使用sprintf進(jìn)行格式拼接 sprintf(str1, "n0.val=%d/xff/xff/xff", China_data.currentConfirmedCount); sprintf(str6, "n5.val=%d/xff/xff/xff", China_data.currentConfirmedIncr); sprintf(str2, "n1.val=%d/xff/xff/xff", China_data.confirmedCount); sprintf(str7, "n6.val=%d/xff/xff/xff", China_data.confirmedIncr); sprintf(str3, "n2.val=%d/xff/xff/xff", China_data.curedCount); sprintf(str8, "n7.val=%d/xff/xff/xff", China_data.curedIncr); sprintf(str4, "n3.val=%d/xff/xff/xff", China_data.deadCount); sprintf(str9, "n8.val=%d/xff/xff/xff", China_data.deadIncr); sprintf(str5, "n4.val=%d/xff/xff/xff", China_data.seriousCount); sprintf(str10, "n9.val=%d/xff/xff/xff", China_data.seriousIncr); //發(fā)送數(shù)據(jù) TJC.print(str1); TJC.print(str2); TJC.print(str3); TJC.print(str4); TJC.print(str5); TJC.print(str6); TJC.print(str7); TJC.print(str8); TJC.print(str9); TJC.print(str10); TJC.print("page 1/xff/xff/xff"); sprintf(str11, "n10.val=%d/xff/xff/xff", World_data.currentConfirmedCount); sprintf(str12, "n11.val=%d/xff/xff/xff", World_data.currentConfirmedIncr); sprintf(str13, "n12.val=%d/xff/xff/xff", World_data.confirmedCount); sprintf(str14, "n13.val=%d/xff/xff/xff", World_data.confirmedIncr); sprintf(str15, "n14.val=%d/xff/xff/xff", World_data.curedCount); sprintf(str16, "n15.val=%d/xff/xff/xff", World_data.curedIncr); sprintf(str17, "n16.val=%d/xff/xff/xff", World_data.deadCount); sprintf(str18, "n17.val=%d/xff/xff/xff", World_data.deadIncr); TJC.print(str11); TJC.print(str12); TJC.print(str13); TJC.print(str14); TJC.print(str15); TJC.print(str16); TJC.print(str17); TJC.print(str18); TJC.print("page 0/xff/xff/xff"); //串口打印// Serial.println("");// Serial.println("----------------國內(nèi)疫情---------------");// Serial.print("現(xiàn)存確診: ");Serial.println(China_data.currentConfirmedCount);// Serial.print("累計(jì)確診: ");Serial.println(China_data.confirmedCount);// Serial.print("累計(jì)治愈: ");Serial.println(China_data.curedCount);// Serial.print("現(xiàn)存無癥: ");Serial.println(China_data.seriousCount);// Serial.print("累計(jì)死亡: ");Serial.println(China_data.deadCount);// Serial.println("----------------國外疫情---------------");// Serial.print("現(xiàn)存確診: ");Serial.println(World_data.currentConfirmedCount);// Serial.print("累計(jì)確診: ");Serial.println(World_data.confirmedCount);// Serial.print("累計(jì)治愈: ");Serial.println(World_data.curedCount);// Serial.print("累計(jì)死亡: ");Serial.println(World_data.deadCount); } } else { //未被響應(yīng)返回錯誤碼 Serial.printf("[HTTP] GET... failed, error: %s/n", http.errorToString(httpCode).c_str()); } http.end(); // 結(jié)束當(dāng)前連接 delay(1200000);//兩分鐘發(fā)送一次請求,過于頻繁可能導(dǎo)致ip被接口封掉}
六、效果展示
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/124450.html
摘要:年末,年即將走進(jìn)尾聲,忙碌了一年準(zhǔn)備給家里人買一份禮物,這些天看了很多產(chǎn)品,最終選定了小米智能手環(huán)。 年末,2021年即將走進(jìn)尾聲,忙碌了一年準(zhǔn)備給家里人買一份禮物...
摘要:使用實(shí)現(xiàn)連網(wǎng)實(shí)現(xiàn)巴法云物聯(lián)網(wǎng)使用硬件程序思路基于正點(diǎn)原子的測試程序在巴法云物聯(lián)網(wǎng)創(chuàng)建的主題初始化代碼比較簡陋主函數(shù)代碼如果想用串口助手調(diào)試,接線方法如下使用硬件我這里使用的是正點(diǎn)原子家的開發(fā)板精英版和模塊。 ...
摘要:添加設(shè)備名和鑒權(quán)信息。記錄如下數(shù)據(jù)二引腳連接和接電源接地和連接至配置的串口三代碼編寫串口配置單片機(jī)需配置兩個串口,串口打印至串口助手,顯示連接狀態(tài)。串口用來發(fā)送信息至串口配置代碼如下系列配置和系列配置不同點(diǎn)在于口上拉和推挽配置略有不同。 ...
摘要:藍(lán)牙串口通訊實(shí)驗(yàn)?zāi)康耐ㄟ^藍(lán)牙串口輸出,實(shí)現(xiàn)無線藍(lán)牙串口調(diào)試串口函數(shù)介紹返回串口緩沖區(qū)中當(dāng)前剩余的字符個數(shù)。只有選中該對象才能,下一頁的哦驅(qū)動安裝完成后,在電腦計(jì)算機(jī)管理,可以查看到硬件藍(lán)牙串口了。 ...
摘要:復(fù)位系列芯片的復(fù)位可使用管腳。這個和保存代碼的是不一樣的,計(jì)劃使用。 記錄一下第一次使用立創(chuàng)EDA第一次設(shè)計(jì)ESP-C3的開發(fā)板(這幾天工作有點(diǎn)忙,稍等) 目錄 ...
閱讀 2306·2021-11-23 09:51
閱讀 3781·2021-10-20 13:49
閱讀 1776·2021-09-06 15:13
閱讀 1882·2021-09-06 15:02
閱讀 3354·2021-09-02 15:11
閱讀 962·2019-08-29 15:37
閱讀 1800·2019-08-29 13:24
閱讀 2323·2019-08-29 11:28