ติดตั้ง Mqtt broker to python3 to mysql on centos7

เรื่อง server เป็น centos 7 นะครับ

เริ่มแรกติดตั้ง Mqtt broker

yum install mosquitto

mkdir /var/lib/mosquitto/

chown mosquitto:mosquitto /var/lib/mosquitto/ -R

 

จากนั้นเข้าไปแก้ config เช่น ip หรือ port แต่น่าจะ set ไว้แล้ว

cd /etc/mosquitto/

vi mosquitto.conf

 

จากนั้น start Mqtt broker

mosquitto -c /etc/mosquitto/mosquitto.conf

จากนั้นต้องการลองว่า สามารถรับข้อมูลได้ไหมด้วยการ subscribe คำสั่งคือ

mosquitto_sub -v -t test_sub

คำว่า test_sub topic ที่เราเลือก ไม่สามารถรับทั้งหมดได้ อาจจะต้องใส่เพิ่ม

 

 

จากนั้นหากต้องการ ใส่ username password ทำได้ดังนี้

mosquitto_passwd -c /etc/mosquitto/passwd <user_name>

จากนั้นให้ใส่ password จากนั้นไป config ที่ไฟล์ 
vi /etc/mosquitto/mosquitto.conf

จากนั้นใส่ค่า
password_file /etc/mosquitto/passwd
allow_anonymous false

save แล้ว start service  mosquitto

 

หากต้องการ ลบ username ให้ใช้คำสั่ง

mosquitto_passwd -D /etc/mosquitto/passwd <user_name>

จากนั้น เราสามารถทำให้ใช้งานบน service และ ตั้ง start up ได้ ดังนี้

ไปสร้างไฟล์ vi /etc/systemd/system/mosquitto.service

copy ด้านล่างนี้ใส่ลงไปอาจจะตรวจที่อยู่ไฟล์ให้เรียบร้อย

[Unit]
Description=Mosquitto MQTT Broker
Documentation=man:mosquitto(8)
Documentation=man:mosquitto.conf(5)
ConditionPathExists=/etc/mosquitto/mosquitto.conf
After=xdk-daemon.service

[Service]
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
ExecReload=/bin/kill -HUP $MAINPID
User=mosquitto
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

จากนั้นตั้ง start up systemctl enable mosquitto.service
reboot เพื่อให้เริ่มใหม่

ทดสอบว่า run ไหมหลังจาก reboot เสร็จ

mosquitto -v

 

 

จากนั้นทำการติดตั้ง python3

จากนั้นติดตั้งส่วนเสริม mqtt ของ python3

pip3.6 install paho-mqtt

จากนั้นติดตั้ง mysql ของ python3

pip3.6 install PyMySQL

จากนั้นลองทดสอบด้วย ไฟล์ python นี้ หากมี error ก็ลองหาดูนะครับ 555

 

 

import paho.mqtt.client as mqtt #import the client1
import pymysql as mysql
db_server="127.0.0.1"
db_username="root"
db_password="123456"
db_name="lora_mqtt"


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))


    client.subscribe("emoncms/#")
    client.subscribe("cat/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):

    print(msg.topic, msg.payload)
    try:
        conn_zyxel = mysql.connect(db_server, db_username, db_password, db_name, charset='UTF8')
        cur_zyxel = conn_zyxel.cursor()
       
        cur_zyxel.execute("INSERT INTO tb_status_update ( topic_set, msg_json) VALUES (%s, %s)", (msg.topic, msg.payload))
        conn_zyxel.commit()
        conn_zyxel.close()
    except Exception as ex:
        print ("Error log_send_down ", str(ex))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set("username", "password")
client.connect("127.0.0.1", 1883, 60)


client.loop_forever()

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องที่ต้องการถูกทำเครื่องหมาย *