-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublisher1.py
More file actions
50 lines (37 loc) · 1.21 KB
/
publisher1.py
File metadata and controls
50 lines (37 loc) · 1.21 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
import paho.mqtt.client as mqtt
import json
import time
from sense_hat import SenseHat
from datetime import datetime
sense = SenseHat()
# MQTT broker details
broker_address = "localhost" # Use the address of your local MQTT broker
port = 1883 # Default MQTT port
# Create an MQTT client instance
client = mqtt.Client("csv_publisher")
# Connect to the MQTT broker
client.connect(broker_address, port)
# Define the topic to which you want to publish
topic = "test_data"
start_time = time.time()
# Define the duration of the loop in seconds (1 hour = 3600 seconds)
duration = 200
# Run the while loop for the specified duration
while (time.time() - start_time) < duration:
# Your code here
# Create a dictionary for each row
data = {
"temp": str(sense.get_temperature()),
"DO": str(sense.get_temperature()),
"Date": str(datetime.now())
}
# Convert dictionary to JSON string
message = json.dumps(data)
# Publish the JSON data to the MQTT topic
client.publish(topic, message)
print(f"Published: {message} to topic: {topic}")
# Sleep for a short duration if needed
time.sleep(1)
# Disconnect from the MQTT broker
else:
client.disconnect()