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



0
0
0.000
3 comments
avatar

I think its awesome that you are learning python. I am putting together a workshop for the new year where I'm planning to teach people how to build there own energy management system.

I'm pretty sure I'm going to have to hack my way through building a python scrip to convert ASCII data into MODBUS to make the whole thing work.

That should be Day 50 or so for you, so I may ask you for a couple tips :)

0
0
0.000
avatar

Thanks.

Checkout this really old post where I hacked together a Raspberry Pi communicating with a Solar charge controller with MODBUS. Mostly cut and paste from another online article I reference in post.

0
0
0.000