-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.py
75 lines (59 loc) · 2.27 KB
/
publisher.py
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
import os
from azure.iot.device import Message, IoTHubDeviceClient
import random
import time
import json
def generate_weather():
"""
it generates random values for temperature, humidity, wind direction, wind intensity and rain
"""
t = random.uniform(-50.0,50.01)
temp = round(t,2)
hum = random.randint(0,100)
wind_dir = random.randint(0,360)
wind_int = random.randint(0,100)
rain = random.randint(0,50)
return temp, hum, wind_dir, wind_int, rain
def choose_device():
"""
it randomly chooses between two different virtual stations: foggia_1 and rome_1.
Fetch the connection string from an enviornment variable
"""
prob = random.random()
if (prob < 0.5):
connection_string = os.getenv("IOTHUB_DEVICE1_CONNECTION_STRING")
else:
connection_string = os.getenv("IOTHUB_DEVICE2_CONNECTION_STRING")
return connection_string
def publish_message(connection_string: str):
"""
:connection_string is the connection string of the device to the IoT hub.
It publishes messages to the IoT hub every 4 seconds.
"""
# Create instance of the device client using the authentication provider
device_client = IoTHubDeviceClient.create_from_connection_string(connection_string,websockets=True)
# Connect the device client
device_client.connect()
while(True):
try:
temp,hum,wind_dir,wind_int,rain = generate_weather()
#Generating the message to send
msg = {'temperature': temp, 'humidity': hum,'wind_direction': wind_dir,'wind_intensity':wind_int,'rain':rain}
message = json.dumps(msg)
# Send the message
print("Sending message...")
device_client.send_message(message)
print("Message successfully sent!")
print(message)
time.sleep(4)
except KeyboardInterrupt:
print("IoTHubClient stopped")
return
except:
print("Unexpected error")
time.sleep(4)
# finally, disconnect
device_client.disconnect()
if __name__ == "__main__":
connection_string = choose_device()
publish_message(connection_string)