Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
import threading
import random
import time
import paho.mqtt.client as mqtt
broker = "broker.hivemq.com"
port = 1883
energy_topic = "home/energy"
temperature_topic = "home/temperature"
Publisher function for energy
def publish_energy_data(client):
while True:
energy_usage = random.uniform(0.5, 2.0) # Simulate energy usage in kWh
message = f"{energy_usage:.2f}"
client.publish(energy_topic, message)
print(f"Published Energy: {message} kWh")
time.sleep(5) # Publish every 5 seconds
Publisher function for temperature
def publish_temperature_data(client):
while True:
temperature = random.uniform(18.0, 30.0) # Simulate temperature in Celsius
message = f"{temperature:.2f}"
client.publish(temperature_topic, message)
print(f"Published Temperature: {message} °C")
time.sleep(5) # Publish every 5 seconds
Subscriber callback
def on_message(client, userdata, message):
topic = message.topic
payload = message.payload.decode()
if topic == energy_topic:
energy_usage = float(payload)
print(f"Received Energy: {energy_usage} kWh")
if energy_usage > 1.5:
print("High energy usage detected! Turning off non-essential devices.")
else:
print("Energy usage is within the normal range.")
elif topic == temperature_topic:
temperature = float(payload)
print(f"Received Temperature: {temperature} °C")
# Add any specific handling for temperature data here
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT broker")
client.subscribe(energy_topic)
client.subscribe(temperature_topic)
else:
print(f"Failed to connect, return code {rc}")
Create and configure the subscriber client
subscriber_client = mqtt.Client()
subscriber_client.on_connect = on_connect
subscriber_client.on_message = on_message
Create and configure the publisher client
publisher_client = mqtt.Client()
Connect both clients to the broker
subscriber_client.connect(broker, port, 60)
publisher_client.connect(broker, port, 60)
Run the MQTT loop for the subscriber in a separate thread subscriber_thread = threading.Thread(target=subscriber_client.loop_forever) subscriber_thread.start()
Run the publisher functions in separate threads
energy_thread = threading.Thread(target=publish_energy_data, args=(publisher_client,)) temperature_thread = threading.Thread(target=publish_temperature_data, args=(publisher_client,))
energy_thread.start()
temperature_thread.start()
Wait for the threads to complete
energy_thread.join()
temperature_thread.join()