极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: centerqi

攀藤 G3 粉尘激光传感器

[复制链接]
发表于 2016-3-7 22:32:02 | 显示全部楼层
lovezypj 发表于 2016-1-13 22:30
if(mCheck == mPkt[22]*256+mPkt[23])//crc ok

你这个校验不对吧?


#include <Arduino.h>
#define LENG 32
char buf[LENG];

int PM01Value=0;          //define PM1.0 value of the air detector module
int PM2_5Value=0;         //define PM2.5 value of the air detector module
int PM10Value=0;         //define PM10 value of the air detector module

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available())
  {
    Serial.readBytes(buf,LENG);
    if(buf[0] == 0x42 && buf[1] == 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: ");  //send PM1.0 data to bluetooth
      Serial.print(PM01Value);
      Serial.println("  ug/m3");            
     
      Serial.print("PM2.5: ");  //send PM1.0 data to bluetooth
      Serial.print(PM2_5Value);
      Serial.println("  ug/m3");     
      
      Serial.print("PM10:  ");  //send PM1.0 data to bluetooth
      Serial.print(PM10Value);
      Serial.println("  ug/m3");   
    }
   
}
char checkValue(char *thebuf, char leng)
{  
  char receiveflag=0;
  int receiveSum=0;
  char i=0;

  for(i=0;i<leng;i++)
  {
  receiveSum=receiveSum+thebuf;
  }
   
  if(receiveSum==((thebuf[leng-2]<<8)+thebuf[leng-1]+thebuf[leng-2]+thebuf[leng-1]))  //check the serial data
  {
    receiveSum=0;
    receiveflag=1;
  }
  return receiveflag;
}

int transmitPM01(char *thebuf)
{
  int PM01Val;
  PM01Val=((thebuf[4]<<8) + thebuf[5]); //count PM1.0 value of the air detector module
  return PM01Val;
}

//transmit PM Value to PC
int transmitPM2_5(char *thebuf)
{
  int PM2_5Val;
  PM2_5Val=((thebuf[6]<<8) + thebuf[7]);//count PM2.5 value of the air detector module
  return PM2_5Val;
  }

//transmit PM Value to PC
int transmitPM10(char *thebuf)
{
  int PM10Val;
  PM10Val=((thebuf[8]<<8) + thebuf[9]); //count PM10 value of the air detector module  
  return PM10Val;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-4 12:28 , Processed in 0.044456 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表