My Technology Trail avatar

Teaching myself Python Day 12

mytechtrail

Published: 21 Dec 2019 › Updated: 21 Dec 2019Teaching myself Python Day 12

Teaching myself Python Day 12

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

Leave Teaching myself Python Day 12 to:

Written by

Projects I am working on, Raspberry Pi, Python and Ham Radio.

Read more #ulog posts


Best Posts From My Technology Trail

We have not curated any of mytechtrail's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From My Technology Trail