校验和失败以及反复读取16962

时间:2019-03-24 19:27:19

标签: c++ arduino-uno adafruit

我正在创建一个程序,该程序使用Adafruit Data Logger Sheild和Arduino Uno板从播种机5003接收数据并将其写入SD卡。它由外部电池组供电,因为其目标是能够在房子中安装它,并使其能够收集数据约2天,或者直到电池用完为止。我只希望它打印出日期,时间,2.5ppm,10ppm,3um,5um。

当前,它将在大约前30次测量中将数据正确打印到SD卡,但随后会反复吐出“ 16962”,并偶尔恢复正常读数(在我家,大约为10-30)一行,然后返回到16962。

它还会在大约4次良好读数后打印到串行监视器,然后校验和失败。

//The first few readings may be all 0's due to the sensor needing to start up
//but the SD card starts before it so it writes 0's since the sensor is not awake yet
#include <SD.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include "RTClib.h"


//Real time clock
RTC_PCF8523 rtc;
//The data enterss with pin 0, and writes to pin 10
SoftwareSerial pmsSerial(0, 10);
//SD pin is 10 with the sheild
const int chipSelect = 10;
void setup() {
  // our debugging output
  Serial.begin(19200);

  // sensor baud rate is 9600

  pmsSerial.begin(9600);

 Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
//opens CSV file
File csvFile = SD.open("datalog2.csv", FILE_WRITE);

if (csvFile) {

//Headers for the data
csvFile.println("Date,Time,25PPM,100PPM,03UM,05UM");
   csvFile.close();
}
//Start the RTC
rtc.begin();

    //Uncomment if you need to reset the time and date from a computer -> should only have to if the coin cell battery dies(~5 years)
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
//Sets up the Plantower 5003 info
struct pms5003data {
  uint16_t framelen;
  uint16_t pm10_standard, pm25_standard, pm100_standard;
  uint16_t pm10_env, pm25_env, pm100_env;
  uint16_t particles_03um, particles_05um, particles_10um, particles_25um, particles_50um, particles_100um;
  uint16_t unused;
  uint16_t checksum;
};

struct pms5003data data;


void loop() {
  //Calls the RTC at the moment just before printing to get a snapshot of the current time
DateTime now = rtc.now();

File csvFile = SD.open("datalog3.csv", FILE_WRITE);
if (csvFile) {
  //printing the date, time, and PPM info to a CSV file that can be opened in Excel
    csvFile.print(now.year());
          csvFile.print("/"); 
    csvFile.print(now.month());   
          csvFile.print("/");
    csvFile.print(now.day());
           csvFile.print(",");

    csvFile.print(now.hour());
          csvFile.print(":"); 
    csvFile.print(now.minute());   
          csvFile.print(":"); 
    csvFile.print(now.second());  
          csvFile.print(",");

    csvFile.print(data.pm25_standard);
          csvFile.print(",");
    csvFile.print(data.pm100_standard);
          csvFile.print(",");
    csvFile.print(data.particles_03um);
          csvFile.print(",");
    csvFile.println(data.particles_05um);

    csvFile.close();
}

// Telling it to collect/write data every 1 second (1000) , or 60 seconds (60000)
delay(1000);


  if (readPMSdata(&pmsSerial)) {

    //Prints to serial monitor
   // reading data was successful!
    Serial.println();
    Serial.println("---------------------------------------");
    Serial.println("Concentration Units (standard)");
    Serial.print("PM 1.0: "); Serial.print(data.pm10_standard);
    Serial.print("\t\tPM 2.5: "); Serial.print(data.pm25_standard);
    Serial.print("\t\tPM 10: "); Serial.println(data.pm100_standard);
    Serial.println("---------------------------------------");
    Serial.println("Concentration Units (environmental)");
    Serial.print("PM 1.0: "); Serial.print(data.pm10_env);
    Serial.print("\t\tPM 2.5: "); Serial.print(data.pm25_env);
    Serial.print("\t\tPM 10: "); Serial.println(data.pm100_env);
    Serial.println("---------------------------------------");
    Serial.print("Particles > 0.3um / 0.1L air:"); Serial.println(data.particles_03um);
    Serial.print("Particles > 0.5um / 0.1L air:"); Serial.println(data.particles_05um);
    Serial.print("Particles > 1.0um / 0.1L air:"); Serial.println(data.particles_10um);
    Serial.print("Particles > 2.5um / 0.1L air:"); Serial.println(data.particles_25um);
    Serial.print("Particles > 5.0um / 0.1L air:"); Serial.println(data.particles_50um);
    Serial.print("Particles > 10.0 um / 0.1L air:"); Serial.println(data.particles_100um);
    Serial.println("---------------------------------------");  
  }


}
//Dont touch, from internet, needed for the sensor to work properly 
boolean readPMSdata(Stream *s) {
  if (! s->available()) {
    return false;
  }

  // Read a byte at a time until we get to the special '0x42' start-byte
  if (s->peek() != 0x42) {
    s->read();
    return false;
  }

  // Now read all 32 bytes
  if (s->available() < 32) {
    return false;
  }

  uint8_t buffer[32];    
  uint16_t sum = 0;
  s->readBytes(buffer, 32);

  // get checksum ready
  for (uint8_t i=0; i<30; i++) {
    sum += buffer[i];
  }

  /* debugging
  for (uint8_t i=2; i<32; i++) {
    Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
  }
  Serial.println();
  */

  // The data comes in endian'd, this solves it so it works on all platforms
  uint16_t buffer_u16[15];
  for (uint8_t i=0; i<15; i++) {
    buffer_u16[i] = buffer[2 + i*2 + 1];
    buffer_u16[i] += (buffer[2 + i*2] << 8);
  }

  // put it into a nice struct :)
  memcpy((void *)&data, (void *)buffer_u16, 30);

  if (sum != data.checksum) {
    Serial.println("Checksum failure");
    return false; 
  }
  // success!
  return true;
}

这是我当前获得的数据的示例

3/24/2019   14:56:49    0   0   0   0
3/24/2019   14:56:50    12  12  1596    470
3/24/2019   14:56:52    12  12  1596    470
3/24/2019   14:56:53    12  12  1596    470
3/24/2019   14:56:54    13  13  1590    463
3/24/2019   14:56:55    13  13  1590    463
3/24/2019   14:56:56    13  13  1590    463
3/24/2019   14:56:57    13  13  1590    463
3/24/2019   14:56:58    13  13  1590    463
3/24/2019   14:56:59    13  13  1590    463
3/24/2019   14:57:00    13  13  1590    463
3/24/2019   14:57:01    13  13  1590    463
3/24/2019   14:57:02    13  13  1590    463
3/24/2019   14:57:03    13  13  1590    463
3/24/2019   14:57:04    13  13  1590    463
3/24/2019   14:57:05    13  13  1590    463
3/24/2019   14:57:06    13  13  1590    463
3/24/2019   14:57:08    13  13  1590    463
3/24/2019   14:57:09    13  13  1590    463
3/24/2019   14:57:10    13  13  1590    463
3/24/2019   14:57:11    13  13  1590    463
3/24/2019   14:57:12    13  13  1590    463
3/24/2019   14:57:13    13  13  1590    463
3/24/2019   14:57:14    13  13  1590    463
3/24/2019   14:57:15    13  13  1590    463
3/24/2019   14:57:16    13  13  1590    463
3/24/2019   14:57:17    13  13  1590    463
3/24/2019   14:57:18    13  13  1590    463
3/24/2019   14:57:19    13  13  1590    463
3/24/2019   14:57:20    13  13  1590    463
3/24/2019   14:57:21    13  13  1590    463
3/24/2019   14:57:22    13  13  1590    463
3/24/2019   14:57:23    13  13  1590    463
3/24/2019   14:57:24    13  13  1590    463
3/24/2019   14:57:25    13  13  1590    463
3/24/2019   14:57:26    13  13  1590    463
3/24/2019   14:57:27    13  13  1590    463
3/24/2019   14:57:28    16962   16962   16962   16962
3/24/2019   14:57:29    16962   16962   16962   16962
3/24/2019   14:57:30    16962   16962   16962   16962
3/24/2019   14:57:32    16962   16962   16962   16962
3/24/2019   14:57:33    16962   16962   16962   16962
3/24/2019   14:57:34    16962   16962   16962   16962
3/24/2019   14:57:35    16962   16962   16962   16962
3/24/2019   14:57:36    16962   16962   16962   16962
3/24/2019   14:57:37    16962   16962   16962   16962
3/24/2019   14:57:38    16962   16962   16962   16962
3/24/2019   14:57:39    16962   16962   16962   16962
3/24/2019   14:57:40    16962   16962   16962   16962
3/24/2019   14:57:41    16962   16962   16962   16962
3/24/2019   14:57:42    16962   16962   16962   16962
3/24/2019   14:57:43    16962   16962   16962   16962
3/24/2019   14:57:44    16962   16962   16962   16962
3/24/2019   14:57:45    16962   16962   16962   16962
3/24/2019   14:57:46    16962   16962   16962   16962
3/24/2019   14:57:47    16962   16962   16962   16962
3/24/2019   14:57:48    16962   16962   16962   16962
3/24/2019   14:57:49    16962   16962   16962   16962
3/24/2019   14:57:50    16962   16962   16962   16962
3/24/2019   14:57:51    16962   16962   16962   16962
3/24/2019   14:57:52    16962   16962   16962   16962
3/24/2019   14:57:53    16962   16962   16962   16962
3/24/2019   14:57:54    16962   16962   16962   16962
3/24/2019   14:57:55    16962   16962   16962   16962
3/24/2019   14:57:56    16962   16962   16962   16962
3/24/2019   14:57:57    16962   16962   16962   16962
3/24/2019   14:57:58    16962   16962   16962   16962
3/24/2019   14:57:59    16962   16962   16962   16962
3/24/2019   14:58:00    16962   16962   16962   16962
3/24/2019   14:58:01    11  12  1614    431
3/24/2019   14:58:03    16962   16962   16962   16962
3/24/2019   14:58:04    16962   16962   16962   16962
3/24/2019   14:58:05    16962   16962   16962   16962
3/24/2019   14:58:06    16962   16962   16962   16962

0 个答案:

没有答案