Skip to content

Instantly share code, notes, and snippets.

@qsun
Created December 10, 2019 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qsun/718e1de3a2701979f78019f7475916cd to your computer and use it in GitHub Desktop.
Save qsun/718e1de3a2701979f78019f7475916cd to your computer and use it in GitHub Desktop.
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