Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this user
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
kaurm105
/
MQTT
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
1
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Projects
Security
Insights
Files
main
Lovepreet Individual contribution
Lovepreet kaur (coding)
README.md
Screenshot 2024-07-29 103309.png
Screenshot 2024-07-30 144825.png
Screenshot 2024-07-30 222035.png
Screenshot 2024-07-30 232934.png
Screenshot 2024-07-30 233608.png
Screenshot 2024-07-31 094030.png
Screenshot 2024-07-31 105451.png
Screenshot 2024-07-31 111609.png
Screenshot 2024-07-31 203035.png
Breadcrumbs
MQTT
/
Lovepreet kaur (coding)
Blame
Blame
Latest commit
History
History
78 lines (66 loc) · 2.63 KB
Breadcrumbs
MQTT
/
Lovepreet kaur (coding)
Top
File metadata and controls
Code
Blame
78 lines (66 loc) · 2.63 KB
Raw
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()
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
You can’t perform that action at this time.