Message boards : Science : A small program for the Raspberry to read the current value and transfer it over Ethernet.
Author | Message |
---|---|
I have written a small program to read the current value from the sensor, it is written in Python 3 and runs on each Raspberry and runs parallel to Boinc. The value is read out via RS232, since I am with USB programming not yet know well ;). I use this to represent the value in my house automation "IP_Symcon". Improvements are always welcome. If you have any suggestions, please email me at "win-ex@web.de" import serial, threading, socket class WorkSerial(threading.Thread): receive_Buffer = "Emty" def __init__(self, iD, speed): threading.Thread.__init__(self) self.iD = iD self.speed = speed def run(self): print("Starting Serial Thread ", self.iD) serial_port = serial.Serial("/dev/ttyAMA0", self.speed) while 1: try: WorkSerial.receive_Buffer = serial_port.readline() except: print("Serial Error... Retry...") class WorkTcp(threading.Thread): receive_Buffer = "Emty" send_Buffer = "" def __init__(self, iD, port=10001, ip=("192.168.1.4"), BufferSize=1024): threading.Thread.__init__(self) self.iD = iD self.port = port self.ip = ip self.BufferSize = BufferSize def run(self): print("Starting TCP Thread ", self.iD) while 1: host_name = socket.getfqdn() host_ip = self.ip print("Hostname: " + str(host_name) + " IP: " + str(host_ip) + " Port: " + str(self.port) + " Buffer: " + str(WorkSerial.receive_Buffer)) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host_ip, self.port)) s.listen(1) conn, addr = s.accept() print('Connection address:', addr) while 1: try: data = conn.recv(self.BufferSize) if not data: break else: if data == b'Send_Value': xStreamOn = True conn.send(bytes(str(WorkSerial.receive_Buffer) + "\r\n", 'utf-8')) if data == b'Send_Status': conn.send(bytes("Penis" + "\r\n", 'utf-8')) except: print("TCP Error... Retry...") conn.close() print ("Starting Crazy´s Serial to TCP Gateway V0.1") # t1 = WorkSerial(Thread ID ,Baudrate) t1 = WorkSerial(1,9600) t1.start() # t1 = WorkTcp(Thread ID ,Port,IP-Adress,Buffer-Size) t2 = WorkTcp(2,10001,"192.168.1.4",1024) t2.start() ____________ | |
ID: 3489 | Rating: 0 | rate: / Reply Quote | |
Message boards :
Science :
A small program for the Raspberry to read the current value and transfer it over Ethernet.