ESP8266-连接阿里云示例
生活随笔
收集整理的這篇文章主要介紹了
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-连接阿里云示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html bootstrap主题,10大
- 下一篇: 游百望山(记于17.09.05)