Feinstaubdatenlogger

Hallo zusammen

Ich bin gerade daran eine Feinstaubdatenlogger zu bauen, welcher mittels einem Feinstaubsensor und einem DHT22 Sensor, die Messwerte PM10, PM2.5, Temperatur und Luftfeuchtigkeit erfasst.

Ich benutze dazu ein Arduino R3 UNO Board und ein Data Logger Shield für Arduino MicroSD Card mit RTC. Das Data Logger Shield ist am Pin 9, der DHT am Pin 2 und der Feinstaubsensor am Pin 5 und 6 angeschlossen.

Ich habe alles einzeln geteste und es hat auch funktioniert. Nun wollte ich die Programmierung zusammenfügen und die Aufzeichnung auf der SD Karte verbessern. Nun erhalte ich aber ein Fehlermeldung beim Kompilieren, leider kann ich den Fehler nicht alleine lösen. Deshalb hoffe ich, dass mir hier jemand sagen kann, was ich ändern muss.

Die Fehlermeldung ist:

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "Arduino/Genuino Uno"

In function 'void loop()':

Datalogger_Feinstaub2:143:9: error: expected initializer before 'strDate'

        strDate = String((int)date1.year());

        ^

Datalogger_Feinstaub2:154:18: error: 'dataFile' was not declared in this scope

 dataFile(strDate);

                 ^

Datalogger_Feinstaub2:156:46: error: 'FILE_Write' was not declared in this scope

      File dataFile = SD.open("datalog.txt", FILE_Write); //open file 

                                             ^

In function 'String twoDigit(int)':

Datalogger_Feinstaub2:203:25: error: 'String2' was not declared in this scope

    return String("0" + String2); //pad with leading zero

                        ^

Mehrere Bibliotheken wurden für "SD.h" gefunden
Benutzt: 
ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\SD

Nicht benutzt: Arduino\libraries\SD-master

exit status 1
expected initializer before 'strDate'

Und noch der Sketch

#include <cactus_io_DHT22.h>
 #include <DHT.h>
 #include <SPI.h> //SPI communication with SD-card controller
 #include <SD.h> //SD-Card headers
 #include <Wire.h> //I2C communication with RTC (Real Time Clock)
 #include "RTClib.h" //Real Time Clock header
 RTC_DS1307 rtc; //define the Real Time Clock object // Adafruit SD-card logger
 #include <SoftwareSerial.h> //Header for serial communication with sensor(s)

SoftwareSerial portTwo(5, 6);
int Nova_PM25;
int Nova_PM10;
unsigned long Sum_PM25;
unsigned long Sum_PM10;
int nSamples;


#define DHT22_Pin 2 //what pin on the Arduino is the DHT22 data line connected to
int a,b,c,d,e,f=0;
unsigned long my_time=0;
unsigned long sec = 1000;
unsigned long tmp=0;
unsigned long currentTime;

// Initialize DHT sensor for normal 16mhz Arduino.
DHT22 dht(DHT22_Pin);

/* Generic logging variables */
boolean boolStart=true;
const unsigned long loggerInt=30UL; // logger intervall, seconds. Should not be less than 30 seconds. "UL" means Unsigned Long integer
unsigned long nextLog;
unsigned long micro0;
unsigned long micro1;
unsigned long micro2; //present time + 1 second (10^6 microseconds)

/*Miscellaneous constants */
const char charDash='-';
const char charColon=':';

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {;} // wait for serial port to connect. Needed for native USB port only
  SD.begin(9); //Digital pin 10 is for Adafruit SD-shield
  rtc.begin();
  
  Serial.begin(9600);
  // Serial.println("DHT22 Humidity - Temperature Sensor");
  // Serial.println("RH\t\tTemp (C)\tTemp");
  dht.begin();

  //Inovafitness sensor:
  portTwo.begin(9600);
  Sum_PM25=0UL;
  Sum_PM10=0UL;
  nSamples=0;

  //Logging:
  DateTime date1 = rtc.now();
  nextLog= long(date1.unixtime());
  micro1=micros();
  micro2=micro1+1000000;
}

void loop() {
  unsigned long thisLog;
  String strLine;
  String strDate;
  int ii;
  char index;
  char receiveflag;
  int receiveSum;
  unsigned char _buffer[32]; //32-byte buffer for reading serial bus

  dht.readHumidity();
  dht.readTemperature();

  //Check if any reads failed and exit early (to try again).
  if (isnan(dht.humidity) || isnan(dht.temperature_C))
  {
   Serial.println("DHT sensor read failure!");
   return;
  }
  // Serial.print(dht.humidity); Serial-print ("%\t");
  // Serial.print(dht.temperature_C);
  // Serial.print(" *C\t");
  // Serial.print("Time: ");

  //currentTime=millis();

  // Wait a few seconds between measurements. The DHT22 should not be read at a higher frequency of
  // about once every 2 seconds. So we add a 3 second delay to cober this.
  delay(10000);


  // Temperatur and humidity measurement


  micro0=micro1;
  micro1=micros();
  if (micro1<micro0) {// In case the Counter overflows (gets automatically zeroed) approx every 70 minutes.
    micro2=micro1+1000000; //reset second target
  } else if (micro2<=micro1) {// if 1 second passed
    // Read Inovafitness sensor:
    //Step1: read 10 bytes from serial bus
    index=0;
    while (portTwo.available() && index !=10) {
      _buffer[index] = portTwo.read();
      index++;
    }
    while (portTwo.read() != -1) {}
    //Step2: Check the package integrity
    if (_buffer[0] == 0xAA && _buffer[1] == 0xC0 && _buffer[9] == 0xAB) {
      receiveSum = 0;
      for (int i = 2; i < 8; i++) {receiveSum += _buffer[i];}

      if ((receiveSum & 0xFF) == _buffer[8]) { //package is valid
        Nova_PM25 = ((_buffer[3] << 8) + _buffer[2]);
        Nova_PM10 = ((_buffer[5] << 8) + _buffer[4]);
        Sum_PM25 += (long)Nova_PM25;
        Sum_PM10 += (long)Nova_PM10; 
        nSamples += 1;
      }
    }
    micro2=micro2+1000000; // next log in 1 second

    DateTime date1 = rtc.now(); //read time from Real Time Clock (RTC)
    thisLog = long(date1.unixtime()); //convert time to integer seconds
    if (nextLog <= thisLog) {  void twoDigits (int sec, int min, int hrs, String title)
    
        strDate = String((int)date1.year());
        strDate += charDash;
        strDate += twoDigit((int)date1.month());
        strDate += charDash;
        strDate += twoDigit((int)date1.day());
        strDate += ' ';
        strDate += twoDigit((int)date1.hour());
        strDate += charColon;
        strDate += twoDigit((int)date1.minute());
        strDate += charColon;
        strDate += twoDigit((int)date1.second());
 dataFile(strDate);

      File dataFile = SD.open("datalog.txt", FILE_Write); //open file 
      if (dataFile) { //file is availabel, write to it:
        if (boolStart) { //Write column header
          // "\n"=new line, "\t"=tab
          dataFile.println("\nDate\ttime\t\tPM2.5\tPM10\thum\ttemp");
          Serial.println("Date\ttime\t\tPM2.5\tPM10\thum\ttemp");

          boolStart=false;         
        }
        // make a string for assembling the data to log:
        if (nSamples==0) nSamples=-1; //prevent divide by zero if there is no data. Outputs negative value instead
        strLine = strDate;
        strLine += "\t";
        strLine += (float)Sum_PM25/(float)(10*nSamples);
        strLine += "\t";
        strLine += (float)Sum_PM10/(float)(10*nSamples);
        strLine += "\t";
        strLine += (dht.humidity);
        strLine += "\t";
        strLine += (dht.temperature_C);
        strLine += "\t";
//      strLine += nSamples;

        dataFile.println(strLine);


        dataFile.close();
        Serial.println(strLine); //print to the serial port too:
//      Serial.print("\t");
      } else  // file isn't open, pop up an error:
        Serial.println("error opening datalog.txt");         
      }

      nextLog=nextLog+loggerInt; //logger interval
      micro1 = micros(); //reset timer for Shinyei
      Sum_PM25=0UL;
      Sum_PM10=0UL;
      nSamples=0;
    } //endif date=nextLog
   } //endif micro2<micro1 (1 second logger-interval)

String twoDigit(int ii){
  // Converts an integer to a rght-justified two-character string lefft-padded with zeros, e.g. 1 => "01"
  // This function is used to output date/time, e.g. months, minutes, hours, minutes, seconds
  String string2=String(ii);
  if (ii<10){
    return String("0" + String2); //pad with leading zero
  } else {
    return string2;
  }
}

Vielen Dank für die Hilfe
Gruss Daniel

Setze den Sketch und die Fehlermeldung bitte in Code-Tags.

Verwende dazu die Schaltfläche </> oben links im Editorfenster.
Dazu den Sketch markieren und die Schaltfläche klicken.

Damit wird dieser für alle besser lesbar, auch mobile Geräte.

eigentlich brauchst nur konsequent alle Error Meldungen des Compilers abarbeiten.

das einfachste Beispiel

Datalogger_Feinstaub2:166:26: error: 'n' was not declared in this scope

         if (nSamples==0) n Samples=-1; //prevent divide by zero if there is no data. Outputs negative value instead

n not declared.
hm.
ja n hast nicht deklariert. willst vermutlich aber gar nicht.
schau'ma' uns das an

if (nSamples==0) n Samples=-1; //prevent divide by zero if there is no data. Outputs negative value instead

Annahme, du wolltest eigentlich nSamples schreiben

usw.

Hallo

Vielen Dank, das mit dem n Sample habe ich wirklich übersehen.

Bei der Abarbeitung der Fehlermeldungen habe ich etwas Probleme. Und zwar verstehe ich das Problem bei dieser Fehlermeldung nicht:

Datalogger_Feinstaub2:143:9: error: expected initializer before 'strDate'

        strDate = String((int)date1.year());

Was muss ich den hier machen?

Gruss Daniel

In der if-Abfrage zuvor ist dein Fehler.

    if (nextLog <= thisLog) {  void twoDigits (int sec, int min, int hrs, String title)
    
        strDate = String((int)date1.year());

na was wolltest du mit den twoDigits wirklich machen?

Drück auch mal in deiner IDE STRG-T , das bringt etwas Ordnung in die Einrückungen.