johnsonzzd 发表于 2016-1-14 20:04:29

攀藤G5霾表

本帖最后由 johnsonzzd 于 2016-1-14 20:08 编辑

淘宝微创联合买的传感器,花俩小时做一个PM2.5表,检测一下空气净化器的能力。霾表在液晶显示的同时向PC机发送数据。PC可以长时间监控并对数据进行处理。




程序展示了对16进制通讯协议进行处理的一种常用方法。


#include <AltSoftSerial.h>
#include <LiquidCrystal_I2C.h>

//软串口,Uno开发板:Tx-D9、Rx-D8。Rx接传感器的Tx。
AltSoftSerial altSerial;

//YWrobot I2C 1602液晶屏
LiquidCrystal_I2C lcd(0x27, 16, 2);// set the LCD address to 0x27 for a 16 chars and 2 line display

//攀藤G5的数据格式
struct _panteng {
        unsigned char len;
        unsigned char pm1_cf1;
        unsigned char pm2_5_cf1;
        unsigned char pm10_0_cf1;
        unsigned char pm1_0;
        unsigned char pm2_5;
        unsigned char pm10_0;
        unsigned char d;
} panteng;

void setup()
{
        lcd.init();                      // initialize the lcd
        lcd.backlight();

        Serial.begin(115200);        //USB串口向PC发送数据
        altSerial.begin(9600);        //软串口连接传感器
}

void loop()
{
        unsigned char c;
        char str;
        static int state = 0;
        static int count = 0;
        static int time=0;
        int pm1_0, pm2_5, pm10_0;        //PM1.0、PM2.5、PM10
        int i;

        if (altSerial.available()) {
                c = altSerial.read();
                switch (state) {
                case 0:
                        if (0x42 == c)
                                state = 1;
                        break;
                case 1:
                        if (0x4d == c) {
                                state = 2;
                                count = 0;
                                //Serial.println(' ');
                        }
                        break;
                case 2:
                        ((unsigned char *) &panteng) = c;
                        sprintf(str, "%02X ", c);
                        //Serial.print(str);
                        if (count > 28) {
                                state = 0;
                                pm1_0 = panteng.pm1_0 * 256 + panteng.pm1_0;
                                pm2_5 = panteng.pm2_5 * 256 + panteng.pm2_5;
                                pm10_0 = panteng.pm10_0 * 256 + panteng.pm10_0;

                                sprintf(str, "%d\t%d\t%d\t%d", time++,pm1_0, pm2_5, pm10_0);
                                Serial.println(str);

                                snprintf(str,16, "PM2.5=%d    ", pm2_5);
                                //lcd.clear();
                                for (i = 0; i < strlen(str); i++) {
                                        lcd.setCursor(i, 0);
                                        lcd.print(&str);
                                }
                        }
                        break;
                default:
                        break;
                }
        }
}

anduony 发表于 2016-1-14 21:15:52

先前也查询了,测试的,你这个使用的是激光测试的模块吗

pumpkingiggle 发表于 2016-1-15 08:04:12

AltSoftSerial.h与IDE自带的SoftwareSerial.h有什么区别吗?

wetnt 发表于 2016-3-22 12:08:58

有个地方不太明白,攀藤G5的数据是多久更新一次?是他自己更新,不是通过命令读取?

lovezypj 发表于 2016-3-22 15:23:39

你都不断给pc发数据了,为啥不直接用usb转串口,pc采集呢?

radiocat 发表于 2016-3-30 10:43:39

请教下楼主:这个1602的二个数据口分别接到arduino的哪二个口呢?谢谢!

托米 发表于 2016-3-30 20:47:18

请教楼主
pm1_0 = panteng.pm1_0 * 256 + panteng.pm1_0;
                              pm2_5 = panteng.pm2_5 * 256 + panteng.pm2_5;
                              pm10_0 = panteng.pm10_0 * 256 + panteng.pm10_0;

                              sprintf(str, "%d\t%d\t%d\t%d", time++,pm1_0, pm2_5, pm10_0);
                              Serial.println(str);

                              snprintf(str,16, "PM2.5=%d    ", pm2_5);
看到读取了pm1.0,pm2.5,pm10,三个数据,但最后只显示了pm2.5,那是不是也可以只读区2.5的数据呢

fyuino 发表于 2016-4-22 13:01:10

需要用校验位吗?:)
页: [1]
查看完整版本: 攀藤G5霾表