Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 8fbfdf6

Browse files
Merge pull request #354 from jonascarvalh/mqtt-client
add: mqtt client project.
2 parents de7c74d + 69220a7 commit 8fbfdf6

7 files changed

Lines changed: 102 additions & 0 deletions

File tree

projects/MQTT Client/.env-example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Broker IP of MQTT (required)
2+
BROKER=
3+
4+
# Default port for MQTT (required)
5+
PORT=1883
6+
7+
# Topic to interact (required)
8+
TOPIC=
9+
10+
# User and Pass for login
11+
USER=
12+
PASS=

projects/MQTT Client/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/__pycache__
2+
/.pytest_cache
3+
/venv
4+
/build
5+
.env

projects/MQTT Client/MqttClient.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from paho.mqtt import client as mqtt_client
2+
from random import randint
3+
from decouple import config
4+
5+
# Reading broker credentials
6+
BROKER = config("BROKER")
7+
PORT = int(config("PORT"))
8+
TOPIC = config("TOPIC")
9+
USER = config("USER")
10+
PASS = config("PASS")
11+
12+
# Create a randdom id for login in MQTT
13+
CLIENT_ID = f'id-mqtt-{randint(0,100)}'
14+
15+
# Create a client to interact with MQTT
16+
def connectMqtt() -> mqtt_client:
17+
def on_connect(client, userdata, flags, rc):
18+
if rc == 0:
19+
print('[MQTT] Connected to server.')
20+
else:
21+
print('[MQTT] Failure to connect.')
22+
23+
client = mqtt_client.Client(CLIENT_ID)
24+
client.username_pw_set(USER, PASS)
25+
client.on_connect = on_connect
26+
client.connect(BROKER,PORT)
27+
28+
return client
29+
30+
# Receiving messages sendings with MQTT
31+
def subscribe(client: mqtt_client):
32+
def on_message(client, userdata, msg):
33+
print(
34+
f'[MQTT] Received message from {msg.topic} topic:',
35+
f'\n{msg.payload.decode()}'
36+
)
37+
38+
client.subscribe(TOPIC)
39+
client.on_message = on_message
40+
41+
# Publishing messages with MQTT
42+
def publish(client, msg='Hello MQTT'):
43+
result = client.publish(TOPIC,msg)
44+
45+
if __name__ == '__main__':
46+
client = connectMqtt()
47+
subscribe(client)
48+
publish(client)
49+
publish(client, msg='Custom Messages Working!')
50+
client.loop_forever()

projects/MQTT Client/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# MQTT Client
2+
> This code connects to an MQTT broker to publish and subscribe to topics to receive messages.
3+
4+
## MQTT Operation
5+
![MQTT Operation](docs/imgs/mqtt_operation.png)
6+
7+
## Requirements
8+
```sh
9+
pip install -r requirements.txt
10+
```
11+
12+
This file includes:
13+
```
14+
paho-mqtt==1.6.1
15+
python-decouple==3.8
16+
```
17+
18+
## How to execute?
19+
1. You need a Broker MQTT running. I recommend [Mosquitto](https://mosquitto.org/download/).
20+
Ex (Do not need to do):
21+
- `mosquitto -v`: Running Broker;
22+
- `mosquitto mosquitto_sub -h BROKER_IP -p PORT -t TOPIC`: Subscribe in topic.
23+
- ` mosquitto_pub -h BROKER_IP -p PORT -t TOPIC -m MESSAGE`: Publish "MESSAGE" in topic.
24+
2. Duplicate the `.env-example` file and configure it;
25+
3. Edit name `.env-example` to `.env`;
26+
4. Execute `MqttClient.py`.
27+
28+
```sh
29+
python3 MqttClient.py
30+
```
31+
32+
## Code Running
33+
![Running Code](docs/imgs/running_code.png)
61.9 KB
Loading
88.1 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
paho-mqtt==1.6.1
2+
python-decouple==3.8

0 commit comments

Comments
 (0)