Created
December 10, 2019 06:31
-
-
Save qsun/718e1de3a2701979f78019f7475916cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import aqi | |
def read_frame(ser): | |
bs = ser.read(2) | |
# print(['0x{:02x}'.format(x) for x in bs]) | |
length = bs[0]*256+bs[1] | |
frame = ser.read(length) | |
pm1_0 = frame[0]*256+frame[1] | |
pm2_5 = frame[2]*256+frame[3] | |
pm10 = frame[4]*256+frame[5] | |
print("Result: \n==================\n") | |
print("Standard particles") | |
print("PM1.0: " + str(pm1_0)) | |
print("PM2.5: " + str(pm2_5)) | |
print("PM10: " + str(pm10)) | |
print("\nAtmospheric Environment") | |
pm1_0 = frame[6]*256+frame[7] | |
pm2_5 = frame[8]*256+frame[9] | |
pm10 = frame[10]*256+frame[11] | |
print("PM1.0: " + str(pm1_0)) | |
print("PM2.5: " + str(pm2_5)) | |
print("PM10: " + str(pm10)) | |
# only atmospheric environment is related | |
cn_aqi = aqi.to_aqi([ | |
(aqi.POLLUTANT_PM10, pm10), | |
(aqi.POLLUTANT_PM25, pm2_5) | |
], algo=aqi.ALGO_MEP) | |
us_aqi = aqi.to_aqi([ | |
(aqi.POLLUTANT_PM10, pm10), | |
(aqi.POLLUTANT_PM25, pm2_5) | |
], algo=aqi.ALGO_EPA) | |
print("\nAQI DATA") | |
print("CN AQI: %d"%(cn_aqi,)) | |
print("US AQI: %d" % (us_aqi,)) | |
if __name__ == '__main__': | |
with serial.Serial('COM43', 9600) as ser: | |
x = ser.read(1) | |
if x[0] == 0x42: | |
x = ser.read(1) | |
if x[0] == 0x4d: | |
read_frame(ser) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment