In this project the MQTT connection with the HiveMQ server will be visualized
This program works with MQTT conection of IOT in order to comunicate with three topics
In order to create a MQTT connection with Python We have to install pip install paho-mqtt Paho Library
The latest stable version is available in the Python Package Index (PyPi) and can be installed using
Or with virtualenv:
virtualenv paho-mqtt
source paho-mqtt/bin/activate
pip install paho-mqtt
After that, We need to open a Broker HIVEMQ Broker Address
The following parameters must be considered to establish the connection
- Host: broker.mqttdashboard.com
- Port: 1883 (Web Port)
- ClientID: This parameter is given by the user
- Username: This parameter is given by the user
- Password: This parameter is given by the user
- Topic: This parameter is given by the user
In order to create a desktop application, the Tkinter Framework which is already installed in Python was used, so only the necessary libraries will be called to make the application.
###### IMPORT LYBRARIES ########
import ssl # Establish secure connection
import sys
import paho.mqtt.client as mqtt # Connect with the MQTT Library
import time # Time Library
from tkinter import *
from tkinter import ttk, font # Import Tkinter Lybrary
from tkinter import messagebox
import getpass# Main program
if __name__ == "__main__":
topic = str(input("Topic: ")) # Input the topic in ""
state = str(input("Data: ")) # Input the data in ""
print(topic)
print(state)
client.subscribe(topic, qos=0)
client.publish(topic, state)
client.loop()Connection is stablish with:
host = "broker.mqttdashboard.com"
port 1883;
keepalive = 60;
clientid = "ClientID";
username = "Your Username";
password = "Your Password";
topic = "YourTopic/#";The same parameters entered in the broker must be established
Connection Function
####### FUNCTION ON CONNECT ######
def on_connect(client, userdata, flags, rc):
print('Connected(%s)',client._client_id)
client.subscribe(topic, qos=0)
client.publish(topic,'Connected')Message Function
####### FUNCTION ON MESSAGE ######
def on_message(client, userdata, message):
print('----------------------')
print('topic: %s', message.topic)
print('payload: %s', message.payload)
print('qos: %d', message.qos)
print(message.payload.decode("utf-8"))Publish
client.publish(topic, state)Disconnected function
client.disconnect()MQTT
JAZMIN RODRIGUEZ