zoukankan      html  css  js  c++  java
  • PMS5003ST+Arduino Nano 串口读取数据

    先上代码:

    库文件是在guihub上的大神写的https://github.com/jbanaszczyk,我拿来小改下用以支持5003ST

    #include <Arduino.h>
    #include <pms.h>
    
    ////////////////////////////////////////
    
    #if defined PMS_DYNAMIC
    Pms5003 *_pms;
    #define pms (*_pms)
    #else
    Pms5003 pms;
    #endif
    
    ////////////////////////////////////////
    
    auto lastRead = millis();
    
    void setup( void ) {
        Serial.begin( 115200 );
        while ( !Serial ) { };
        Serial.println( "PMS5003ST" );
    
    #if defined PMS_DYNAMIC
        _pms = new Pms5003();
    #else
        pms.begin();
    #endif
    
        //    pms.write( Pms5003::cmdWakeup );
        //    pms.write( Pms5003::cmdModePassive );
    
        //    Serial.println( pms.getDataSize() );
        //    pms.write( Pms5003::cmdModeActive );
    }
    
    ////////////////////////////////////////
    
    void loop( void ) {
    
    
        const int n = Pms5003::Reserved;
        Pms5003::pmsData data[ n ];
    
        Pms5003::PmsStatus status = pms.read( data, n );
    
        switch ( status ) {
            case Pms5003::OK:
            {
    
                Serial.println( "_________________" );
                auto newRead = millis();
                Serial.print( "Wait time " );
                Serial.println( newRead - lastRead );
                lastRead = newRead;
    
    
                for ( size_t i = Pms5003::PM1dot0; i < n; ++i ) {
                    if(i == n-3)
                       Serial.print( float(data[ i ]/1000) );
                    if(i == n-2 || i == n-1)
                        Serial.print( float(data[ i ]/10)); 
                    else
                      Serial.print( data[ i ] );
                    Serial.print( "	" );
                    Serial.print( Pms5003::dataNames[ i ] );
                    Serial.print( " [" );
                    Serial.print( Pms5003::metrics[ i ] );
                    Serial.print( "]" );
                    Serial.println();
                }
                break;
            }
            case Pms5003::noData:
                break;
            default:
                Serial.println( "_________________" );
                Serial.println( Pms5003::errorMsg[ status ] );
        };
    }

    接线图:

    PIN1-----> 5V

    PIN2----->GND

    PIN5----->D8(要接电平转换板,PIN5要3.3V,而D8输出是5V;或者直接用  USB转TTL 连接电脑也可以)

    在来张实拍图:

  • 相关阅读:
    03.友盟项目--原始日志数据生成(改进版)---redis存储 用户设备的信息
    Java中的字符集
    时间复杂度
    Hive建模
    08、Spark常用RDD变换
    06、部署Spark程序到集群上运行
    05、Spark
    04、Spark Standalone集群搭建
    02、体验Spark shell下RDD编程
    03、IDEA下Spark API编程
  • 原文地址:https://www.cnblogs.com/nightnine/p/6572891.html
Copyright © 2011-2022 走看看