Skip to content

In this project the MQTT connection with the HiveMQ server will be visualized

License

Notifications You must be signed in to change notification settings

FreakJazz/Connection-MQTT-with-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Connection-MQTT-with-Python

In this project the MQTT connection with the HiveMQ server will be visualized

Topic Configuration Topic Configuration

Description

This program works with MQTT conection of IOT in order to comunicate with three topics

Contents

Up

Library Installation

In order to create a MQTT connection with Python We have to install pip install paho-mqtt Paho Library

Installation

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 

Up

Broker

After that, We need to open a Broker HIVEMQ Broker Address

The following parameters must be considered to establish the connection Topic Configuration

  • 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

Up

Programation

Python

Configure

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

# 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()

MQTT

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()

Up

Result

Send data from Python to Broker

Topic Configuration

Topic

MQTT

Topic Configuration

Up

Contributing

JAZMIN RODRIGUEZ

GitHub, LinkedIn

Up

About

In this project the MQTT connection with the HiveMQ server will be visualized

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages