- import time
- import json
- import random
- import paho.mqtt.client as mqtt
- # MQTT broker (change this to your broker's IP address or hostname)
- broker_address = "localhost"
- broker_port = 1883
- # MQTT topic to publish to
- topic = "sru/nan"
- # Create an MQTT client instance
- client = mqtt.Client("JetsonNano")
- # Connect to the broker
- client.connect(broker_address, broker_port)
- # Function to simulate sensor data
- def get_sensor_data():
- # Replace this with actual sensor data acquisition logic
- temperature = random.uniform(20.0, 30.0)
- humidity = random.uniform(40.0, 60.0)
- sensor_data = {
- "temperature": temperature,
- "humidity": humidity
- }
- return sensor_data
- try:
- while True:
- # Simulate sensor data
- data = get_sensor_data()
- # Convert data to JSON format
- payload = json.dumps(data)
- # Publish data to MQTT broker
- client.publish(topic, payload)
- print(f"Published: {payload}")
- # Wait for some time before publishing the next data
- time.sleep(5) # Change this value as per your requirement
- except KeyboardInterrupt:
- print("Interrupted, exiting...")
- client.disconnect()
[text] PUB
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: