这是本文档旧的修订版!
====== 树莓派从pm25传感器读取数据 ====== GP2Y105AU0F 粉尘传感器 <code> def pm25(): ser = serial.Serial('/dev/ttyAMA0', 2400, timeout=0.5) arr =[] for num in range(0,30): getchr = ser.read(10) hexstr = "%s" % getchr.encode('hex') #print(hexstr) a = hexstr.find('aa') a1= hexstr[a+2:a+4] a2= hexstr[a+4:a+6] #print (a1,a2) a1 = int(a1, 16) a2 = int(a2, 16) pm25 = (float(a1) * 256 + float(a2))/1024*5*900 pm25 = round(pm25,2) arr+=[pm25] print(num,pm25) endTime = time.time() time.sleep(1) ser.close() return round(sum(arr)/len(arr),2) </code>