写在前面
在上篇博客 ESP8266WiFi模块接入乐为物联平台中,简答介绍了乐为物联平台,并且用串口调试助手,通过串口转USB线连接ESP8266 WiFi模块,并且串口命令建立了与乐为物联的连接,还虚拟了一些传感器的数据并且POST上去。
在实际的应用中,我们需要用微控制器来完成这一系列操作,并且把获取的传感器数据上传到乐为物联平台,而且能够从乐为物联对设备反向控制。
还有一个比较重要的问题是在实际应用中,不同的环境的无线路由的账号和密码都不同,那么如何能够让ESP8266主动连接到到无线路由呢,这里借助ESP8266硬件支持smartconfig配网,因此在程序中通过按键能够进行smartconfig自动连接WiFi的过程。
smartconfig
if(KEY1==0)
{
while(KEY1==0);
usart3_printf("rnKEY1 has been passedrn");
Smartconfig();
}
主要功能是退出透传模式,启动smartconfig,等待配网,如果配网成功,串口会有些反馈,所以启动一个定时器 (周期10ms),在定时器中断中GET_Status(void),判断是否配网成功。
void Smartconfig(void)
{
printf("+++");
Delay_ms(100);
printf("+++");
Delay_ms(100);
printf("AT+SAVETRANSLINK=0rn");
Delay_ms(100);
printf("AT+RSTrn"); //ESP8266 reset 1s
Delay_ms(1000);
printf("AT+CWMODE=1rn");
Delay_ms(100);
printf("AT+CIPMUX=0rn");
Delay_ms(100);
if(WIFI_Status==ENABLE)
usart3_printf("wifi has been connectedrn");
else
{
printf("AT+CWSTARTSMARTrn");
Delay_ms(100);
printf("AT+CWSTOPSMARTrn");
Delay_ms(100);
printf("AT+CWSTARTSMARTrn");
// Send_Cmd("+++","OK",10);
// Send_Cmd("+++","OK",10);
// Send_Cmd("AT+SAVETRANSLINK=0rn","OK",10);
// Send_Cmd("AT+RSTrn","OK",50); //ESP8266 reset 1s
//
// Send_Cmd("AT+CWMODE=1rn","OK",10);
// Send_Cmd("AT+CIPMUX=0rn","OK",10);
//
// Send_Cmd("AT+CWSTARTSMART","OK",10);
// Send_Cmd("AT+CWSTOPSMART","OK",10);
// Send_Cmd("AT+CWSTARTSMART","OK",10);
SMART_Status = 0;
while(SMART_Status==0)
{
IWDG_Feed();
LED1_Blink();
Delay_ms(150);
}
Send_Cmd("AT+CWSTOPSMART","OK",10);
printf("AT+SAVETRANSLINK=1,"101.37.32.173",9960,"TCP"rn");
Blink_Blink();
printf("AT+RSTrn");
Blink_Blink();
}
}
// 前面 smartconfig 发的命令,串口会有些反馈,判断是否配网成功
void GET_Status(void)
{
if(USART1_Check("WIFI DISCONNECT")) //send wifi disconnect
WIFI_Status = 0; // the flag take 0
if(USART1_Check("WIFI CONNECTED")) //send wifi connected
WIFI_Status = 1; // the flag take 1
if(USART1_Check("smartconfig connected wifi")) //smartconfig successful
{
SMART_Status = 1;
printf("AT+CWSTOPSMART");
}
}
#
上传数据到乐为物联,这里使用长连接,定时上传数据到乐为物联
5s 上传一次
printf(Heartbeat); //send heartbeat
Lewei_Send();
const char *Heartbeat = "{"method":"update","gatewayNo":"01","userkey":"afe8c596525747f49a3db2f6b7f69fa7"}&^!"; //keeping link heartbeat message
const char *Response = "{"method":"response","result":{"successful":true,"message":"Write serial successful 0"}}&^!"; //Response the commend message ,to make sure receive the signal
void Lewei_Send(void)
{
char *Device_string;
Device_string = mymalloc(150);
sprintf(Device_string,"{"method":"upload","data":
[{"Name":"T1","Value":"%d"},
{"Name":"H1","Value":"%d"},
{"Name":"LED1","Value":"%d"},
{"Name":"LED2","Value":"%d"}]}&^!",System.Temp,System.Humi,System.LED1_Sta,System.LED2_Sta);
printf(Device_string);
myfree(Device_string);
}
收到数据之后,在定时器中断中GET_Commed(),响应收到数据,并且对数据进行分割、解析
工程概述
整个工程不仅包含ESP8266的配网和收发数据,同时还包含NRF24L01组网, HMI串口屏显示部分。
写在前面
在上篇博客 ESP8266WiFi模块接入乐为物联平台中,简答介绍了乐为物联平台,并且用串口调试助手,通过串口转USB线连接ESP8266 WiFi模块,并且串口命令建立了与乐为物联的连接,还虚拟了一些传感器的数据并且POST上去。
在实际的应用中,我们需要用微控制器来完成这一系列操作,并且把获取的传感器数据上传到乐为物联平台,而且能够从乐为物联对设备反向控制。
还有一个比较重要的问题是在实际应用中,不同的环境的无线路由的账号和密码都不同,那么如何能够让ESP8266主动连接到到无线路由呢,这里借助ESP8266硬件支持smartconfig配网,因此在程序中通过按键能够进行smartconfig自动连接WiFi的过程。
smartconfig
if(KEY1==0)
{
while(KEY1==0);
usart3_printf("rnKEY1 has been passedrn");
Smartconfig();
}
主要功能是退出透传模式,启动smartconfig,等待配网,如果配网成功,串口会有些反馈,所以启动一个定时器 (周期10ms),在定时器中断中GET_Status(void),判断是否配网成功。
void Smartconfig(void)
{
printf("+++");
Delay_ms(100);
printf("+++");
Delay_ms(100);
printf("AT+SAVETRANSLINK=0rn");
Delay_ms(100);
printf("AT+RSTrn"); //ESP8266 reset 1s
Delay_ms(1000);
printf("AT+CWMODE=1rn");
Delay_ms(100);
printf("AT+CIPMUX=0rn");
Delay_ms(100);
if(WIFI_Status==ENABLE)
usart3_printf("wifi has been connectedrn");
else
{
printf("AT+CWSTARTSMARTrn");
Delay_ms(100);
printf("AT+CWSTOPSMARTrn");
Delay_ms(100);
printf("AT+CWSTARTSMARTrn");
// Send_Cmd("+++","OK",10);
// Send_Cmd("+++","OK",10);
// Send_Cmd("AT+SAVETRANSLINK=0rn","OK",10);
// Send_Cmd("AT+RSTrn","OK",50); //ESP8266 reset 1s
//
// Send_Cmd("AT+CWMODE=1rn","OK",10);
// Send_Cmd("AT+CIPMUX=0rn","OK",10);
//
// Send_Cmd("AT+CWSTARTSMART","OK",10);
// Send_Cmd("AT+CWSTOPSMART","OK",10);
// Send_Cmd("AT+CWSTARTSMART","OK",10);
SMART_Status = 0;
while(SMART_Status==0)
{
IWDG_Feed();
LED1_Blink();
Delay_ms(150);
}
Send_Cmd("AT+CWSTOPSMART","OK",10);
printf("AT+SAVETRANSLINK=1,"101.37.32.173",9960,"TCP"rn");
Blink_Blink();
printf("AT+RSTrn");
Blink_Blink();
}
}
// 前面 smartconfig 发的命令,串口会有些反馈,判断是否配网成功
void GET_Status(void)
{
if(USART1_Check("WIFI DISCONNECT")) //send wifi disconnect
WIFI_Status = 0; // the flag take 0
if(USART1_Check("WIFI CONNECTED")) //send wifi connected
WIFI_Status = 1; // the flag take 1
if(USART1_Check("smartconfig connected wifi")) //smartconfig successful
{
SMART_Status = 1;
printf("AT+CWSTOPSMART");
}
}
#
上传数据到乐为物联,这里使用长连接,定时上传数据到乐为物联
5s 上传一次
printf(Heartbeat); //send heartbeat
Lewei_Send();
const char *Heartbeat = "{"method":"update","gatewayNo":"01","userkey":"afe8c596525747f49a3db2f6b7f69fa7"}&^!"; //keeping link heartbeat message
const char *Response = "{"method":"response","result":{"successful":true,"message":"Write serial successful 0"}}&^!"; //Response the commend message ,to make sure receive the signal
void Lewei_Send(void)
{
char *Device_string;
Device_string = mymalloc(150);
sprintf(Device_string,"{"method":"upload","data":
[{"Name":"T1","Value":"%d"},
{"Name":"H1","Value":"%d"},
{"Name":"LED1","Value":"%d"},
{"Name":"LED2","Value":"%d"}]}&^!",System.Temp,System.Humi,System.LED1_Sta,System.LED2_Sta);
printf(Device_string);
myfree(Device_string);
}
收到数据之后,在定时器中断中GET_Commed(),响应收到数据,并且对数据进行分割、解析
工程概述
整个工程不仅包含ESP8266的配网和收发数据,同时还包含NRF24L01组网, HMI串口屏显示部分。
举报