创客百科

姿势共享,有节操无门槛参与的创客百科,创客动力之源 \ (^_^) /

用户工具

站点工具


note:spoony:树莓派从pm25传感器读取数据

树莓派从pm25传感器读取数据

GP2Y105AU0F 粉尘传感器

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)
本页面的其他翻译:
note/spoony/树莓派从pm25传感器读取数据.txt · 最后更改: 2016/12/31 14:59 (外部编辑)