初步实现MQTT上传DHT11温度到onenet。根据正点原子的ESP8266例子,改成mqtt协议通信上传温湿度到onenet
单片机源程序如下:
- #include "stm32f4xx.h"
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "key.h"
- #include "usmart.h"
- #include "sram.h"
- #include "malloc.h"
- #include "w25qxx.h"
- #include "sdio_sdcard.h"
- #include "ff.h"
- #include "exfuns.h"
- #include "fontupd.h"
- #include "text.h"
- #include "usmart.h"
- #include "touch.h"
- #include "usart3.h"
- #include "common.h"
- #include "dht11.h"
- int temp_limit_up = 35, temp_limit_down = 10, humi_limit_up = 90, humi_limit_down = 20; //温湿度阈值
- //测试界面主UI
- void esp_8266_mtest_ui(u16 x,u16 y)
- {
- LCD_Clear(WHITE);
- POINT_COLOR=RED;
- Show_Str_Mid(0,y,"ESP8266 WIFI应用",16,240);
- Show_Str(x,y+25,200,16,"请选择网络模式:",16,0);
- Show_Str(x,y+45,200,16,"KEY0:WIFI STA+AP",16,0);
- Show_Str(x,y+65,200,16,"KEY1:WIFI STA",16,0);
- Show_Str(x,y+85,200,16,"KEY2:WIFI AP",16,0);
- Show_Str(x,y+105,200,16,"WK_UP:WIFI STAMQTT",16,0);
-
- }
- //esp-ESP8266模块测试主函数
- void esp_8266_test(void)
- {
- u8 key;
- POINT_COLOR=RED; //LCD字体设置红色
- while(esp_8266_send_cmd("AT","OK",20))//检查WIFI模块是否在线
- {
- esp_8266_quit_trans();//退出透传
- esp_8266_send_cmd("AT+CIPMODE=0","OK",200); //关闭透传模式
- Show_Str(40,55,200,16,"未检测到模块!!!",16,0);
- delay_ms(800);
- LCD_Fill(40,55,200,55+16,WHITE); //对矩形区域填充白色
- Show_Str(40,55,200,16,"尝试连接模块...",16,0);
- }
- while(esp_8266_send_cmd("ATE0","OK",20));//关闭回显
- esp_8266_mtest_ui(32,30); //显示UI界面
- while(1)
- {
- delay_ms(10);
- key=KEY_Scan(0);
- if(key)
- {
- LCD_Clear(WHITE);
- POINT_COLOR=RED;
- switch(key)
- {
- case 1://KEY0
- Show_Str_Mid(0,30,"ESP WIFI-AP+STA 测试",16,240);
- Show_Str_Mid(0,50,"正在配置ESP8266模块,请稍等...",12,240);
- esp_8266_apsta_test();//WIFI APSTA测试
- break;
- case 2://KEY1
- Show_Str_Mid(0,30,"ESP WIFI-STA 测试",16,240);
- Show_Str_Mid(0,50,"正在配置ESP8266模块,请稍等...",12,240);
- esp_8266_wifista_test();//WIFI STA测试
- break;
- case 3://KEY2
- Show_Str_Mid(0,30,"ESP WIFI-AP 测试",16,240);
- Show_Str_Mid(0,50,"正在配置ESP8266模块,请稍等...",12,240);
- esp_8266_wifiap_test();//WIFI AP测试
- break;
- case 4://KEY_UP
- Show_Str_Mid(0,30,"ESP WIFI-STA-MQTT 测试",16,240);
- Show_Str_Mid(0,50,"正在配置ESP8266模块,请稍等...",12,240);
- esp_8266_wifistamqtt_test();//WIFI STA MQTT测试
- break;
- }
- }
- }
- }
- int main(void)
- {
- u8 key,fontok=0;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
- delay_init(168); //初始化延时函数
- uart_init(115200); //初始化串口波特率为115200
- usart3_init(115200); //初始化串口3波特率为115200
- LED_Init(); //初始化LED
- LCD_Init(); //LCD初始化
- KEY_Init(); //按键初始化
- W25QXX_Init(); //初始化W25Q128
- tp_dev.init(); //初始化触摸屏
- usmart_dev.init(168); //初始化USMART
- my_mem_init(SRAMIN); //初始化内部内存池
- my_mem_init(SRAMCCM); //初始化CCM内存池
- exfuns_init(); //为fatfs相关变量申请内存
- f_mount(fs[0],"0:",1); //挂载SD卡
- f_mount(fs[1],"1:",1); //挂载FLASH.
- key=KEY_Scan(0);
- if(key==KEY0_PRES) //强制校准
- {
- LCD_Clear(WHITE); //清屏
- TP_Adjust(); //屏幕校准
- TP_Save_Adjdata();
- LCD_Clear(WHITE); //清屏
- }
- fontok=font_init(); //检查字库是否OK
- if(fontok||key==KEY1_PRES)//需要更新字库
- {
- LCD_Clear(WHITE); //清屏
- POINT_COLOR=RED; //设置字体为红色
- LCD_ShowString(60,50,200,16,16,"ALIENTEK STM32");
- while(SD_Init()) //检测SD卡
- {
- LCD_ShowString(60,70,200,16,16,"SD Card Failed!");
- delay_ms(200);
- LCD_Fill(60,70,200+60,70+16,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(60,70,200,16,16,"SD Card OK");
- LCD_ShowString(60,90,200,16,16,"Font Updating...");
- key=update_font(20,110,16,"0:");//从SD卡更新
- while(key)//更新失败
- {
- LCD_ShowString(60,110,200,16,16,"Font Update Failed!");
- delay_ms(200);
- LCD_Fill(20,110,200+20,110+16,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(60,110,200,16,16,"Font Update Success!");
- delay_ms(1500);
- LCD_Clear(WHITE);//清屏
- }
-
- if(DHT11_Init()) //dht11器件不存在
- {
- Show_Str_Mid(0,30,"DHT11 Init ERROR",16,240);
-
- }
-
- /*
- while(1)
- {
-
- key=KEY_Scan(0);
- if(key)
- {
-
- switch(key)
- {
- case 1://KEY0
- printf("a");
- break;
- case 2://KEY1
- printf("b");
- break;
- case 3://KEY2
- printf("c");
- break;
- case 4://KEY_UP
- printf("d");
- break;
- }
- }
- }
- */
- esp_8266_test(); //进入ATK_ESP8266测试
- }
复制代码
所有资料51hei附件下载:
MQTT应用代码.7z
(829.88 KB, 下载次数: 60)
|