Go Down

Topic: Writing data from 2 sensors to sd card (Read 1 time) previous topic - next topic

kmmrao

Hi, I'm trying to write the data from Plantower PMS5003 dust sensor and DHT11 temperature and humidity sensor. But I am unable to write it continuously. The dust sensor data is missing in the middle. Please, someone, help Output showing some error.
My program and out are as follows.




#include <SoftwareSerial.h>
#include <SPI.h>                        //Serial library for SD card shield
#include <SD.h>                         //SD card library
#include <RTClib.h>
#include <Wire.h>
#include "DHT.h"
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

SoftwareSerial pmsSerial(2, 3);
const int chipSelect = 10;               //chip select on digital 4 for SD card shield
RTC_DS1307 rtc;
char buf1[20];

void setup() {

  Serial.begin(115200);                   //begin serial comms
  while (!Serial) {
    ;                                   // wait for serial port to connect. Needed for native USB port only
  }

 Wire.begin();
  rtc.begin();
  dht.begin();
  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));


  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:
    return;
  }
  Serial.println("card initialized.");

    pmsSerial.begin(9600);                      //begin the pms sensor
   // dataFile.print("Date&Time\t\tP.M1.0(S)\t\tP.M2.5(S)\t\tP.M10.0(S)\t\tP.M1.0(E)\t\tP.M2.5(E)\t\tP.M10.0(E)"));
}

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() {
  // reading data was successful!
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if (dataFile)
if (readPMSdata(&pmsSerial)) {
 

  DateTime now = rtc.now();

  sprintf(buf1, "%02d:%02d:%02d %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year()); 

    dataFile.println();
    dataFile.print(F("Date/Time:\t "));
    dataFile.print(buf1);
    dataFile.print("\t\t");dataFile.print(data.pm10_standard);
    dataFile.print("\t"); dataFile.print(data.pm25_standard);
    dataFile.print("\t"); dataFile.print(data.pm100_standard);
    dataFile.print("\t"); dataFile.print(data.pm10_env);
    dataFile.print("\t"); dataFile.print(data.pm25_env);
    dataFile.print("\t"); dataFile.print(data.pm100_env);
    dataFile.print("\t"); dataFile.print(data.particles_03um+1);
    dataFile.print("\t"); dataFile.print(data.particles_05um);
    dataFile.print("\t"); dataFile.print(data.particles_10um);
    dataFile.print("\t"); dataFile.print(data.particles_25um);
    dataFile.print("\t"); dataFile.print(data.particles_50um);
    dataFile.print("\t"); dataFile.print(data.particles_100um);
    //dataFile.println();
 
}
 else {
    dataFile.println("error opening datalog.txt");
  }

//***************************PMS5003 END & DHT BEGIN *****************
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  //dataFile.print("Humidity: ");
  dataFile.print("\t");dataFile.print(h);
  dataFile.print(" %\t");
  //dataFile.print("Temperature %\t: ");
  dataFile.print("\t");dataFile.print(t);
  dataFile.print(" *C ");
  //dataFile.print(f);
  //dataFile.print(" *F\t");
  //dataFile.print("Heat index: ");
  dataFile.print("\t");dataFile.print(hic);
  dataFile.print(" *C ");
  //dataFile.print(hif);
  //dataFile.println(" *F");
  dataFile.close();


}


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;
}


/* debugging
for (uint8_t i=2; i<32; i++) {
Serial.print("0x"); Serial.print(buffer, HEX); Serial.print(", ");
}
Serial.println();
© Adafruit Industries https://learn.adafruit.com/pm25-air-quality-sensor Page 6 of 12
You'll see data printed out once a second, with all the measurements. For a clean-air indoor room you'll see something
like this:
If you hold up a smoking soldering iron or something else that creates a lot of dust, you'll see much higher numbers!
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 = buffer[2 + i*2 + 1];
buffer_u16 += (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;
}

AWOL

And now edit in the code tags please.

Go Up