Q_9
Posts: 2
Joined: Mon Sep 02, 2019 5:02 am

PMS5003 Sensor Coding - second opinion please

Mon Sep 02, 2019 5:16 am

I've looked around for some code for the PMS5003 particulate sensor, and I've had to mash about 3 or 4 different pieces of code together to get something that looks like it works. I'm not convinced I'm getting the right results as yet.

All I am doing at the moment is receiving some data, manipulating it a bit into various fields (based on other peoples code samples) and writing it out to a file. I'm not finding as much python/rasperrypi samples as Arduino so just having a bit of trouble verifying everything is going according to plan.

My code looks like:

Code: Select all

import serial
import time
import datetime

port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=3.0)
logfile = open("PMS5003 Sensor log.txt", "a")
try:
    while True:
        
        rcv = str(port.read(32))
        print(datetime.datetime.now()) 
        logfile.write("Time and Date: " + str(datetime.datetime.now()) + "\n")
            
        # PM1, PM2.5 and PM10 values for standard particle in ug/m^3
        pm1_hb_std = int(rcv[4].encode('hex'), 16)
        pm1_lb_std = int(rcv[5].encode('hex'), 16)
        pm1_std = float(pm1_hb_std * 256 + pm1_lb_std)
        pm25_hb_std = int(rcv[6].encode('hex'), 16)
        pm25_lb_std = int(rcv[7].encode('hex'), 16)
        pm25_std = float(pm25_hb_std * 256 + pm25_lb_std)                
        pm10_hb_std = int(rcv[8].encode('hex'), 16)
        pm10_lb_std = int(rcv[9].encode('hex'), 16)
        pm10_std = float(pm10_hb_std * 256 + pm10_lb_std)
                    
        #PM1, PM2.5 and PM10 values for atmospheric conditions in ug/m^3
        pm1_hb_atm = int(rcv[10].encode('hex'), 16)
        pm1_lb_atm = int(rcv[11].encode('hex'), 16)
        pm1_atm = float(pm1_hb_atm * 256 + pm1_lb_atm)
        pm25_hb_atm = int(rcv[12].encode('hex'), 16)
        pm25_lb_atm = int(rcv[13].encode('hex'), 16)
        pm25_atm = float(pm25_hb_atm * 256 + pm25_lb_atm)
        pm10_hb_atm = int(rcv[14].encode('hex'), 16)
        pm10_lb_atm = int(rcv[15].encode('hex'), 16)
        pm10_atm = float(pm10_hb_atm * 256 + pm10_lb_atm)
    
        #Number of particles bigger than 0.3 um, 0.5 um, etc. in #/cm^3
        part_03_hb = int(rcv[16].encode('hex'), 16)
        part_03_lb = int(rcv[17].encode('hex'), 16)
        part_03 = int(part_03_hb * 256 + part_03_lb)
        part_05_hb = int(rcv[18].encode('hex'), 16)
        part_05_lb = int(rcv[19].encode('hex'), 16)
        part_05 = int(part_05_hb * 256 + part_05_lb)
        part_1_hb = int(rcv[20].encode('hex'), 16)
        part_1_lb = int(rcv[21].encode('hex'), 16)
        part_1 = int(part_1_hb * 256 + part_1_lb)
        part_25_hb = int(rcv[22].encode('hex'), 16)
        part_25_lb = int(rcv[23].encode('hex'), 16)
        part_25 = int(part_25_hb * 256 + part_25_lb)
        part_5_hb = int(rcv[24].encode('hex'), 16)
        part_5_lb = int(rcv[25].encode('hex'), 16)
        part_5 = int(part_5_hb * 256 + part_5_lb)
        part_10_hb = int(rcv[26].encode('hex'), 16)
        part_10_lb = int(rcv[27].encode('hex'), 16)
        part_10 = int(part_10_hb * 256 + part_10_lb)
    
        print("Standard particle:")
        print("PM1: ", pm1_std, "ug/m^3  PM2.5: ", pm25_std, "ug/m^3  PM10: ", pm10_std, "ug/m^3")
        print("Atmospheric conditions:")
        print("PM1: ", pm1_atm, "ug/m^3  PM2.5: ", pm25_atm, "ug/m^3  PM10: ", pm10_atm, "ug/m^3")
        print("Number of particles:")
        print(">0.3: ", part_03, " >0.5: ", part_05, " >1.0: ", part_1, " >2.5: ", part_25, " >5: ", part_5, " >10: ", part_10)
        logfile.write("\n-------------------------------------------------------\n")
        logfile.write("Standard particle:\n")
        logfile.write("-------------------------------------------------------\n")
        logfile.write("| PM1: " + str(pm1_std) + "ug/m^3 | PM2.5: " + str(pm25_std) + "ug/m^3 | PM10: " + str(pm10_std) + "ug/m^3 |\n")
        logfile.write("-------------------------------------------------------\n")
        logfile.write("\nAtmospheric conditions:\n")
        logfile.write("-------------------------------------------------------\n")
        logfile.write("| PM1: " + str(pm1_atm) +  "ug/m^3 | PM2.5: " + str(pm25_atm) + "ug/m^3 | PM10: " + str(pm10_atm) + "ug/m^3 |\n")
        logfile.write("-------------------------------------------------------\n")
        logfile.write("\nNumber of particles:\n")
        logfile.write("----------------------------------------------------------------\n")
        logfile.write("| >0.3: " + str(part_03) + " | >0.5: " + str(part_05) + " | >1.0: " + str(part_1) + " | >2.5: " + str(part_25) +  " | >5: " + str(part_5) +  " | >10: " + str(part_10) + " |\n")
        logfile.write("----------------------------------------------------------------\n")
  
        
        print("\r\nReceived Data:" + repr(rcv))
        logfile.write("\r\nReceived Data:" + repr(rcv) + "\n\n")
        time.sleep(30)
except KeyboardInterrupt:
    print("goodbye")
    logfile.flush()
    logfile.close()
    time.sleep(3)
and my results look like:
Time and Date: 2019-09-02 10:18:23.449210

-------------------------------------------------------
Standard particle:
-------------------------------------------------------
| PM1: 1.0ug/m^3 | PM2.5: 3.0ug/m^3 | PM10: 7.0ug/m^3 |
-------------------------------------------------------

Atmospheric conditions:
-------------------------------------------------------
| PM1: 1.0ug/m^3 | PM2.5: 3.0ug/m^3 | PM10: 7.0ug/m^3 |
-------------------------------------------------------

Number of particles:
----------------------------------------------------------------
| >0.3: 531 | >0.5: 146 | >1.0: 26 | >2.5: 12 | >5: 4 | >10: 4 |
----------------------------------------------------------------

Received Data:'BM\x00\x1c\x00\x01\x00\x03\x00\x07\x00\x01\x00\x03\x00\x07\x02\x13\x00\x92\x00\x1a\x00\x0c\x00\x04\x00\x04\x97\x00\x02-'

Time and Date: 2019-09-02 10:18:53.481335

-------------------------------------------------------
Standard particle:
-------------------------------------------------------
| PM1: 1.0ug/m^3 | PM2.5: 4.0ug/m^3 | PM10: 7.0ug/m^3 |
-------------------------------------------------------

Atmospheric conditions:
-------------------------------------------------------
| PM1: 1.0ug/m^3 | PM2.5: 4.0ug/m^3 | PM10: 7.0ug/m^3 |
-------------------------------------------------------

Number of particles:
----------------------------------------------------------------
| >0.3: 558 | >0.5: 159 | >1.0: 32 | >2.5: 12 | >5: 4 | >10: 4 |
----------------------------------------------------------------

Received Data:'BM\x00\x1c\x00\x01\x00\x04\x00\x07\x00\x01\x00\x04\x00\x07\x02.\x00\x9f\x00 \x00\x0c\x00\x04\x00\x04\x97\x00\x02]'

User avatar
MrYsLab
Posts: 372
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: PMS5003 Sensor Coding - second opinion please

Mon Sep 02, 2019 2:20 pm

I can't verify if your output is correct or not, but if you have an Arduino and can verify that its output is correct, you can do an A/B test with the RPi and compare results.
I googled "PMS5003 python" and came up with lots of hits and one specifically for the RPi from Pimoroni: https://github.com/pimoroni/pms5003-python

Q_9
Posts: 2
Joined: Mon Sep 02, 2019 5:02 am

Re: PMS5003 Sensor Coding - second opinion please

Tue Sep 03, 2019 10:27 am

Thanks for your reply. I think I did actually come across that project but had some issues getting it going, from memory some linked/imported file or something wasn't found and I couldn't get it for some reason. That's pretty much why I ended up using 3 or so different projects. Will have another look.

Actually I did order some arduino's as well as there seems to be more of this type of project with them, but they haven't arrived yet. I've discovered quite quickly that most of the sensors will work with either/any platform, but there does seem to be varying levels of projects out there.

Return to “Python”