IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> ESP8266 连接 MQTT -> 正文阅读

[嵌入式]ESP8266 连接 MQTT

ESP8266 连接 MQTT

主控芯片:MM32F2377 (MB-039)

WiFi 适配器:ESP8266

开发环境:IAR 7.80.4

MQTT 模拟服务器:MQTT.fx

MQTT

MQTT is an OASIS standard messaging protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish/subscribe messaging transport that is ideal for connecting remote devices with a small code footprint and minimal network bandwidth. MQTT today is used in a wide variety of industries, such as automotive, manufacturing, telecommunications, oil and gas, etc.

—— mqtt.org

MQTT(Message Queuing Telemetry Transport) 消息队列遥测传输协议,是一个基于客户端-服务器的消息发布/订阅传输协议。主要的概念有5个:

  • Broker 代理:MQTT 服务器
  • Publish 发布者:客户端
  • Subscribe 订阅者:客户端,可订阅多个 topic
  • Topic 主题:消息的类型,订阅主题之后就可以收到该 topic 的消息内容即 payload
  • Payload 消息内容:具体的内容

MQTT 模拟服务器:

MQTT 模拟服务器客户端使用

我在这里使用的是第二个 MQTT.fx,用这个客户端模拟一下 MQTT 的 Publish/Subscribe。

  1. 首先在 EMQ 官网上找到 MQTT 接入信息,主要是 Broker 和 TCP 端口号

    emq_broker
    在接下来的模拟服务器使用中,Broker Address 可以直接用 broker-cn.emqx.io,也可以到查询网上查找对应的 IP 地址:47.111.117.220

    EMQ 分为在国内有专门的服务器,连接更方便一些,国外的 Broker Address 是 broker.emqx.io

  2. 打开 MQTT.fx,点击齿轮状的设置按钮,填写 Broker Address,Port 等,再到 User Credentials 里填写用户名和密码,OK,Connect

    mqttx_conn

  3. 连接成功后,发布消息:填 topic,我这里 topic 叫 esp8266,以 json 格式进行发送,点击 Publish

    mqttfx_pub

  4. 去订阅消息,切换到 Subscribe,订阅 esp8266 这个 topic,然后再发布消息,就可以在这里看到了

    mqttfx_sub

ESP8266 + MQTT

ESP8266 的介绍以及基本使用可查看上一篇 CSDN 「Rose Island」MM32F3277 + ESP8266 使用指南(1. 实现 TCP Client 透传)

首先需要确认 AT 指令集具有 MQTT 指令:

具体的 MQTT AT 指令集可查阅:

ESP8266 MQTT 配置指令

  1. 配置 STA 模式:AT+CWMODE=1

  2. 连接路由器:AT+CWJAP="wifiName","wifiPsd"

  3. 配置 MQTT 属性:AT+MQTTUSERCFG=0,1,"clientID","username","password",0,0,""

  4. 连接制定的 MQTT broker:AT+MQTTCONN=0,"47.111.117.220",1883,0

  5. 查询 MQTT 连接状态:AT+MQTTCONN?

  6. 订阅 topic 数据:AT+MQTTSUB=0,"topicName",1

  7. 发布 topic 数据:AT+MQTTPUB=0,"topicName","payload",1,0

MM32F3277 + ESP8266 + MQTT

这里放的是与 MQTT 相关的函数,ESP8266 连网和 TCP 连接等函数可见「Rose Island」MM32F3277 + ESP8266 使用指南(1. 实现 TCP Client 透传)

esp8266.c


/// @brief  Configure ESP8266 MQTT parameters.
/// @param  clientId: Client ID num.
/// @param  username: MQTT server username to log in MQTT broker.
/// @param  psd: MQTT server password to log in MQTT broker.
/// @param  scheme: Verification methods.
/// @retval None.

void ESP8266_MQTT_Init(char* clientId, char* username, char* psd, ESP8266_MQTT_Scheme_Typedef scheme)
{
    ESP8266_MQTT_InitStruct.ipAddr.mqttIPAddr0 = ESP8266_MQTT_HOST_IPADDR_0;
    ESP8266_MQTT_InitStruct.ipAddr.mqttIPAddr1 = ESP8266_MQTT_HOST_IPADDR_1;
    ESP8266_MQTT_InitStruct.ipAddr.mqttIPAddr2 = ESP8266_MQTT_HOST_IPADDR_2;
    ESP8266_MQTT_InitStruct.ipAddr.mqttIPAddr3 = ESP8266_MQTT_HOST_IPADDR_3;
    ESP8266_MQTT_InitStruct.port = (uint32_t)ESP8266_MQTT_PORT;
    ESP8266_MQTT_InitStruct.clientID = clientId;
    ESP8266_MQTT_InitStruct.username = username;
    ESP8266_MQTT_InitStruct.password = psd;
    ESP8266_MQTT_InitStruct.scheme = scheme;
    ESP8266_MQTT_InitStruct.mqttStatus = NO_INIT;
}


/// @brief  Connect with MQTT server.
/// @param  clientId: Client ID num.
/// @param  username: MQTT server username to log in MQTT broker.
/// @param  psd: MQTT server password to log in MQTT broker.
/// @param  scheme: Verification methods.
/// @retval ESP8266 OK or ERROR.

ESP8266_Error_Typedef ESP8266_Connect_MQTT(char* clientId, char* username, char* psd, ESP8266_MQTT_Scheme_Typedef scheme)
{
    ESP8266_MQTT_Init(clientId, username, psd, scheme);

    char *cmd = (char*)malloc(256);
    sprintf(cmd, "AT+MQTTUSERCFG=0,1,\"%s\",\"%s\",\"%s\",0,0,\"\"", clientId, username, psd);

    if(ESP8266_Send_AT_Command_Times(cmd, "OK", 0xffff, 2)){
        printf("Error: Fail to Configure MQTT \n");
        return ESP8266_ERROR;
    }else{
        ESP8266_MQTT_InitStruct.mqttStatus = USERCFG_SET;
        printf("OK: Configure MQTT successfully \n");
    }

    sprintf(cmd, "AT+MQTTCONN=0,\"%d.%d.%d.%d\",%d,0", ESP8266_MQTT_HOST_IPADDR_0, ESP8266_MQTT_HOST_IPADDR_1, ESP8266_MQTT_HOST_IPADDR_2, ESP8266_MQTT_HOST_IPADDR_3, ESP8266_MQTT_PORT);
    if(ESP8266_Send_AT_Command_Times(cmd, "OK", 0xffff, 2)){
        ESP8266_MQTT_InitStruct.mqttStatus = DISCONNECTED;
        printf("Error: Fail to Connect MQTT \n");
        return ESP8266_ERROR;
    }else{
        ESP8266_MQTT_InitStruct.mqttStatus = CONNECTED;
        printf("OK: Connect MQTT successfully \n");
    }

    return ESP8266_OK;
}


/// @brief  Check MQTT client state.
/// @param  None.
/// @retval ESP8266 OK or ERROR.

ESP8266_Error_Typedef ESP8266_Check_MQTT()
{
    char *cmd = "AT+MQTTCONN?";

    if(ESP8266_Send_AT_Command_Times(cmd, "OK", 0xffff, 2)){
        // printf("Error: Fail to Connect MQTT \n");
        return ESP8266_ERROR;
    }else{
        // printf("OK: Connect MQTT successfully \n");
    }

    return ESP8266_OK;
}


/// @brief  Subscribe topic from MQTT server.
/// @param  subTopic: Subscribed topic name.
/// @retval ESP8266 OK or ERROR.

ESP8266_Error_Typedef ESP8266_MQTT_SUB(char* subTopic)
{
    char *cmd = (char*)malloc(strlen("AT+MQTTSUB=0,\"%s\",1") + strlen(subTopic));
    sprintf(cmd, "AT+MQTTSUB=0,\"%s\",1", subTopic);

    if(ESP8266_Send_AT_Command_Times(cmd, "OK", 0xffff, 2)){
        ESP8266_MQTT_InitStruct.mqttStatus = CONNECTED_NO_SUB;
        printf("Error: Fail to Subscribe topic \n");
        return ESP8266_ERROR;
    }else{
        ESP8266_MQTT_InitStruct.mqttStatus = CONNECTED_AND_SUB;
        printf("OK: Subscribe topic successfully \n");
    }

    return ESP8266_OK;
}


/// @brief  Unsubscribe topic from MQTT server.
/// @param  subTopic: Unsubscribed topic name.
/// @retval ESP8266 OK or ERROR.

ESP8266_Error_Typedef ESP8266_MQTT_UNSUB(char* subTopic)
{
    char *cmd = (char*)malloc(strlen("AT+MQTTUNSUB=0,\"\"") + strlen(subTopic));
    sprintf(cmd, "AT+MQTTUNSUB=0,\"%s\"", subTopic);

    if(ESP8266_Send_AT_Command_Times(cmd, "OK", 0xffff, 2)){
        ESP8266_MQTT_InitStruct.mqttStatus = CONNECTED_NO_SUB;
        printf("Error: Fail to Subscribe topic \n");
        return ESP8266_ERROR;
    }else{
        ESP8266_MQTT_InitStruct.mqttStatus = CONNECTED_AND_SUB;
        printf("OK: Subscribe topic successfully \n");
    }

    return ESP8266_OK;
}


/// @brief  Publish topic to MQTT server.
/// @param  subTopic: Publish topic name.
/// @retval ESP8266 OK or ERROR.

ESP8266_Error_Typedef ESP8266_MQTT_PUB(char* pubTopic, char* data)
{
    char *cmd = (char*)malloc(strlen("AT+MQTTPUB=0,\"\",\"\",1,0") + strlen(pubTopic) + strlen(data));
    sprintf(cmd, "AT+MQTTPUB=0,\"%s\",\"%s\",1,0", pubTopic, data);

    if(ESP8266_Send_AT_Command_Times(cmd, "OK", 0xffff, 2)){
        printf("Error: Fail to Publish topic \n");
        return ESP8266_ERROR;
    }else{
        printf("OK: Publish topic successfully \n");
    }

    return ESP8266_OK;
}


/// @brief  Disconnect with MQTT server.
/// @param  None.
/// @retval ESP8266 OK or ERROR.

ESP8266_Error_Typedef ESP8266_MQTT_CLEAN()
{
    char *cmd = "AT+MQTTCLEAN=0";

    if(ESP8266_Send_AT_Command_Times(cmd, "OK\r\n", 0xffff, 2)){
        printf("Error: Fail to Disconnect MQTT \n");
        return ESP8266_ERROR;
    }else{
        ESP8266_MQTT_InitStruct.mqttStatus = NO_INIT;
        printf("OK: Disconnect MQTT successfully \n");
    }

    return ESP8266_OK;
}


/// @brief  Send key-value to MQTT.
/// @param  dataSend: The whole key-value.
/// @param  deviceId: Device id.
/// @param  deviceType: Device type.
/// @param  dataType: Data type.
/// @param  dataValue: Data value.
/// @retval None.

void ESP8266_Data_Send(char *dataSend, char *deviceId, char *deviceType, char *dataType, float dataValue)
{
    sprintf(dataSend,"{\r\n\"device_id\":\"%s\",\r\n\"device_type\":\"%s\",\r\n\"data_type\":\"%s\",\r\n\"data_value\":\"%.2f\"\r\n}",
                        deviceId, deviceType, dataType, dataValue);
    printf("data sent is %s \n", dataSend);
}

esp8266.h

typedef enum{
    NO_INIT                             = (uint8_t)0,
    USERCFG_SET                         = (uint8_t)1,
    CONNCFG_SET                         = (uint8_t)2,
    DISCONNECTED                        = (uint8_t)3,
    CONNECTED                           = (uint8_t)4,
    CONNECTED_NO_SUB                    = (uint8_t)5,
    CONNECTED_AND_SUB                   = (uint8_t)6
} ESP8266_MQTT_Status_Typedef;

typedef enum{
    NO_TLS                              = (uint8_t)1,
    TLS_NO_CA                           = (uint8_t)2,
    TLS_SERVICE_CA                      = (uint8_t)3,
    TLS_CLIENT_CA                       = (uint8_t)4,
    TLS_SERVICE_CLIENT_CA               = (uint8_t)5,
    WS_TCP                              = (uint8_t)6,
    WSS_NO_CA                           = (uint8_t)7,
    WSS_SERVICE_CA                      = (uint8_t)8,
    WSS_CLIENT_CA                       = (uint8_t)9,
    WSS_SERVICE_CLIENT_CA               = (uint8_t)10
} ESP8266_MQTT_Scheme_Typedef;

#define ESP8266_MQTT_CLIENTIP           "mm32f3270"
#define ESP8266_MQTT_USERNAME           "mm32"
#define ESP8266_MQTT_PSD                "mm32psd"
#define ESP8266_MQTT_HOST               "broker-cn.emqx.io"
#define ESP8266_MQTT_HOST_IPADDR_0      47
#define ESP8266_MQTT_HOST_IPADDR_1      111
#define ESP8266_MQTT_HOST_IPADDR_2      117
#define ESP8266_MQTT_HOST_IPADDR_3      220
#define ESP8266_MQTT_PORT               1883
#define ESP8266_MQTT_TOPIC              "esp8266"

typedef struct
{
    uint8_t                             mqttIPAddr0;
    uint8_t                             mqttIPAddr1;
    uint8_t                             mqttIPAddr2;
    uint8_t                             mqttIPAddr3;
}ESP8266_MQTT_IPADDR_Typedef;

typedef struct
{
    ESP8266_MQTT_IPADDR_Typedef         ipAddr;
    uint32_t                            port;
    char*                               clientID;
    char*                               username;
    char*                               password;
    ESP8266_MQTT_Scheme_Typedef         scheme;
    ESP8266_MQTT_Status_Typedef         mqttStatus;
} ESP8266_MQTT_Typedef;
ESP8266_MQTT_Typedef ESP8266_MQTT_InitStruct;

Conclusion

🙏🙏🙏🙏🙏🙏🙏

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2022-05-21 19:08:40  更:2022-05-21 19:09:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 0:34:59-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码