/
mb_demo3_mq.py
119 lines (97 loc) · 3.31 KB
/
mb_demo3_mq.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt #import the client1
import time
import serial
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus_rtu as modbus_rtu
import time
from struct import *
import random
mbComPort = '/dev/ttyUSB0'#'3COM7' # COM3->RS-485
baudrate = 9600
databit = 8
parity = 'N'
stopbit = 1
mbTimeout = 100 # ms
def on_connect(client, userdata, flags, rc):
m="Connected flags"+str(flags)+", result code "+str(rc)+", client_id "+str(client)
print(m)
# first value OFF
print('set light off!')
control_light(0)
client1.publish(topic,0)
def on_message(client1, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")), message.topic, message.qos, message.retain)
if message.topic == topic:
my_message = str(message.payload.decode("utf-8"))
print("set light: ", my_message)
if my_message=='1' or my_message==1:
control_light(1)
else:
control_light(0)
def on_log(client, userdata, level, buf):
print("log: ",buf)
def control_light(value):
mb_port = serial.Serial(port=mbComPort, baudrate=baudrate, bytesize=databit, parity=parity, stopbits=stopbit)
master = modbus_rtu.RtuMaster(mb_port)
master.set_timeout(mbTimeout/1000.0)
mbId = 1
addr = 2 #base0
try:
#-- FC5: write multi-coils
rr = master.execute(mbId, cst.WRITE_SINGLE_COIL, addr, output_value=value)
print("Write(addr, value)=%s" %(str(rr)))
except Exception as e:#Exception, e:
print("modbus test Error: " + str(e))
master._do_close()
def read_power_meter():
mb_port = serial.Serial(port=mbComPort, baudrate=baudrate, bytesize=databit, parity=parity, stopbits=stopbit)
master = modbus_rtu.RtuMaster(mb_port)
master.set_timeout(mbTimeout/1000.0)
mbId = 4
#[0x1000-0x1001]=VIn_a
addr = 0x1000#4096
v_a = None
try:
# FC3
rr = master.execute(mbId, cst.READ_INPUT_REGISTERS, addr, 4)
print('read value:', rr)
# convert to float:
# IEEE754 ==> (Hi word Hi Bite, Hi word Lo Byte, Lo word Hi Byte, Lo word Lo Byte)
try:
v_a_hi = rr[1]
v_a_lo = rr[0]
v_a = unpack('>f', pack('>HH', v_a_hi, v_a_lo))
print('v_a=', v_a)
except Exception as e:#Exception, e:
print(e)
except Exception as e:#Exception, e:
print("modbus test Error: " + str(e))
master._do_close()
return v_a[0]
# some online free broker:
# iot.eclipse.org
# test.mosquitto.org
# broker.hivemq.com
broker_address="iot.eclipse.org"
topic = "malo-iot/light"
client1 = mqtt.Client() #create new instance
client1.on_connect = on_connect #attach function to callback
client1.on_message = on_message #attach function to callback
#client1.on_log=on_log
time.sleep(1)
client1.connect("iot.eclipse.org", 1883, 60) #connect to broker
client1.subscribe(topic)
#client1.loop_forever()
# 有自己的while loop,所以call loop_start(),不用loop_forever
client1.loop_start() #start the loop
time.sleep(2)
print("loop start")
while True:
v = read_power_meter()
print('V=%s, type(V)=%s' %(v, type(v)))
#v = random.randint(100, 120)
client1.publish("malo-iot/voltage", v)
time.sleep(2)