Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create Lovepreet Individual contribution
  • Loading branch information
lovepre21 committed Aug 1, 2024
1 parent c637172 commit 49928c1
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Lovepreet Individual contribution
@@ -0,0 +1,37 @@
# 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



# 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 ( manraj preet and lovepreet (me) ) worked together on this topic





# 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()

0 comments on commit 49928c1

Please sign in to comment.