3
3
4
4
# Create a virtual IoT device that simulates the ambient temperature and
5
5
# publishes it via MQTT. The simulation function is already predefined.
6
- # This exercise to give a simple introduction to the communication via MQTT.
6
+ # This exercise gives a simple introduction to the communication via MQTT.
7
7
8
8
# The input sections are marked with 'ToDo'
9
9
31
31
# import simulation model
32
32
from tutorials .ngsi_v2 .simulation_model import SimulationModel
33
33
34
-
35
34
# ## Parameters
36
- # ToDo: Enter your mqtt broker url and port, e.g mqtt://test.mosquitto.org:1883
37
- MQTT_BROKER_URL = "mqtt://test.mosquitto.org:1883"
38
- # ToDo: If required enter your username and password
35
+ # ToDo: Enter your mqtt broker url and port, e.g. mqtt://test.mosquitto.org:1883.
36
+ MQTT_BROKER_URL = "mqtt://test.mosquitto.org:1883"
37
+ # ToDo: If required, enter your username and password.
39
38
MQTT_USER = ""
40
- MQTT_PW = ""
39
+ MQTT_PW = ""
41
40
42
41
# ToDo: Create a unique topic that your weather station will publish on,
43
- # e.g. by using a uuid
42
+ # e.g. by using a uuid.
44
43
UNIQUE_ID = str (uuid4 ())
45
44
TOPIC_WEATHER_STATION = f"fiware_workshop/{ UNIQUE_ID } /weather_station"
46
45
50
49
51
50
T_SIM_START = 0 # simulation start time in seconds
52
51
T_SIM_END = 24 * 60 * 60 # simulation end time in seconds
53
- COM_STEP = 60 * 60 * 1 # 60 min communication step in seconds
54
-
52
+ COM_STEP = 60 * 60 # 60 min communication step in seconds
55
53
56
54
# ## Main script
57
55
if __name__ == '__main__' :
61
59
temp_max = TEMPERATURE_MAX ,
62
60
temp_min = TEMPERATURE_MIN )
63
61
64
- # define lists to store historical data
62
+ # define a list for storing historical data
65
63
history_weather_station = []
66
64
67
- # ToDo: create a MQTTv5 client with paho-mqtt
65
+ # ToDo: Create an MQTTv5 client with paho-mqtt.
68
66
mqttc = mqtt .Client (protocol = mqtt .MQTTv5 )
69
67
# set user data if required
70
68
mqttc .username_pw_set (username = MQTT_USER , password = MQTT_PW )
69
+
71
70
# ToDo: Define a callback function that will be executed when the client
72
71
# receives message on a subscribed topic. It should decode your message
73
- # and store the information for later in our history
74
- # Note: do not change function's signature!
72
+ # and store the information for later in our history.
73
+ # Note: Do not change the function's signature!
75
74
def on_message (client , userdata , msg ):
76
75
"""
77
76
Callback function for incoming messages
78
77
"""
79
78
# decode the payload
80
79
payload = msg .payload .decode ('utf-8' )
81
80
# ToDo: Parse the payload using the `json` package and write it to
82
- # the history
81
+ # the history.
83
82
history_weather_station .append (json .loads (payload ))
84
83
85
84
86
-
87
85
# add your callback function to the client. You can either use a global
88
86
# or a topic specific callback with `mqttc.message_callback_add()`
89
87
mqttc .on_message = on_message
90
88
91
- # ToDO: connect to the mqtt broker and subscribe to your topic
89
+ # ToDo: Connect to the mqtt broker and subscribe to your topic.
92
90
mqtt_url = urlparse (MQTT_BROKER_URL )
93
91
mqttc .connect (host = mqtt_url .hostname ,
94
92
port = mqtt_url .port ,
@@ -98,27 +96,27 @@ def on_message(client, userdata, msg):
98
96
clean_start = mqtt .MQTT_CLEAN_START_FIRST_ONLY ,
99
97
properties = None )
100
98
101
- # ToDo: print and subscribe to the weather station topic
99
+ # ToDo: Print and subscribe to the weather station topic.
102
100
print (f"WeatherStation topic:\n { TOPIC_WEATHER_STATION } " )
103
101
mqttc .subscribe (topic = TOPIC_WEATHER_STATION )
104
102
105
103
# create a non-blocking thread for mqtt communication
106
104
mqttc .loop_start ()
107
105
108
- # ToDo: Create a loop that publishes every second a message to the broker
106
+ # ToDo: Create a loop that publishes every 0.2 seconds a message to the broker
109
107
# that holds the simulation time "t_sim" and the corresponding temperature
110
- # "t_amb"
108
+ # "t_amb".
111
109
for t_sim in range (sim_model .t_start ,
112
110
int (sim_model .t_end + COM_STEP ),
113
111
int (COM_STEP )):
114
- # ToDo: publish the simulated ambient temperature
112
+ # ToDo: Publish the simulated ambient temperature.
115
113
mqttc .publish (topic = TOPIC_WEATHER_STATION ,
116
114
payload = json .dumps ({"t_amb" : sim_model .t_amb ,
117
115
"t_sim" : sim_model .t_sim }))
118
116
119
117
# simulation step for next loop
120
118
sim_model .do_step (int (t_sim + COM_STEP ))
121
- time .sleep (1 )
119
+ time .sleep (0.2 )
122
120
123
121
# close the mqtt listening thread
124
122
mqttc .loop_stop ()
@@ -127,9 +125,9 @@ def on_message(client, userdata, msg):
127
125
128
126
# plot results
129
127
fig , ax = plt .subplots ()
130
- t_simulation = [item ["t_sim" ] for item in history_weather_station ]
128
+ t_simulation = [item ["t_sim" ]/ 3600 for item in history_weather_station ]
131
129
temperature = [item ["t_amb" ] for item in history_weather_station ]
132
130
ax .plot (t_simulation , temperature )
133
- ax .set_xlabel ('time in s ' )
131
+ ax .set_xlabel ('time in h ' )
134
132
ax .set_ylabel ('ambient temperature in °C' )
135
133
plt .show ()
0 commit comments