Calling all Python experts!
I am working with some sensors that connect to hub. The sensors send their data to the hub in intervals and it looks like this:
#[device address],[distance],[time],[reading counter]
#1,3200.000,18/06/02 8:00:05,271
#2,3200.000,18/06/02 8:05:05,271
#3,3200.000,18/06/02 8:10:05,271
Executing this python code returns the expected value above in stdout.
import serial
ser =serial.Serial ('/dev/ttyUSB0', 9600)
response = ser.read(35)
print response
I would like to come up with a better way to handle these readings. For example if the response string starts with #1 then write the [distance] portion of that string to a file named sensor 1.txt
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600) #listen on USB0 @ 9600 buad
response = ser.read(35) # read 35 characters of response from device connected to USB0
# The following liines need to send readings to specific files based on the incoming sensor data...
if response.startswith('#1')
file = open("sensor_1.txt", "w")
file.write(response) # I would also like to just print the distance reading so string.str #1, ,18/06/02 8:01:05,271,184
file.close # the empty spaces would be the distance reading that is written to the file.
elif response.startswith('#2')
file = open("sensor_2.txt","w")
file.write(response)
file.close
elif response.startswith('#3')
file = open("sensor_3.txt","w")
file.write(response)
file.close
else
file = open("sensor_error","w")
file.write('Check Sensor Connection')
file.close
endif:
My thought is that if I get the correct chunk of data to the correct file then I can use file sensors for each device. I would also like it if this would continue to run or loop. As of right now it exits once the response from the sensor is received.
If you couldn’t tell I have no coding experience but have been using HA since ~ 0.12. I have a good grasp on templates and other components.
If anyone has a better ideas I am all ears!
Regards,