一、簡介
1.這期視頻向大家介紹如何使用CW32單片機通過ESP8266連接OneNET物聯網平臺,發送溫濕度數據到平臺,并接受平臺的云控制。
二、所需物料
2.本實驗使用到了CW32-48F大學計劃開發板、DHT11溫濕度傳感器模塊、ESP8266WIFI模塊及Keil5開發環境。
CW32-48F大學計劃板
DHT11溫濕度傳感器模塊
ESP8266WIFI模塊
ESP8266WIFI模塊引腳分布
實物圖
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...rn");
while(ESP8266_SendCmd(ESP8266_ONENET_INFO, "CONNECT")) //ESP8266聯網
Delay_ms(500);
UsartPrintf(USART_DEBUG, "NET_OKrn");
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:%drn",temperature,humidity);
UsartPrintf(USART_DEBUG, "OneNet_SendDatarn");
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: %drn", 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 OKrn");
break;
case MQTT_PKT_SUBACK: //發送Subscribe消息的Ack
if(MQTT_UnPacketSubscribe(cmd) == 0)
UsartPrintf(USART_DEBUG, "Tips: MQTT Subscribe OKrn");
else
UsartPrintf(USART_DEBUG, "Tips: MQTT Subscribe Errrn");
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, "接收到1rn");
//開燈
PA07_SETLOW();
TFTShowString(5,0,"light: ON ");
}
else if(num == 0) //控制數據如果為0,代表關
{
UsartPrintf(USART_DEBUG, "接收到0rn");
//關燈
PA07_SETHIGH();
TFTShowString(5,0,"light: OFF");
}
}
}
if(type == MQTT_PKT_CMD || type == MQTT_PKT_PUBLISH)
{
MQTT_FreeBuffer(cmdid_topic);
MQTT_FreeBuffer(req_payload);
}
}
附件:基于CW32的物聯網應用源碼.zip
審核編輯:湯梓紅
-
物聯網
+關注
關注
2909文章
44557瀏覽量
372753 -
OneNET
+關注
關注
1文章
51瀏覽量
12800 -
單片機
+關注
關注
0文章
214瀏覽量
16658 -
CW32
+關注
關注
1文章
203瀏覽量
626
發布評論請先 登錄
相關推薦
評論