zoukankan      html  css  js  c++  java
  • Centos8上搭建EMQ MQTT

    layout:     post
    title:      Centos8上搭建EMQ MQTT
    subtitle:   在阿里云Centos8搭建EMQ并配置接入
    date:       2020-3-11
    author:     Dapenson
    header-img: img/post-bg-kuaidi.jpg
    catalog: true
    tags:
        - MQTT
        - EMQ
        - 物联网
    

    Centos8上搭建EMQ MQTT服务器

    我的版本CentOS Linux release 8.1.1911 (Core)_x64、EMQX_v3.2.2

    一 使用储存库安装 EMQ X

    官方参考文档

    1 安装所需要的依赖包
     sudo yum install -y yum-utils device-mapper-persistent-data lvm2
    
    2 使用以下命令设置稳定存储库
    sudo yum-config-manager --add-repo https://repos.emqx.io/emqx-ce/redhat/centos/7/emqx-ce.repo
    
    3 安装特定版本的 EMQ X

    3.1. 查询可用版本

     yum list emqx --showduplicates | sort -r
    
    emqx.x86_64                     3.1.0-1.el7                        emqx-stable
    emqx.x86_64                     3.0.1-1.el7                        emqx-stable
    emqx.x86_64                     3.0.0-1.el7                        emqx-stable
    

    3.2. 根据第二列中的版本字符串安装特定版本,例如 3.1.0

     sudo yum install emqx-3.2.2
    

    image-20200310224342886

    5 启动 EMQ X
    • 直接启动

      运行emqx
      emqx start
          emqx 3.1.0 is started successfully!
      
      查看运行状态
      emqx_ctl status
          Node 'emqx@127.0.0.1' is started
          emqx 3.2.2 is running
      

    image-20200310224502664

    启动后,打开服务器地址+18083即可看到后台管理页面,默认用户名/密码为admin/public

    image-20200310225534026

    当然前提是该端口已经打开,如果您是阿里云的服务器,则需要手动打开该端口

    • 登录ECS管理控制台
    • 在左侧导航栏,单击网络与安全 > 安全组
    • 在顶部状态栏左上角处,选择地域。
    • 找到要配置授权规则的安全组,在操作列中,单击配置规则

    6 常用命令

    查看端口信息
    emqx_ctl listeners
    
    运行
    emqx start
    
    重启
    emqx restart 
    
    查看运行状态
    emqx_ctl status
    
     emqx {start|start_boot <file>|ertspath|foreground|stop|restart|reboot|pid|ping|console|console_clean|console_boot <file>|attach|remote_console|upgrade|escript|rpc|rpcterms|eval}
    
    

    二 配置EMQ X

    打开文件etc/emqx/emqx.conf进行配置修改

    配置端口

    在安装以后,EMQ X 默认会使用以下端口:

    • 1883: MQTT 协议端口
    • 8883: MQTT/SSL 端口
    • 8083: MQTT/WebSocket 端口
    • 8080: HTTP API 端口
    • 18083: Dashboard 管理控制台端口

    按照安装环境需要,可以修改以上端口。

    修改协议端口请编辑 EMQ X 系统配置文件,找到以下各行,并按需要修改端口号:这里由于和mosquitomqtt端口号冲突,因此我将其修改为61883

    listener.tcp.external = 0.0.0.0:1883
    
    listener.ssl.external = 8883
    
    listener.ws.external = 8083
    

    修改 HTTP API 端口请编辑 emqx_management 插件的配置文件etc/emqx/plugins/emqx_management.conf,找到下述行,并按需修改端口号:

    management.listener.http = 8080
    

    修改 Dashboard 管理控制台端口请编辑 emqx_dashboard 插件的配置文件etc/emqx/plugins/emqx_dashboard.conf, 找到下述行,并按需修改端口号:

    dashboard.listener.http = 18083
    

    在 Web Dashboard 的 MANAGEMENT -> Listeners 菜单下,可以查看现在正在使用的监听器端口和属性。

    image-20200311130509931

    配置插件

    pass

    三 接入EMQX

    使用MQTT.fx进行接入

    1. 配置地址和端口

    image-20200311131918507

    1. 订阅任意Topic

    image-20200311132236242

    1. 订阅之后Dashboard即可查看您的Topic

      image-20200311132722680

      image-20200311132726267

    image-20200311133030304

    规则转发

    Topic间转发
    1. 在dashboard建立消息转发规则如下

    image-20200311133642894

    1. 发布消息到emqx_pub

    image-20200311133730116

    1. 检查emqx_display

    image-20200311133836716

    任意MQTT云平台桥接转发

    新建规则响应动作如图进行设置

    image-20200311135149243

    image-20200311135219639

    image-20200311135229971

    将你的其他mqtt云平台接入信息填入,点击测试连接即可

    使用NodeMCU进行接入

    使用ArduinoIDE进行编译上传如下代码

    8266_pubsubclient_mqtt.ino
    /**********************************************************************
    项目名称/Project          : 然也物联 / Ranye-Iot
    程序名称/Program name     : 8266_pubsubclient_mqtt
    团队/Team                : 太极创客团队 / Taichi-Maker (www.taichi-maker.com)
    作者/Author              : Dapenson
    日期/Date(YYYYMMDD)     : 202003011
    程序目的/Purpose          : 
    使用基础的MQTT库与EMQ物联网平台进行设备端接入和消息发布、订阅。
    (请注意下方"其它说明"中重要信息!)
    -----------------------------------------------------------------------
    修订历史/Revision History  
    日期/Date    作者/Author      参考号/Ref    修订说明/Revision Description
    -----------------------------------------------------------------------
    其它说明:
    1. 为了保护个人隐私,此程序连接wifi所涉及的WiFI名称和密码,以及 EMQ物联网平台密钥都存储于info.h文件中。
    info.h文件在git仓库中处于.gitignore忽略清单中,故仓库中无info.h文件。为了便于团队成员开发,
    可使用本程序同文件夹中的info_template.h文件,并且将wifi信息和密钥信息填入该文件相应位置,
    再将info_template.h改名为info.h后即可使用本程序成功编译使用。
    
    2. 此程序需要安装PubSubClient库并对其中头文件进行修改,具体参考此程序md文档
    ***********************************************************************/
    
    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
    #include "info.h"
    
    
    WiFiClient espClient;
    PubSubClient client(espClient);
    unsigned long lastMsg = 0;
    #define MSG_BUFFER_SIZE	(50)
    char msg[MSG_BUFFER_SIZE];
    int value = 0;
    
    void setup_wifi() {
    
      delay(10);
      // We start by connecting to a WiFi network
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
    
      WiFi.begin(ssid, password);
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      randomSeed(micros());
    
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
    
    void callback(char* topic, byte* payload, unsigned int length) {
      Serial.print("Message arrived [");
      Serial.print(topic);
      Serial.print("] ");
      for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
      }
      Serial.println();
    
      // Switch on the LED if an 1 was received as first character
      if ((char)payload[0] == '1') {
        digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
        // but actually the LED is on; this is because
        // it is active low on the ESP-01)
      } else {
        digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
      }
    
    }
    
    void reconnect() {
      // Loop until we're reconnected
      while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        
        // 开始连接
        if (client.connect(mqtt_clientID, mqtt_username , mqtt_userpassword)) {
          Serial.println("connected");
         
          // 连接成功之后发布一条信息
          client.publish(topic_pub, "中文汉字测试,当前已连接云服务器");
          Serial.println("中文汉字测试,当前已连接云服务器");
         
          //订阅Topic
          client.subscribe(topic_sub);
        } else {
          Serial.print("failed, rc=");
          Serial.print(client.state());
          Serial.println(" try again in 5 seconds");
          // Wait 5 seconds before retrying
          delay(5000);
        }
      }
    }
    
    void setup() {
      pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
      Serial.begin(9600);
      setup_wifi();
      client.setServer(mqtt_server, mqtt_port);
      client.setCallback(callback);
    }
    
    void loop() {
    
      if (!client.connected()) {
        reconnect();
      }
      client.loop();
    
      unsigned long now = millis();
      if (now - lastMsg > 2000) {
        lastMsg = now;
        ++value;
        snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
        Serial.print("Publish message: ");
        Serial.println(msg);
        client.publish(topic_pub, msg);
      }
    }
    
    
    info.h
    //填入您的WIFI连接信息
    #define  ssid             "xxx" 
    #define  password         "xxx" 
    
    
    //填入您的云服务器接入信息
    #define  mqtt_server           "iot.dapenson.xyz" 
    #define  mqtt_port              61883
    #define  mqtt_clientID         "device_Dapenson" 
    #define  mqtt_username         "Dapenson" 
    #define  mqtt_userpassword     "Dapenson" 
    
    #define  topic_sub             "user/get" 
    #define  topic_pub             "user/update" 
     
    
    
  • 相关阅读:
    114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)
    101在检索框中添加一个书签按钮(扩展知识:在检索框中添加一个范围条)
    088实现自动倒计时功能
    086设置日期选择器框的显示样式
    Shell if else
    Shell数组
    Shell字符串
    Shell运算符
    数据挖掘标准流程规范
    Shell转义字符与变量替换
  • 原文地址:https://www.cnblogs.com/dapenson/p/12822570.html
Copyright © 2011-2022 走看看