| 12345678910111213141516171819202122232425262728293031323334353637 |
- import paho.mqtt.client as mqtt
- import time
- import random # Replace with actual voltage measurement code
- """
- This script connects to local MQTT broker and publishes random voltage measurements
- """
- BROKER = "localhost"
- PORT = 1883 # Use 8883 for TLS
- TOPIC = "cells_inserted/device1" # Change for each device
- USERNAME = "robot"
- PASSWORD = "robot"
- def on_connect(client, userdata, flags, rc):
- if rc == 0:
- print("Connected to MQTT Broker!")
- else:
- print(f"Failed to connect, return code {rc}\n")
- client = mqtt.Client()
- client.username_pw_set(USERNAME, PASSWORD)
- client.on_connect = on_connect
- client.connect(BROKER, PORT, 60)
- client.loop_start()
- try:
- while True:
- slot = int(random.uniform(0, 5)) # Replace with actual measurement
- client.publish(TOPIC, slot)
- print(f"Published: {slot} to topic {TOPIC}")
- time.sleep(2) # Adjust as needed
- except KeyboardInterrupt:
- print("Disconnecting from broker")
- client.disconnect()
- client.loop_stop()
|