-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublisher_cbor_data.py
More file actions
98 lines (80 loc) · 2.71 KB
/
Copy pathpublisher_cbor_data.py
File metadata and controls
98 lines (80 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import ssl, os, cbor2
from paho import mqtt
import paho.mqtt.client as paho
import time, random
from dotenv import load_dotenv
def on_connect(client, userdata, flags, reason_code, properties):
if reason_code == 0:
print(f"Connected to MQTT Broker at {HOST_NAME}!")
# Add connection status to userdata
userdata['connected'] = True
else:
print(f"Failed to connect, reason code: {reason_code}")
userdata['connected'] = False
def on_publish(client, userdata, mid, reason_code, properties):
print(f"Message ID: {mid}, Published from {userdata['location']}, Status: {reason_code}")
def on_disconnect(client, userdata, reason_code, properties, packet):
print(f"Disconnected with reason code: {reason_code}")
userdata['connected'] = False
# Simulate temperature sensor with more realistic values
def read_temperature():
base_temp = 24.0 # Room temperature baseline
variation = random.uniform(-2.0, 2.0)
return round(base_temp + variation, 1)
def create_sensor_payload():
return {
"temperature": read_temperature(),
"humidity": random.randint(45, 75),
"device_id": "sensor_123",
"timestamp": int(time.time())
}
# Load environment variables
load_dotenv()
HOST_NAME = os.getenv("HOST_NAME")
PORT = int(os.getenv("PORT", 8883)) # Default to 8883 for TLS
USER_NAME = os.getenv("USER_NAME")
PASSWORD = os.getenv("PASSWORD")
# Initialize userdata with more information
sensor_data = {
"location": "room1",
"connected": False
}
# Client setup with clean session
client = paho.Client(
protocol=paho.MQTTv5,
callback_api_version=paho.CallbackAPIVersion.VERSION2,
client_id=f"sensor_{random.randint(0, 1000)}", # Random client ID
userdata=sensor_data
)
# Set callbacks
client.on_publish = on_publish
client.on_connect = on_connect
client.on_disconnect = on_disconnect
# Set up TLS
client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)
client.username_pw_set(USER_NAME, PASSWORD)
try:
client.connect(HOST_NAME, PORT)
# loop_start triggers the on_message, on_publish and on_connect callbacks
client.loop_start()
# Wait for connection
time.sleep(1)
if sensor_data['connected']:
# Create and publish payload
payload = create_sensor_payload()
cbor_data = cbor2.dumps(payload)
result = client.publish(
'measure/all',
cbor_data,
qos=1,
retain=True
)
print(f"Published: {payload}, Result: {result}")
else:
print("Not connected to broker, cannot publish")
except Exception as e:
print(f"Error occurred: {e}")
finally:
# Clean disconnect
client.loop_stop()
client.disconnect()