ESP8266-NONOS_SDK的MQTT对接Onenet平台

概述

mqtt协议,对接的是onenet平台开放的mqtt,该平台优点是使用mqtt测试无压力,自己搭建的mqtt服务器不能保证。

详细

概述

物联网时代来临,仅仅知道TCP、UDP、HTTP等这些是不行的,最主要核心的协议MQTT已经渐渐登场了!在原本自己搭建的MQTT服务器之后,由于设备多,故而服务器频临崩溃,所哟不得不依靠强大的ONENET平台的MQTT,毫无压力!然后在最初之时,ONENET论坛里很难找到关于MQTT正确连接的协议信息,所以自己摸索,编写,测试等各个环节,终于成功对接MQTT了!下面我来简单介绍这个简单的Demo吧!

详细

一、运行效果

628.png

2738.png

二、实现过程

①、成为ONENET平台的开发者

  这个具体的不做细分介绍,我给你个链接,自己去注册下吧,超简单!
  https://jingyan.baidu.com/article/0a52e3f4efe75bbf62ed7201.html
  https://open.iot.10086.cn/doc/book/easy-manual/login.html

②、获取MQTT相关的信息

主要需要得到以下几个信息:

  1. 设备ID与鉴权信息;

    在“产品开发”左侧栏目点击“设备列表”,点击页面里的“详情”就能看打设备id和鉴权信息。

    2.png

  2. 产品ID。

    1.png

     

③、代码

主要的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

如图:

3.png

三、项目结构图

4.png

四、补充

该程序只是简单的MQTT对接Onenet平台,具体后面的订阅和主题,以及一键配网,还有json协议等我会再详细出一个demo出来的!

注意:ESP8266-12F或者ESP8266-12E的都行,其他的板子需要改变flash的大小。

详细源码链接在这里:http://www.demodashi.com/demo/15628.html 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软硬兼施虾米

你的打赏就是我前进更新的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值