-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt_client_pub01.py
61 lines (50 loc) · 1.54 KB
/
mqtt_client_pub01.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
'''
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='10.75.58.26'
MQTT_PORT=1883
MQTT_TOPIC="sensor"
sensor_id = "01"
order_No = "0018"
message_interval = 10
message_count = 10
mqttc=mqtt.Client()
try:
mqttc.connect(MQTT_HOST,MQTT_PORT,60)
mqttc.loop_start()
flag = "START"
'''
flag START
SENDING
STOP
'''
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()