Teaching myself Python Day 12

Words
53
Reading
1 min
Listen
Play
7y

pythonpowered.png

Today's learning consisted of learning how to parse the data stream from the USB attached wireless node collecting remote temperature data. The next step will be to store it to my MySQL temperature database.

Here is the code from today's learning:

import serial

# Open USB serial port attached to wireless receiver
serport = serial.Serial('/dev/ttyUSB0',115200)

while True:
    # Read a line of data from wireless module
    serdata = serport.readline()
    # convert bytetype data to string data
    sensordata = serdata.decode('UTF-8')

    # if sensor data string starts with '#' symbol parse line of data
    if sensordata[0] == '#':
        # find start of sensor id
        sidpos = sensordata.find('][') + 2
        # find end of sensor id
        sidend = sensordata.find(']', sidpos)
        # find end of temp data
        stpos = sensordata.find('[', sidend)
        # get sensor id from string
        senid = sensordata[sidpos: sidend]
        if senid == '2':
            senidname = 'Master'
        elif senid == '10':
            senidname = 'diningrm'
        else:
            senidname = 'None'
        # get sensor temperature as float
        sentemp = float(sensordata[sidend + 1: stpos])


        print(sensordata)
        print(senidname +  ' ' + str(sentemp))

And here is a screen capture of the output from the program:

pylearn-day12.png

Teaching myself Python Day 12 | Ecency