Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 校验和故障和16962的读数反复出现_C++_Arduino Uno_Adafruit - Fatal编程技术网

C++ 校验和故障和16962的读数反复出现

C++ 校验和故障和16962的读数反复出现,c++,arduino-uno,adafruit,C++,Arduino Uno,Adafruit,我正在创建一个程序,从plantower 5003接收数据,并使用Adafruit数据记录器Sheild和Arduino Uno板将数据写入SD卡。它由一个外部电池组供电,因为目标是能够在家里安装它,让它收集数据约2天,或者直到电池耗尽。我只想把日期,时间,下午2点5分,10分,3分,5分打印出来 目前,它会将数据打印到SD卡上,大约在前30次测量中,打印效果很好,但之后会一遍又一遍地吐出“16962”,偶尔会返回到正常读数(我家大约10-30)一行,然后返回到16962 它还向串行监视器打印大

我正在创建一个程序,从plantower 5003接收数据,并使用Adafruit数据记录器Sheild和Arduino Uno板将数据写入SD卡。它由一个外部电池组供电,因为目标是能够在家里安装它,让它收集数据约2天,或者直到电池耗尽。我只想把日期,时间,下午2点5分,10分,3分,5分打印出来

目前,它会将数据打印到SD卡上,大约在前30次测量中,打印效果很好,但之后会一遍又一遍地吐出“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;
}

readBytes
会失败吗?我不确定。我没有收到任何错误,特别是说它可以
readBytes
失败?我不确定。我没有任何错误明确地说出来
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