概述
mqtt協議,對接的是onenet平臺開放的mqtt,該平臺優點是使用mqtt測試無壓力,自己搭建的mqtt服務器不能保證。
詳細
概述
物聯網時代來臨,僅僅知道TCP、UDP、HTTP等這些是不行的,最主要核心的協議MQTT已經漸漸登場了!在原本自己搭建的MQTT服務器之後,由於設備多,故而服務器頻臨崩潰,所喲不得不依靠強大的ONENET平臺的MQTT,毫無壓力!然後在最初之時,ONENET論壇裏很難找到關於MQTT正確連接的協議信息,所以自己摸索,編寫,測試等各個環節,終於成功對接MQTT了!下面我來簡單介紹這個簡單的Demo吧!
詳細
一、運行效果
二、實現過程
①、成爲ONENET平臺的開發者
這個具體的不做細分介紹,我給你個鏈接,自己去註冊下吧,超簡單! https://jingyan.baidu.com/article/0a52e3f4efe75bbf62ed7201.html https://open.iot.10086.cn/doc/book/easy-manual/login.html
②、獲取MQTT相關的信息
主要需要得到以下幾個信息:
-
設備ID與鑑權信息;
在“產品開發”左側欄目點擊“設備列表”,點擊頁面裏的“詳情”就能看打設備id和鑑權信息。
-
產品ID。
③、代碼
主要的MQTT的配置代碼如下:
#ifndef __MQTT_CONFIG_H__ #define __MQTT_CONFIG_H__ typedef enum{ NO_TLS = 0, // 0: disable SSL/TLS, there must be no certificate verify between MQTT server and ESP8266 TLS_WITHOUT_AUTHENTICATION = 1, // 1: enable SSL/TLS, but there is no a certificate verify ONE_WAY_ANTHENTICATION = 2, // 2: enable SSL/TLS, ESP8266 would verify the SSL server certificate at the same time TWO_WAY_ANTHENTICATION = 3, // 3: enable SSL/TLS, ESP8266 would verify the SSL server certificate and SSL server would verify ESP8266 certificate }TLS_LEVEL; /***********************************************************************************************************************/ #define CFG_HOLDER 0x00FF55A5 /* Change this value to load default configurations */ #define MQTT_HOST "183.230.40.39" // "183.230.40.39" 是ONENET平臺的MQTT統一的IP #define MQTT_PORT 6002 // ONENET的MQTT默認的端口 #define MQTT_CLIENT_ID "填設備Id" // the ID of yourself, any string is OK,client would use this ID register itself to MQTT server #define MQTT_USER "填產品ID" // your MQTT login name, if MQTT server allow anonymous login,any string is OK, otherwise, please input valid login name which you had registered #define MQTT_PASS "鑑權信息" // you MQTT login password, same as above #define STA_SSID "your AP/router SSID" // your AP/router SSID to config your device networking #define STA_PASS "your AP/router password" // your AP/router password #define DEFAULT_SECURITY NO_TLS // very important: you must config DEFAULT_SECURITY for SSL/TLS #define CA_CERT_FLASH_ADDRESS 0x77 // CA certificate address in flash to read, 0x77 means address 0x77000 #define CLIENT_CERT_FLASH_ADDRESS 0x78 // client certificate and private key address in flash to read, 0x78 means address 0x78000 /***********************************************************************************************************************/ /*Please Keep the following configuration if you have no very deep understanding of ESP SSL/TLS*/ #define CFG_LOCATION 0x79 /* Please don't change or if you know what you doing */ #define MQTT_BUF_SIZE 1024 #define MQTT_KEEPALIVE 120 /*second*/ #define MQTT_RECONNECT_TIMEOUT 5 /*second*/ #define MQTT_SSL_ENABLE //* Please don't change or if you know what you doing */ #define STA_TYPE AUTH_WPA2_PSK #define QUEUE_BUFFER_SIZE 2048 #define PROTOCOL_NAMEv311 /*MQTT version 3.1 compatible with Mosquitto v0.15*/ #endif
運行主程序代碼:
void user_init(void)
{
uart_init(BIT_RATE_115200, BIT_RATE_115200);
os_delay_us(60000);
CFG_Load();
MQTT_InitConnection(&mqttClient, sysCfg.mqtt_host, sysCfg.mqtt_port, sysCfg.security);
//MQTT_InitConnection(&mqttClient, "183.230.40.39", 6002, 0);
MQTT_InitClient(&mqttClient, sysCfg.device_id, sysCfg.mqtt_user, sysCfg.mqtt_pass, sysCfg.mqtt_keepalive, 1);
//MQTT_InitClient(&mqttClient, "client_id", "user", "pass", 120, 1);
MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0);
MQTT_OnConnected(&mqttClient, mqttConnectedCb);
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
MQTT_OnPublished(&mqttClient, mqttPublishedCb);
MQTT_OnData(&mqttClient, mqttDataCb);
WIFI_Connect("填WIFI路由器名稱", "填WIFI密碼", wifiConnectCb); //連接WIFI
INFO("\r\nSystem started ...\r\n");
}
④、在eclipse-cywbin編譯該源碼,產生固件在bin文件裏,燒錄。
燒錄方法: boot_v1.7.bin 0x00000 user1.4096.new.4.bin 0x01000 esp_init_data_default_v05.bin 0x3FC000 blank.bin 0x3FE000
如圖:
三、項目結構圖
四、補充
該程序只是簡單的MQTT對接Onenet平臺,具體後面的訂閱和主題,以及一鍵配網,還有json協議等我會再詳細出一個demo出來的!
注意:ESP8266-12F或者ESP8266-12E的都行,其他的板子需要改變flash的大小。
詳細源碼鏈接在這裏:http://www.demodashi.com/demo/15628.html