91 lines
2.1 KiB
Python
91 lines
2.1 KiB
Python
import serial
|
|
import json
|
|
import pika
|
|
import time
|
|
import struct,fcntl
|
|
|
|
|
|
ser = serial.Serial("/dev/ttyMAX0", baudrate=9600, stopbits=1, bytesize=8, parity="N", timeout=1,rtscts=True)
|
|
|
|
fd=ser.fileno()
|
|
serial_rs485=struct.pack('hhhhhhhh', 1, 1, 0, 0, 0, 0, 0, 0)
|
|
fcntl.ioctl(fd,0x542F,serial_rs485)
|
|
|
|
address = 0x01
|
|
function_code = 0x03
|
|
up_reg_address = 0x00
|
|
down_reg_addree = 0x00
|
|
multi_reg = 0x00
|
|
prime_reg = 0x02
|
|
down_crc_code = 0xC4
|
|
up_crc_code = 0x0B
|
|
|
|
def circle():
|
|
request = [address, function_code, up_reg_address, down_reg_addree, multi_reg, prime_reg, down_crc_code, up_crc_code]
|
|
packet = bytearray()
|
|
|
|
for data in request:
|
|
packet.append(data)
|
|
|
|
# print(packet)
|
|
response = ser.write(packet)
|
|
# print(response)
|
|
|
|
result = ser.read(9)
|
|
# print(result)
|
|
# print(hex(result[0]),hex(result[1]),hex(result[2]),hex(result[3]),hex(result[4]),hex(result[5]),hex(result[6]),hex(result[7]),hex(result[8]))
|
|
|
|
hum = (result[3]*256 +result[4])
|
|
temp = (result[5]*256 + result[6])
|
|
|
|
f = open ('/sys/devices/virtual/thermal/thermal_zone0/temp', 'r')
|
|
cputemp = f.readline()
|
|
|
|
f.close()
|
|
|
|
Data = {
|
|
"NodeQ":{
|
|
"Interface": "RS-485",
|
|
"CPU Temperature": int(cputemp)/1000,
|
|
},
|
|
"componentId": "CP-SDT-btftm01",
|
|
"timestamp": int(time.time()*1000),
|
|
"data": {
|
|
"Temperature": int(temp)/10,
|
|
"Humidity": int(hum)/10
|
|
}
|
|
}
|
|
|
|
dict_1 = json.dumps(Data, indent=2)
|
|
print(" [x] Sent 'Data!' Datas are", dict_1)
|
|
|
|
|
|
|
|
# connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.0.20'))
|
|
# channel = connection.channel()
|
|
|
|
# channel.basic_publish(exchange='lab',
|
|
# routing_key='node.q',
|
|
# body = dict_1)
|
|
|
|
|
|
|
|
# connection.close()
|
|
|
|
while True:
|
|
circle()
|
|
print("\nStop : cntl + c\n")
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
# Data = {
|
|
# "componentId": "CP-SDT-btftm01",
|
|
# "timestamp": int(time.time()*1000),
|
|
# "data": {
|
|
# "Temperature": int(temp)/10,
|
|
# "Humidity": int(hum)/10,
|
|
# "CPU Temperature": int(cputemp)/1000
|
|
# }
|
|
# }
|