-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt_client_pub02.py
56 lines (45 loc) · 1.46 KB
/
mqtt_client_pub02.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
'''
simulator send temperature sensor random value to MQTT
temp value range 0-30
'''
import time
import paho.mqtt.client as mqtt
from datetime import datetime
from random import randint
if __name__=="__main__":
global MQTT_HOST,MQTT_PORT,MQTT_TOPIC,voiceDb
MQTT_HOST='172.16.155.154'
MQTT_PORT=1883
MQTT_TOPIC="sensor"
sensor_id = "02"
order_No = "0029"
message_interval = 10
message_count = 10
mqttc=mqtt.Client()
try:
mqttc.connect(MQTT_HOST,MQTT_PORT,60)
mqttc.loop_start()
flag = "START"
count = 0
while True:
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
temp_value = randint(0,40)
if flag == 'START':
mqttc.publish(MQTT_TOPIC, '{} {} {} {}'.format(dt_string, sensor_id, order_No, flag))
flag = "SENDING"
else:
mqttc.publish(MQTT_TOPIC,'{} {} {} {}'.format(dt_string,sensor_id,order_No,str(temp_value)))
count +=1
if count > message_count:
flag = "STOP"
mqttc.publish(MQTT_TOPIC,'{} {} {} {}'.format(dt_string,sensor_id,order_No,flag))
mqttc.loop_stop()
mqttc.disconnect()
break
time.sleep(message_interval)
except Exception as e:
print("Interrupted")
print(e)
mqttc.loop_stop()
mqttc.disconnect()