-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirai_mqtt.py
107 lines (93 loc) · 3.01 KB
/
mirai_mqtt.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
import paho.mqtt.client as mqtt
import time
import pymodbus
import serial
from pymodbus.pdu import ModbusRequest
# initialize a serial RTU client instance
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer
import logging
logging.basicConfig(level="INFO")
log = logging.getLogger()
log.setLevel(logging.INFO)
MQTT_SERVER = "192.168.1.216"
MQTT_TOPIC_ROOT = "/MIRAI/"
# count= the number of registers to read
# unit= the slave unit this request is targeting
# address= the starting address to read from
try:
client_ModBus = ModbusClient(
method="rtu",
port="/dev/ttyUSB0",
stopbits=1,
bytesize=8,
parity='E',
baudrate=9600)
client_Mqtt = mqtt.Client("MIRAI-001")
print("Connecting to MQTT server... ", MQTT_SERVER)
if client_Mqtt.connect(MQTT_SERVER) == 0: # connect
while True:
# Connect to the serial modbus server
connection = client_ModBus.connect()
# print (connection)
if connection:
result1_start = 8970
result1_lenght = 62
result1 = client_ModBus.read_holding_registers(
result1_start, result1_lenght, unit=0x1)
result2_start = 9032
result2_lenght = 62
result2 = client_ModBus.read_holding_registers(
result2_start, result1_lenght, unit=0x1)
result3_start = 16438
result3_lenght = 62
result3 = client_ModBus.read_holding_registers(
result3_start, result1_lenght, unit=0x1)
result_lenghT = result1_lenght + result2_lenght + result3_lenght
#print("result_lenghT " + str(result_lenghT))
Matrix = {}
Indice = 0
if result1:
x = 0
result1_counter = result1_start
while (x < len(result1.registers)):
result1_counter = result1_counter + 1
topic = MQTT_TOPIC_ROOT + str(result1_counter)
Matrix[Indice, 1] = topic
Matrix[Indice, 2] = result1.registers[x]
Indice = Indice + 1
# print(topic + "=" + str(result1.registers[x]))
x = x + 1
if result2:
x = 0
result2_counter = result2_start
while (x < len(result2.registers)):
result2_counter = result2_counter + 1
topic = MQTT_TOPIC_ROOT + str(result2_counter)
Matrix[Indice, 1] = topic
Matrix[Indice, 2] = result2.registers[x]
Indice = Indice + 1
# print(topic + "=" + str(result2.registers[x]))
x = x + 1
if result3:
x = 0
result3_counter = result3_start
while (x < len(result3.registers)):
result3_counter = result3_counter + 1
topic = MQTT_TOPIC_ROOT + str(result3_counter)
Matrix[Indice, 1] = topic
Matrix[Indice, 2] = result3.registers[x]
Indice = Indice + 1
# print(topic + "=" + str(result3.registers[x]))
x = x + 1
while (x < len(Matrix) / 2):
logging.debug(Matrix[x,1]+"="+str(Matrix[x, 2]))
#print(Matrix[x,1]+"="+str(Matrix[x, 2]))
client_Mqtt.publish(Matrix[x, 1], Matrix[x, 2])
x = x + 1
time.sleep(5)
client_ModBus.close()
client_Mqtt.disconnect()
except KeyboardInterrupt:
print('\n\n Keyboard exception received. Exiting.')
exit()