欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

ESP8266-连接阿里云示例

发布时间:2023/12/18 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 ESP8266-连接阿里云示例 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1:环境

ESP8266
STM42F407
wifi:手机热点
阿里云

2:目的

通过在F407中运行代码,让其与ESP8266通过uart3来进行通信。通过uart3发送AT指令。也接收返回的数据。
实现功能:1)可以向阿里云的物模型持续发送数据
2)可以通过阿里云上的开关来控制开发板上LED灯的亮灭

3:主要代码

#include "common.h" u8 atk_8266_aliyun_test(void) {u8 netpro;u8 key=0;u8 timex=0; u8 ipbuf[16]; //IP缓存u8 *p;u8 *data;u16 t=0;u8 res=0;u16 rlen=0;u8 constate=0; //连接状态u8 i =0;char *str;p=mymalloc(SRAMIN,100); data=mymalloc(SRAMIN,1000);res = atk_8266_send_cmd("AT","OK",50);//检测状态res = atk_8266_send_cmd("AT+CWMODE=1","OK",50);//设置WIFI STA modeif(res){printf("AT+CWMODE=1 error\n");}res = atk_8266_send_cmd("AT+CIPSNTPCFG=1,8,\"ntp1.aliyun.com\"", "OK", 50); //设置阿里云的参数if(res){printf("AT+CIPSNTPCFG=1 error\n");}sprintf((char*)p,"AT+CWJAP=\"%s\",\"%s\"",wifista_ssid,wifista_password);//连接wifires = atk_8266_send_cmd(p,"OK",1000); if(res){printf("AT+CWJAP error\r\n");}/*设置阿里云上的设备的用户用和密码,可以用阿里云配置工具生成*/res = atk_8266_send_cmd("AT+MQTTUSERCFG=0,1,\"NULL\",\"smart-door&a1vlcNvprSS\",\"FA5EC5AB4CB947695E16C77F718B8EC5669496EE\",0,0,\"\"", "OK", 50); //aliyunif(res){printf("MQTTUSERCFG error\r\n");}/*设置阿里云的clinetID,可以用阿里云配置工具生成*/res = atk_8266_send_cmd("AT+MQTTCLIENTID=0,\"kayshi|securemode=3\\,signmethod=hmacsha1\\,timestamp=4546|\"", "OK", 100); //aliyun if(res){printf("MQTTCLIENTID error\r\n");}/*连接阿里云*/res = atk_8266_send_cmd("AT+MQTTCONN=0,\"a1Py84SEWWJ.iot-as-mqtt.cn-shanghai.aliyuncs.com\",1883,1", "OK", 100); //aliyunif(res){printf("AT+MQTTCONN=0 error\r\n");}/*订阅一个主题*/res = atk_8266_send_cmd("AT+MQTTSUB=0,\"/a1vlcNvprSS/smart-door/user/test\",1", "OK", 100);if(res){printf("AT+MQTTSUB error\r\n");}/*订阅物模型的主题*/res = atk_8266_send_cmd("AT+MQTTSUB=0,\"/sys/smart-door/smart-door/thing/event/property/post_reply\",1", "OK", 100);if(res){printf("AT+MQTTSUB error\r\n");} // res = atk_8266_send_cmd("AT+MQTTPUB=0,\"/sys/a1vlcNvprSS/smart-door/thing/event/property/post\",\"{params:{\\\"LightSwitch\\\":1}}\",0,0", "OK", 100); //发布topic数据 // if(res){ // printf("AT+MQTTPUB error\r\n"); // } // res = atk_8266_send_cmd("AT+MQTTPUB=0,\"/a1vlcNvprSS/smart-door/user/test\",\"data\",0,0", "OK", 100); //发布topic数据 // if(res){ // printf("AT+MQTTPUB error\r\n"); // }while(1) {/*每过1秒,物模型主题发送一次数据*/if((t%100) == 0) {sprintf((char*)p,"AT+MQTTPUB=0,\"/sys/a1vlcNvprSS/smart-door/thing/event/property/post\",\"{params:{\\\"CurrentTemperature_14\\\":%d}}\",0,0", i);//设置无线参数:ssid,密码res = atk_8266_send_cmd(p, "OK", 100); //aliyun if(res){printf("AT+MQTTPUB wumo error\r\n");}i++;if(i > 31) i = 0;}/*通过uart3 接受ESP8266数据*/if(USART3_RX_STA&0X8000) //接收到一次数据了{ rlen=USART3_RX_STA&0X7FFF; //得到本次接收到的数据长度USART3_RX_BUF[rlen]=0; //添加结束符 printf("%s",USART3_RX_BUF); //发送到串口 // sprintf((char*)p,"收到%d字节,内容如下",rlen);//接收到的字节数 str = strtok(USART3_RX_BUF, ",");//以逗号分隔字符串while(str){printf("%s\r\n",str);//打印分隔字符串str = strtok(NULL, ",");//继续分隔,这里设置为NULL是为了分隔剩下的字符串if (strstr(str, "LightSwitch")){//判断字符串中是否由有LightSwitch,如何存在则根据后面的值是0或1来决定开关灯printf("===============>%s\r\n",str);if(str[24] == '1') {/*开灯*/printf("===============>%c\r\n",str[24]);GPIO_ResetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);//GPIOF9,F10设置高,灯灭}else{/*关灯*/printf("===============>%c\r\n",str[24]);GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);//GPIOF9,F10设置高,灯灭}}}USART3_RX_STA=0;}t++;delay_ms(10);}return 0; }

4:设备上电查看结果

1)产品下查看物模型的值

2)在Iot studio的wed开发中,创建项目,添加控件关联上面的物模型
可以看到计数的值在不断变化
而且可以通过开关按钮来进行控制led灯的开关

代码:链接

总结

以上是生活随笔为你收集整理的ESP8266-连接阿里云示例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。