Skip to content

Commit 6dfc2f6

Browse files
committed
Adding implementation
1 parent 3abd0d1 commit 6dfc2f6

File tree

7 files changed

+156
-1
lines changed

7 files changed

+156
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# mqtt-docker-python
2-
Testing MQTT using Docker and the Python client
2+
3+
Testing MQTT using Docker and the Paho Python client

config/mosquitto.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
allow_anonymous true
2+
listener 1883
3+
protocol mqtt
4+
listener 9001
5+
protocol websockets
6+
socket_domain ipv4
7+
persistence true
8+
# password_file /mosquitto/config/pwfile
9+
persistence_file mosquitto.db
10+
persistence_location /mosquitto/data/
11+
log_type all

docker-compose.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
mqtt:
3+
image: eclipse-mosquitto:2.0-openssl
4+
ports:
5+
- 1883:1883
6+
- 9001:9001
7+
volumes:
8+
- ./config:/mosquitto/config:rw
9+
- mqtt-data:/mosquitto/data
10+
11+
mqtt_client:
12+
image: emqx/mqttx-web:v1.12.0
13+
ports:
14+
- 9000:80
15+
16+
networks:
17+
default:
18+
name: mqtt_docker
19+
driver: bridge
20+
21+
volumes:
22+
mqtt-data: {}

poetry.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pub_sub_mqtt.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import paho.mqtt.client as mqtt
2+
import time
3+
import random
4+
import json
5+
6+
def on_message(client, userdata, msg):
7+
payload = msg.payload.decode()
8+
print(f"Received message: {msg.topic} -> {payload}")
9+
10+
payload_dict = json.loads(payload)
11+
12+
if payload_dict["value"] == "exit":
13+
print("Exiting")
14+
client.exit_flag = True
15+
16+
topic = "paho/temperature"
17+
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
18+
mqttc.on_message = on_message
19+
mqttc.exit_flag = False
20+
21+
mqttc.connect("localhost", 1883, 60)
22+
mqttc.subscribe(topic)
23+
24+
mqttc.loop_start()
25+
26+
while not mqttc.exit_flag:
27+
sensor_reading = random.uniform(5, 15)
28+
print(sensor_reading)
29+
30+
json_message = json.dumps({"value": sensor_reading })
31+
mqttc.publish(topic, json_message)
32+
33+
time.sleep(5)
34+
35+
mqttc.loop_stop()
36+
mqttc.disconnect()

pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.poetry]
2+
name = "mqtt-docker-python"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["James Bristow <james.bristow@plantandfood.co.nz>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.10"
10+
paho-mqtt = "^2.1.0"
11+
12+
13+
[build-system]
14+
requires = ["poetry-core"]
15+
build-backend = "poetry.core.masonry.api"

state_machine_mqtt.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import paho.mqtt.client as mqtt
2+
import time
3+
import json
4+
from enum import Enum, auto
5+
6+
class State(Enum):
7+
IDLE = auto()
8+
RUNNING = auto()
9+
PAUSED = auto()
10+
STOPPED = auto()
11+
ERROR = auto()
12+
MAINTENANCE = auto()
13+
14+
class Vehicle:
15+
def __init__(self):
16+
self.state = State.IDLE
17+
18+
car = Vehicle()
19+
20+
def on_message(client, userdata, msg):
21+
payload = msg.payload.decode()
22+
print(f"Received message: {msg.topic} -> {payload}")
23+
24+
payload_dict = json.loads(payload)
25+
event = payload_dict.get("value")
26+
27+
if event is None:
28+
print("Value not provided in message")
29+
30+
try:
31+
vehicle_state = State[event]
32+
car.state = vehicle_state
33+
print(car.state)
34+
except KeyError:
35+
print(f"Invalid state: {event}")
36+
37+
topic = "paho/temperature"
38+
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
39+
mqttc.on_message = on_message
40+
mqttc.exit_flag = False
41+
42+
mqttc.connect("localhost", 1883, 60)
43+
mqttc.subscribe(topic)
44+
mqttc.loop_start()
45+
46+
while car.state != State.STOPPED:
47+
time.sleep(5)
48+
49+
mqttc.loop_stop()
50+
mqttc.disconnect()

0 commit comments

Comments
 (0)