STM32搭载ESP8266与乐为物联交互

写在前面

在上篇博客 ESP8266WiFi模块接入乐为物联平台中,简答介绍了乐为物联平台,并且用串口调试助手,通过串口转USB线连接ESP8266 WiFi模块,并且串口命令建立了与乐为物联的连接,还虚拟了一些传感器的数据并且POST上去。

在实际的应用中,我们需要用微控制器来完成这一系列操作,并且把获取的传感器数据上传到乐为物联平台,而且能够从乐为物联对设备反向控制。

还有一个比较重要的问题是在实际应用中,不同的环境的无线路由的账号和密码都不同,那么如何能够让ESP8266主动连接到到无线路由呢,这里借助ESP8266硬件支持smartconfig配网,因此在程序中通过按键能够进行smartconfig自动连接WiFi的过程。

smartconfig

                if(KEY1==0)
                {
                    while(KEY1==0);
                    usart3_printf("\r\nKEY1 has been passed\r\n");
                    Smartconfig();  
                }

smartconfig 主要功能是退出透传模式,启动smartconfig,等待配网,如果配网成功,串口会有些反馈,所以启动一个定时器 (周期10ms),在定时器中断中GET_Status(void),判断是否配网成功。

void Smartconfig(void)
    {

        printf("+++");
        Delay_ms(100);
        printf("+++");
        Delay_ms(100);
        printf("AT+SAVETRANSLINK=0\r\n");   
        Delay_ms(100);      
        printf("AT+RST\r\n"); //ESP8266 reset 1s
        Delay_ms(1000);

        printf("AT+CWMODE=1\r\n");
        Delay_ms(100);
        printf("AT+CIPMUX=0\r\n");
        Delay_ms(100);

        if(WIFI_Status==ENABLE)
            usart3_printf("wifi has been connected\r\n");
        else
        {       
        printf("AT+CWSTARTSMART\r\n");
        Delay_ms(100);
        printf("AT+CWSTOPSMART\r\n");
        Delay_ms(100);
        printf("AT+CWSTARTSMART\r\n");
//      Send_Cmd("+++","OK",10);
//      Send_Cmd("+++","OK",10);
//      Send_Cmd("AT+SAVETRANSLINK=0\r\n","OK",10);     
//      Send_Cmd("AT+RST\r\n","OK",50); //ESP8266 reset 1s
//      
//      Send_Cmd("AT+CWMODE=1\r\n","OK",10);
//      Send_Cmd("AT+CIPMUX=0\r\n","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\"\r\n");
     Blink_Blink();
     printf("AT+RST\r\n");
     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串口屏显示部分。
具体工程文件分享在这里

STM32+ESP8266+smartconfig配网.zip

NRF24L01+STM32 slave1 湿度DHT22.zip

NRF24L01+STM32 slave3 反向控制.zip

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一銤阳光

希望分享的内容对你有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值