天津大學(xué)生專業(yè)做網(wǎng)站直鏈平臺(tái)
?完整工程文件(百度網(wǎng)盤免費(fèi)下載,提取碼:0625)在文章末尾,需要請(qǐng)移步至文章末尾。
目錄
宏定義配置
串口通信配置
消息解析及數(shù)據(jù)發(fā)送
ESP8266初始化
注意事項(xiàng)
完整工程文件
????????經(jīng)過基礎(chǔ)教程使用AT指令連接阿里云后,大家應(yīng)該對(duì)設(shè)備連接云平臺(tái)的基本流程有所了解。接下來,我們將深入探討如何通過STM32微控制器來配置和優(yōu)化連接阿里云的過程。
????????STM32提供了廣泛的控制和定制選項(xiàng),能夠靈活應(yīng)對(duì)復(fù)雜的物聯(lián)網(wǎng)應(yīng)用需求。利用STM32,我們能夠?qū)崿F(xiàn)高效且可靠的設(shè)備與阿里云之間的通信,支持各種復(fù)雜的數(shù)據(jù)傳輸和設(shè)備管理功能。
宏定義配置
????????當(dāng)使用STM32微控制器連接到阿里云時(shí),通過宏定義可以顯著提升代碼的可讀性和靈活性。在開始配置和編寫代碼之前,通常會(huì)定義一些初始宏來簡化代碼的編寫和調(diào)試過程。
//WIFI配置
#define ESP8266_WIFI_INFO "AT+CWJAP=\"ALIYUN\",\"12345678\"\r\n"//阿里云證書配置
#define MQTT_CLIENT_ID "j07fRDwyQJM.STM32D|securemode=2\\,signmethod=hmacsha256\\,timestamp=1720052943739|"#define MQTT_USER_NAME "STM32D&j07fRDwyQJM"#define MQTT_PASSWD "5c7858f8ec839caddb835afc8c9ea8351b1aa7867c05e8d84c8df4eaf9e88ae6"#define ESP8266_CERT_INFO "AT+MQTTUSERCFG=0,1,\""MQTT_CLIENT_ID"\",\""MQTT_USER_NAME"\",\""MQTT_PASSWD"\",0,0,\"\"\r\n"//阿里云域名配置
#define MQTT_HOSTURL "iot-06z00iurioijdlq.mqtt.iothub.aliyuncs.com"#define ESP8266_ALIYUN_INFO "AT+MQTTCONN=0,\""MQTT_HOSTURL"\",1883,0\r\n"//數(shù)據(jù)下行
#define SUB_TOPIC "/sys/j07fRDwyQJM/STM32D/thing/service/property/set"#define ESP8266_SUBTOPIC_INFO "AT+MQTTSUB=0,\""SUB_TOPIC"\",0\r\n"//數(shù)據(jù)上行
#define PUB_TOPIC "/sys/j07fRDwyQJM/STM32D/thing/event/property/post"//上行數(shù)據(jù)
#define JSON_FORMAT "{\\\"params\\\":{\\\"temp\\\":%d\\,\\\"humi\\\":%d\\}\\,\\\"version\\\":\\\"1.0.0\\\"}"
串口通信配置
????????當(dāng)配置STM32與阿里云通信時(shí),串口數(shù)據(jù)處理函數(shù)是至關(guān)重要的一部分,用于處理從Wi-Fi模塊或其他外設(shè)接收到的數(shù)據(jù),并與阿里云進(jìn)行通信。
unsigned char esp8266_buf[256];
unsigned short esp8266_cnt = 0, esp8266_cntPre = 0;//==========================================================
// 函數(shù)名稱: ESP8266_Clear
//
// 函數(shù)功能: 清空緩存
//
// 入口參數(shù): 無
//
// 返回參數(shù): 無
//
// 說明:
//==========================================================
void ESP8266_Clear(void)
{memset(esp8266_buf, 0, sizeof(esp8266_buf));esp8266_cnt = 0;}//==========================================================
// 函數(shù)名稱: ESP8266_WaitRecive
//
// 函數(shù)功能: 等待接收完成
//
// 入口參數(shù): 無
//
// 返回參數(shù): REV_OK-接收完成 REV_WAIT-接收超時(shí)未完成
//
// 說明: 循環(huán)調(diào)用檢測是否接收完成
//==========================================================
_Bool ESP8266_WaitRecive(void)
{if(esp8266_cnt == 0) //如果接收計(jì)數(shù)為0 則說明沒有處于接收數(shù)據(jù)中,所以直接跳出,結(jié)束函數(shù)return REV_WAIT;if(esp8266_cnt == esp8266_cntPre) //如果上一次的值和這次相同,則說明接收完畢{esp8266_cnt = 0; //清0接收計(jì)數(shù)return REV_OK; //返回接收完成標(biāo)志}esp8266_cntPre = esp8266_cnt; //置為相同return REV_WAIT; //返回接收未完成標(biāo)志}//==========================================================
// 函數(shù)名稱: ESP8266_SendCmd
//
// 函數(shù)功能: 發(fā)送命令
//
// 入口參數(shù): cmd:命令
// res:需要檢查的返回指令
//
// 返回參數(shù): 0-成功 1-失敗
//
// 說明:
//==========================================================
_Bool ESP8266_SendCmd(char *cmd, char *res)
{unsigned char timeOut = 200;Usart_SendString(USART2, (unsigned char *)cmd, strlen((const char *)cmd));while(timeOut--){if(ESP8266_WaitRecive() == REV_OK) //如果收到數(shù)據(jù){if(strstr((const char *)esp8266_buf, res) != NULL) //如果檢索到關(guān)鍵詞{ESP8266_Clear(); //清空緩存return 0;}}delay_ms(10);}return 1;}
消息解析及數(shù)據(jù)發(fā)送
????????當(dāng)處理來自阿里云平臺(tái)的下行數(shù)據(jù)以及上行數(shù)據(jù)時(shí),需要實(shí)現(xiàn)消息解析和向阿里云發(fā)送數(shù)據(jù)的功能,具體涉及MQTT消息的解析和處理。
//==========================================================
// 函數(shù)名稱: Json_Analysis
//
// 函數(shù)功能: JSON數(shù)據(jù)解析
//
// 入口參數(shù): json_data:待解析數(shù)據(jù)
//
// 返回參數(shù): 無
//
// 說明:
//==========================================================
void Json_Analysis(unsigned char *json_data)
{cJSON *JSON_Value;cJSON* root = cJSON_Parse((const char*)json_data);if(root == NULL){UsartPrintf(USART_DEBUG,"cJSON_Error!\r\n");}else{cJSON* items = cJSON_GetObjectItem(root,"items");cJSON* lighting = cJSON_GetObjectItem(items,"lighting");JSON_Value = cJSON_GetObjectItem(lighting,"value");if(JSON_Value ->valueint){UsartPrintf(USART_DEBUG,"OPEN_LED!\r\n");LED0 = 0;}else{UsartPrintf(USART_DEBUG,"CLOSE_LED!\r\n");LED0 = 1;}}cJSON_Delete(root);
}//==========================================================
// 函數(shù)名稱: ESP8266_SendData
//
// 函數(shù)功能: 發(fā)送數(shù)據(jù)
//
// 入口參數(shù): 無
//
//
// 返回參數(shù): 無
//
// 說明:
//==========================================================
void ESP8266_SendData(void)
{unsigned char cmdBuf[256];ESP8266_Clear(); //清空接收緩存sprintf((char *)cmdBuf,"AT+MQTTPUB=0,\""PUB_TOPIC"\",\""JSON_FORMAT"\",0,0\r\n",temp,humi); //發(fā)送命令 while(ESP8266_SendCmd((char *)cmdBuf, "OK"));UsartPrintf(USART1,"temp:%d, humi:%d\r\n",temp,humi); //調(diào)試串口打印
}//==========================================================
// 函數(shù)名稱: ESP8266_ReceiveData
//
// 函數(shù)功能: 接收數(shù)據(jù)
//
// 入口參數(shù): timeOut:等待時(shí)間
//
// 返回參數(shù): 無
//
// 說明:
//==========================================================
void ESP8266_ReceiveData(unsigned short timeOut)
{char *ptrMQT = NULL;unsigned int msg_len=0;unsigned char msg_body[256] = {0};do{if(ESP8266_WaitRecive() == REV_OK) //如果接收完成{ptrMQT = strstr((char *)esp8266_buf, "+MQTTSUBRECV:0"); //搜索“MQT”頭if(ptrMQT == NULL) //如果沒找到,可能是MQT頭的延遲,還是需要等待一會(huì),但不會(huì)超過設(shè)定的時(shí)間{UsartPrintf(USART_DEBUG, "\"MQT\" not found\r\n");}else{sscanf((const char *)ptrMQT,"+MQTTSUBRECV:0,\""SUB_TOPIC"\",%d,%s",&msg_len,msg_body);//UsartPrintf(USART_DEBUG,"len:%d,msg:%s\r\n",msg_len,msg_body);Json_Analysis(msg_body);}}delay_ms(5);timeOut--; } while(timeOut >0);
}
ESP8266初始化
????????最后對(duì)ESP8266模塊進(jìn)行初始化,以確保它能夠與STM32微控制器實(shí)現(xiàn)通信,為兩者之間穩(wěn)定的數(shù)據(jù)交換建立必要的基礎(chǔ)。
//==========================================================
// 函數(shù)名稱: ESP8266_Init
//
// 函數(shù)功能: 初始化ESP8266
//
// 入口參數(shù): 無
//
// 返回參數(shù): 無
//
// 說明:
//==========================================================
void ESP8266_Init(void)
{ESP8266_Clear();UsartPrintf(USART_DEBUG, "0. RST\r\n");while(ESP8266_SendCmd("AT+RST\r\n", "OK"))delay_ms(500);UsartPrintf(USART_DEBUG, "1. AT\r\n");while(ESP8266_SendCmd("AT\r\n", "OK"))delay_ms(500);UsartPrintf(USART_DEBUG, "2. CWMODE\r\n");while(ESP8266_SendCmd("AT+CWMODE=1\r\n", "OK"))delay_ms(500);UsartPrintf(USART_DEBUG, "3. CWDHCP\r\n");while(ESP8266_SendCmd("AT+CWDHCP=1,1\r\n", "OK"))delay_ms(500);UsartPrintf(USART_DEBUG, "4. CWJAP\r\n");while(ESP8266_SendCmd(ESP8266_WIFI_INFO, "OK"))delay_ms(500);UsartPrintf(USART_DEBUG, "5. CONFIG CERT INFO\r\n");while(ESP8266_SendCmd(ESP8266_CERT_INFO, "OK"))delay_ms(500);UsartPrintf(USART_DEBUG, "6. CONFIG ALIYUN NETWORK\r\n");while(ESP8266_SendCmd(ESP8266_ALIYUN_INFO, "OK"))delay_ms(500);UsartPrintf(USART_DEBUG, "7. SUBSCRIBE TOPIC\r\n");while(ESP8266_SendCmd(ESP8266_SUBTOPIC_INFO, "OK"))delay_ms(500);UsartPrintf(USART_DEBUG, "8. ESP8266 Init OK\r\n");}
注意事項(xiàng)
????????此外,當(dāng)處理大量的JSON數(shù)據(jù)時(shí),如果未調(diào)整棧的大小,可能會(huì)導(dǎo)致棧溢出問題,進(jìn)而影響程序的運(yùn)行結(jié)果,甚至導(dǎo)致程序崩潰。在這種情況下,需要手動(dòng)調(diào)整棧的大小,以確保程序能夠正常運(yùn)行。
堆棧大小調(diào)整
- 打開啟動(dòng)文件后,點(diǎn)擊下方的Configuration Wizard,通過Option設(shè)置框調(diào)整堆??臻g的大小。建議將堆棧大小設(shè)置為以下數(shù)值:
Stack_Size EQU 0x00000800Heap_Size EQU 0x00000400