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 连接电脑也可以)

    在来张实拍图:

  • 相关阅读:
    echarts + timeline 显示多个options
    微信如何获取unionid 并且打通微信公众号和小程序
    枚举
    十三、springboot集成定时任务(Scheduling Tasks)
    十二、springboot之web开发之静态资源处理
    十一、springboot之web开发之Filter
    十、springboot之web开发打包生产
    九、springboot整合redis二之缓冲配置
    RedisTemplate使用
    八、springboot整合redis
  • 原文地址:https://www.cnblogs.com/nightnine/p/6572891.html
Copyright © 2011-2022 走看看