Gravity: PM2.5激光粉尘环境质量传感器 V2

2023-02-107676
AI 快速预览详细 收起
本文介绍了如何解决Gravity: PM2.5激光粉尘环境质量传感器 V2数据一直为零的问题。通过检查0x42和0x4D标志位以及数据校验和,可以有效定位和解决传感器数据读取问题。具体步骤包括设置串口超时时间、读取数据包、验证数据包完整性等。通过这些步骤,用户可以快速修复传感器数据读取问题,确保传感器正常工作。
Gravity: PM2.5激光粉尘环境质量传感器 V2图1
我得到的数据一直是零有人知道是怎么改吗

#include <Arduino.h>#define LENG 31   //0x42 + 31 bytes equal to 32 bytesunsigned char buf[LENG;int PM01Value=0;          //define PM1.0 value of the air detector moduleint PM2_5Value=0;         //define PM2.5 value of the air detector moduleint PM10Value=0;         //define PM10 value of the air detector modulevoid setup(){  Serial.begin(9600);   //use serial0  Serial.setTimeout(1500);    //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor}void loop(){  if(Serial.find(0x42)){    //start to read when detect 0x42    Serial.readBytes(buf,LENG);    if(buf[0 == 0x4d){      if(checkValue(buf,LENG)){        PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module        PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module        PM10Value=transmitPM10(buf); //count PM10 value of the air detector module      }    }  }  static unsigned long OledTimer=millis();    if (millis() - OledTimer >=1000)    {      OledTimer=millis();      Serial.print("PM1.0: ");      Serial.print(PM01Value);      Serial.println("  ug/m3");      Serial.print("PM2.5: ");      Serial.print(PM2_5Value);      Serial.println("  ug/m3");      Serial.print("PM1 0: ");      Serial.print(PM10Value);      Serial.println("  ug/m3");      Serial.println();    }}char checkValue(unsigned char *thebuf, char leng){  char receiveflag=0;  int receiveSum=0;  for(int i=0; i<(leng-2); i++){  receiveSum=receiveSum+thebuf[i;  }  receiveSum=receiveSum + 0x42;  if(receiveSum == ((thebuf[leng-2<<8)+thebuf[leng-1))  //check the serial data  {    receiveSum = 0;    receiveflag = 1;  }  return receiveflag;}int transmitPM01(unsigned char *thebuf){  int PM01Val;  PM01Val=((thebuf[3<<8) + thebuf[4); //count PM1.0 value of the air detector module  return PM01Val;}//transmit PM Value to PCint transmitPM2_5(unsigned char *thebuf){  int PM2_5Val;  PM2_5Val=((thebuf[5<<8) + thebuf[6);//count PM2.5 value of the air detector module  return PM2_5Val;  }//transmit PM Value to PCint transmitPM10(unsigned char *thebuf){  int PM10Val;  PM10Val=((thebuf[7<<8) + thebuf[8); //count PM10 value of the air detector module  return PM10Val;}


创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -