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" 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM