-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOT_LAB7_FINAL.py
More file actions
62 lines (50 loc) · 1.69 KB
/
IOT_LAB7_FINAL.py
File metadata and controls
62 lines (50 loc) · 1.69 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
import serial
import paho.mqtt.publish as publish
import time
import json
# Serial setup
ser = serial.Serial('COM2', 9600, timeout=1)
ser.flushInput()
# MQTT settings
mqtt_host = "broker.emqx.io"
mqtt_port = 1883
mqtt_topic = "channels/3201453/publish"
while True:
data = ser.readline()
if len(data) > 0:
print("Raw data:", data)
strdata = data.decode("utf-8").rstrip("\r\n")
parts = strdata.split(",")
if len(parts) != 9:
print("Invalid data length:", parts)
continue
# Replace Arduino timestamp with real system time
timestamp = int(time.time())
# ✅ FIX: use parts, not fields — and strip CRLF
fan_state = int(parts[8].strip())
# Build JSON payload with correct field names
payload = {
"Log_Timestamp": timestamp,
"Occupancy": int(parts[1]),
"Temperature_Data": float(parts[2]),
"Humidity_Data": float(parts[3]),
"Pressure_Data": float(parts[4]),
"Dust_Data": float(parts[5]),
"Light_Data": float(parts[6]),
"Distance_Data": float(parts[7]),
"Fan_State": fan_state # ✅ use cleaned value
}
try:
print("Publishing:", payload)
publish.single(
mqtt_topic,
json.dumps(payload),
hostname=mqtt_host,
port=mqtt_port,
transport="tcp"
)
except KeyboardInterrupt:
print("Stopped by user.")
break
except Exception as e:
print("Error while publishing:", e)