一、简介
1.这期视频向大家介绍如何使用CW32单片机通过ESP8266连接OneNET物联网平台,发送温湿度数据到平台,并接受平台的云控制。
二、所需物料
2.本实验使用到了CW32-48F大学计划开发板、DHT11温湿度传感器模块、ESP8266WIFI模块及Keil5开发环境。
3.接线:根据原理图,把模块连接到开发板上
(1)ESP8266WIFI模块:
开发板上预留了该模块接口,通过串口向ESP8266写入指令。
(2)DHT11模块:开发板上预留了该模块接口
4.(1)MQTT协议介绍:
MQTT是一种轻量级的发布/订阅消息传输协议,常用于物联网领域。它的特点是简化和减少了网络流量,可以高效地进行数据传输。在本次应用中,我们将使用MQTT协议进行数据上传。
(2)OneNET代码介绍:
在使用OneNet平台时,我们需要使用TCP协议进行连接。首先需要设置好TCP协议的相关参数,然后通过POST方法将温湿度数据上传到OneNet平台中。在上传数据时需要使用OneNet平台提供的API接口进行数据传输。
注:#define PROID "XXX" //xxx为产品ID
#define ACCESS_KEY "XXX" //xxx为acess_key访问密钥
#define DEVICE_NAME "XXX" //xxx为设备名称
三、核心代码
main.c
int main(void) { unsigned short timeCount = 0; //发送间隔变量 unsigned char *dataPtr = NULL; Hardware_Init(); //初始化外围硬件 while(DHT11_Init()) //检测DHT11 Delay_ms(200); ESP8266_Init(); //初始化ESP8266 UsartPrintf(USART_DEBUG, "Connect MQTTs Server... "); while(ESP8266_SendCmd(ESP8266_ONENET_INFO, "CONNECT")) //ESP8266联网 Delay_ms(500); UsartPrintf(USART_DEBUG, "NET_OK "); while(OneNet_DevLink()) //接入OneNET Delay_ms(500); OneNET_Subscribe(); //订阅消息传输,允许接受下发指令 TFTShowString(0,0," OneNET "); //界面 TFTShowString(1,0," 智能物联平台 "); TFTShowString(3,0,"humidity: %"); TFTShowString(4,0,"temperature: C"); while(1) { if(++timeCount >= 50) { DHT11_Read_Data(&temperature,&humidity); //读取温湿度值 TFTShowNumber(3,10,humidity); TFTShowNumber(4,12,temperature); UsartPrintf(USART_DEBUG,"TEMP:%d HUMI:%d ",temperature,humidity); UsartPrintf(USART_DEBUG, "OneNet_SendData "); OneNet_SendData(); //数据上传 timeCount = 0; ESP8266_Clear(); //清除缓存 } dataPtr = ESP8266_GetIPD(0); //接收OneNET平台返回的数据 if(dataPtr != NULL) OneNet_RevPro(dataPtr); //确认返回数据,执行下发命令 Delay_ms(10); } }Onenet.c:
#define PROID "3OB4R7cKCw" //产品ID(名称) #define ACCESS_KEY "n7GptYNpf9U97zHTz2ltc1Pvy4GB+0MXsU1nKel9ixc=" //产品访问密钥 #define DEVICE_NAME "111" //设备ID(名称) void OneNet_RevPro(unsigned char *cmd) { char *req_payload = NULL; char *cmdid_topic = NULL; unsigned short topic_len = 0; unsigned short req_len = 0; unsigned char qos = 0; static unsigned short pkt_id = 0; unsigned char type = 0; short result = 0; char *dataPtr = NULL; char numBuf[10]; int num = 0; type = MQTT_UnPacketRecv(cmd); switch(type) { case MQTT_PKT_PUBLISH: //接收的Publish消息 result = MQTT_UnPacketPublish(cmd, &cmdid_topic, &topic_len, &req_payload, &req_len, &qos, &pkt_id); if(result == 0) { char *data_ptr = NULL; UsartPrintf(USART_DEBUG, "topic: %s, topic_len: %d, payload: %s, payload_len: %d ", cmdid_topic, topic_len, req_payload, req_len); data_ptr = strstr(cmdid_topic, "request/"); //查找cmdid if(data_ptr) { char topic_buf[80], cmdid[40]; data_ptr = strchr(data_ptr, '/'); data_ptr++; memcpy(cmdid, data_ptr, 36); //复制cmdid cmdid[36] = 0; snprintf(topic_buf, sizeof(topic_buf), "$sys/%s/%s/cmd/response/%s", PROID, DEVICE_NAME, cmdid); OneNET_Publish(topic_buf, "ojbk"); //回复命令 } } case MQTT_PKT_PUBACK: //发送Publish消息,平台回复的Ack if(MQTT_UnPacketPublishAck(cmd) == 0) UsartPrintf(USART_DEBUG, "Tips: MQTT Publish Send OK "); break; case MQTT_PKT_SUBACK: //发送Subscribe消息的Ack if(MQTT_UnPacketSubscribe(cmd) == 0) UsartPrintf(USART_DEBUG, "Tips: MQTT Subscribe OK "); else UsartPrintf(USART_DEBUG, "Tips: MQTT Subscribe Err "); break; default: result = -1; break; } ESP8266_Clear(); //清空缓存 if(result == -1) return; dataPtr = strchr(req_payload, ':'); //搜索':' if(dataPtr != NULL) //如果找到了 { dataPtr++; while(*dataPtr >= '0' && *dataPtr <= '9') //判断是否是下发的命令控制数据 { numBuf[num++] = *dataPtr++; } numBuf[num] = 0; num = atoi((const char *)numBuf); //转为数值形式 if(strstr((char *)req_payload, "light")) //搜索"light",判断":"后面的数值进行控制 { if(num == 1) //控制数据如果为1,代表开 { UsartPrintf(USART_DEBUG, "接收到1 "); //开灯 PA07_SETLOW(); TFTShowString(5,0,"light: ON "); } else if(num == 0) //控制数据如果为0,代表关 { UsartPrintf(USART_DEBUG, "接收到0 "); //关灯 PA07_SETHIGH(); TFTShowString(5,0,"light: OFF"); } } } if(type == MQTT_PKT_CMD || type == MQTT_PKT_PUBLISH) { MQTT_FreeBuffer(cmdid_topic); MQTT_FreeBuffer(req_payload); } }审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !