摘要:一硬件組成用作主控。型號為一塊串口屏用作顯示。型號為轉模塊用作調試。二開發環境搭建進入官網下載。
一、硬件組成
二、開發環境搭建(Arduino IDE)
????????
輸入:https://dl.espressif.com/dl/package_esp32_index.json 保存并重啟Arduino
?選擇工具/開發板管理器
搜索esp32并下載安裝
完成后可以看到esp32
三、向api接口發送HTTP請求并解析
????????
1、在Arduino中安裝所需要的庫文件:點擊工具/管理庫?
????????在搜索框輸入 WiFi ,選中需要安裝的庫文件?
? ? ?????????
?2、設備初始化代碼
#include //用于連接WiFi網絡#include //用于向服務端請求數據信息#include //用于解析返回的json數據const char *dssid = "ssid"; //你的網絡名稱const char *password = "password"; //你的網絡密碼void setup() { //做設備初始化 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、請求數據并返回
void loop() { HTTPClient http; //建立HTTPClient對象 http.begin("https://lab.isaaclin.cn/nCoV/api/overall");//向該網址發送http數據請求 int httpCode = http.GET(); //保存返回的狀態碼 // put your main code here, to run repeatedly: if (httpCode > 0) // 如果狀態碼大于0說明請求過程無異常 { if (httpCode == HTTP_CODE_OK) // 請求被服務器正常響應,等同于httpCode == 200 { String payload = http.getString(); // 讀取服務器返回的響應正文數據 Serial.println(payload); //串口打印返回的數據 } } else { //未被響應返回錯誤碼 Serial.printf("[HTTP] GET... failed, error: %s/n", http.errorToString(httpCode).c_str()); } http.end(); // 結束當前連接 delay(1200000);//兩分鐘發送一次請求,過于頻繁可能導致ip被接口封掉}
4、下載驗證:開發板型號選擇:
PS:端口根據自身情況選擇
串口打印結果如下:
?
返回數據為:
5、對返回數據做json解析:?定義結構體
struct ncov_data{ //該結構體用于保存數據 long currentConfirmedCount;//現存確診 long currentConfirmedIncr;//較昨日 long confirmedCount;//累計確診 long confirmedIncr;//較昨日 long curedCount;//累計治愈 long curedIncr;//較昨日 long seriousCount;//現存無癥狀 long seriousIncr;//較昨日 long deadCount;//累計死亡 long deadIncr;//較昨日 String updateTime;//更新時間};
Json初始化
ncov_data China_data; //國內疫情數據ncov_data World_data; //國外疫情數據DynamicJsonDocument doc(1000); //創建DynamicJsonDocument對象deserializeJson(doc, payload); //反序列化數據JsonObject results_0 = doc["results"][0];
解析出的數據賦值
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("----------------國內疫情---------------"); Serial.print("現存確診: ");Serial.println(China_data.currentConfirmedCount); Serial.print("累計確診: ");Serial.println(China_data.confirmedCount); Serial.print("累計治愈: ");Serial.println(China_data.curedCount); Serial.print("現存無癥: ");Serial.println(China_data.seriousCount); Serial.print("累計死亡: ");Serial.println(China_data.deadCount); Serial.println("----------------國外疫情---------------"); Serial.print("現存確診: ");Serial.println(World_data.currentConfirmedCount); Serial.print("累計確診: ");Serial.println(World_data.confirmedCount); Serial.print("累計治愈: ");Serial.println(World_data.curedCount); Serial.print("累計死亡: ");Serial.println(World_data.deadCount);
四、發送數據到串口屏并顯示
????????1、下載USART HMI串口屏軟件,搭建自己喜歡的ui,過程可自行百度,略過。
? ? ? ? 2、Arduino代碼:串口定義
#define TJC Serial2 //串口屏收發串口定義為串口2
????????初始化
TJC.begin(115200); //初始化收發串口波特率為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);
數據發送
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、參考文章 :疫情監控三部曲——在STM32F103 MCU上實現(裸機版) - 云+社區 - 騰訊云 (tencent.com)
五、完整代碼
#include //用于連接WiFi網絡#include //用于向服務端請求數據信息#include //用于解析返回的json數據#define TJC Serial2 //串口屏收發串口定義為串口2const char *dssid = "DOG"; //你的網絡名稱const char *password = "66666666"; //你的網絡密碼struct ncov_data{ //該結構體用于保存數據 long currentConfirmedCount;//現存確診 long currentConfirmedIncr;//較昨日 long confirmedCount;//累計確診 long confirmedIncr;//較昨日 long curedCount;//累計治愈 long curedIncr;//較昨日 long seriousCount;//現存無癥狀 long seriousIncr;//較昨日 long deadCount;//累計死亡 long deadIncr;//較昨日 String updateTime;//更新時間};void setup() { //做設備初始化 Serial.begin(115200); //初始化串口波特率為115200 TJC.begin(115200); //初始化收發串口波特率為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");//向該網址發送http數據請求 int httpCode = http.GET(); //保存返回的狀態碼 // put your main code here, to run repeatedly: if (httpCode > 0) // 如果狀態碼大于0說明請求過程無異常 { if (httpCode == HTTP_CODE_OK) // 請求被服務器正常響應,等同于httpCode == 200 { String payload = http.getString(); // 讀取服務器返回的響應正文數據 Serial.println(payload); //串口打印返回的數據 ncov_data China_data; //國內疫情數據 ncov_data World_data; //國外疫情數據 DynamicJsonDocument doc(1000); //創建DynamicJsonDocument對象 deserializeJson(doc, payload); //反序列化數據 JsonObject results_0 = doc["results"][0]; //將解析出的數據賦值給結構體 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進行格式拼接 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); //發送數據 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("----------------國內疫情---------------");// Serial.print("現存確診: ");Serial.println(China_data.currentConfirmedCount);// Serial.print("累計確診: ");Serial.println(China_data.confirmedCount);// Serial.print("累計治愈: ");Serial.println(China_data.curedCount);// Serial.print("現存無癥: ");Serial.println(China_data.seriousCount);// Serial.print("累計死亡: ");Serial.println(China_data.deadCount);// Serial.println("----------------國外疫情---------------");// Serial.print("現存確診: ");Serial.println(World_data.currentConfirmedCount);// Serial.print("累計確診: ");Serial.println(World_data.confirmedCount);// Serial.print("累計治愈: ");Serial.println(World_data.curedCount);// Serial.print("累計死亡: ");Serial.println(World_data.deadCount); } } else { //未被響應返回錯誤碼 Serial.printf("[HTTP] GET... failed, error: %s/n", http.errorToString(httpCode).c_str()); } http.end(); // 結束當前連接 delay(1200000);//兩分鐘發送一次請求,過于頻繁可能導致ip被接口封掉}
六、效果展示
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/124450.html
摘要:年末,年即將走進尾聲,忙碌了一年準備給家里人買一份禮物,這些天看了很多產品,最終選定了小米智能手環。 年末,2021年即將走進尾聲,忙碌了一年準備給家里人買一份禮物...
摘要:使用實現連網實現巴法云物聯網使用硬件程序思路基于正點原子的測試程序在巴法云物聯網創建的主題初始化代碼比較簡陋主函數代碼如果想用串口助手調試,接線方法如下使用硬件我這里使用的是正點原子家的開發板精英版和模塊。 ...
摘要:添加設備名和鑒權信息。記錄如下數據二引腳連接和接電源接地和連接至配置的串口三代碼編寫串口配置單片機需配置兩個串口,串口打印至串口助手,顯示連接狀態。串口用來發送信息至串口配置代碼如下系列配置和系列配置不同點在于口上拉和推挽配置略有不同。 ...
摘要:藍牙串口通訊實驗目的通過藍牙串口輸出,實現無線藍牙串口調試串口函數介紹返回串口緩沖區中當前剩余的字符個數。只有選中該對象才能,下一頁的哦驅動安裝完成后,在電腦計算機管理,可以查看到硬件藍牙串口了。 ...
摘要:復位系列芯片的復位可使用管腳。這個和保存代碼的是不一樣的,計劃使用。 記錄一下第一次使用立創EDA第一次設計ESP-C3的開發板(這幾天工作有點忙,稍等) 目錄 ...
閱讀 2089·2021-11-23 09:51
閱讀 3697·2021-10-20 13:49
閱讀 1706·2021-09-06 15:13
閱讀 1815·2021-09-06 15:02
閱讀 3154·2021-09-02 15:11
閱讀 889·2019-08-29 15:37
閱讀 1731·2019-08-29 13:24
閱讀 2273·2019-08-29 11:28