【物联网实战项目】STM32C8T6+esp8266/mqtt+dht11+onenet+uniapp

时间:2024-06-08 12:40:37
//单片机头文件 #include "stm32f10x.h" //网络协议层 #include "onenet.h" //网络设备 #include "esp8266.h" //硬件驱动 #include "delay.h" #include "usart.h" #include "led.h" #include "key.h" #include "dht11.h" #include "oled.h" //C库 #include <string.h> #define ESP8266_ONENET_INFO "AT+CIPSTART=\"TCP\",\"mqtts.heclouds.com\",1883\r\n" void Hardware_Init(void); void Display_Init(void); void Refresh_Data(void); u8 temp,humi; /* ************************************************************ * 函数名称: main * * 函数功能: * * 入口参数: 无 * * 返回参数: 0 * * 说明: ************************************************************ */ int main(void) { unsigned short timeCount = 0; //发送间隔变量 unsigned char *dataPtr = NULL; Hardware_Init(); //初始化外围硬件 ESP8266_Init(); //初始化ESP8266 // UsartPrintf(USART_DEBUG, "Connect MQTTs Server...\r\n"); OLED_Clear(); OLED_ShowString(0,0,"Connect MQTTs Server...",16); while(ESP8266_SendCmd(ESP8266_ONENET_INFO, "CONNECT")) DelayXms(500); // UsartPrintf(USART_DEBUG, "Connect MQTT Server Success\r\n"); OLED_ShowString(0,4,"Connect MQTT Server Success",16); DelayXms(500); OLED_Clear(); OLED_ShowString(0,0,"Device login ...",16); while(OneNet_DevLink()) //接入OneNET { ESP8266_SendCmd(ESP8266_ONENET_INFO, "CONNECT"); DelayXms(500); } OneNET_Subscribe(); Display_Init(); while(1) { if(++timeCount >= 100) //发送间隔5s { DHT11_Read_Data(&temp,&humi); // UsartPrintf(USART_DEBUG, "OneNet_SendData\r\n"); OneNet_SendData(); //发送数据 timeCount = 0; ESP8266_Clear(); } dataPtr = ESP8266_GetIPD(0); if(dataPtr != NULL) OneNet_RevPro(dataPtr); Refresh_Data(); DelayMs(10); } }