Wie Temperaturwerte Im Datenlogger ändern?

Hallo,

ich habe mir einen Datenlogger gebastelt mit welchen ich einen Temperatursensor (DHT11) auslese und die Temperatur logge. Leider wird die Temperatur mit ein einem Punkt statt einem Komma gespeichert. Zum Beispiel 22.10 statt 22,10 Celsius.
Ich hatte in die DHT Bibliothek geguckt ob man es dort ändern kann aber leider nicht. Auch geht es wohl in meinem Programm nicht.

Hat jemand eine Idee wie ich dies ändern könnte?

#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <DHT.h> // for the DHT sensor

#include <RTClib.h> // for the RTC

#define DHTPIN 7     
#define DHTTYPE DHT11    

DHT dht(DHTPIN, DHTTYPE);

const int chipSelect = 5; 

File myFile;


RTC_DS1307 rtc;

void setup() {

  dht.begin();
  Serial.begin(9600);
  
  while(!Serial); 
    if(!rtc.begin()) {
      Serial.println("Couldn't find RTC");
      while (1);
    }
    else {
      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }
    if(! rtc.isrunning()) {
      Serial.println("RTC is NOT running!");
    }
    
  Serial.print("Initializing SD card...");

  if(!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
    
  myFile=SD.open("DATA.txt", FILE_WRITE);

  if (myFile) {
    Serial.println("File opened ok");

    myFile.println("Date,Time,Temperature ºC");
  }
  myFile.close();
}

void loggingTime() {
  DateTime now = rtc.now();
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print("|");
  }
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  myFile.close();
  delay(1000);  
}

void loggingTemperature() {

  float t = dht.readTemperature();

  if  (isnan(t) ) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" °C");

  
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("open with success");
    myFile.print(t);
    myFile.println("");
  }
  myFile.close();
}

void loop() {
  loggingTime();
  loggingTemperature();
  delay(10000);
}

Wenn du die datei als csv Datei in excel (oder openoffice oder libreoffice) importierst, kannst du das meines Wissens angeben.

Oder du siehst ein, dass 0.1K Auflösung sowieso nur Spaß ist, und lässt es weg.

ALT:

neu:

  if (myFile) {
    Serial.println("open with success");
    myFile.print(t/10);
    myFile.print(',');
    myFile(print(t-t/10));
    myFile.println("");

Vielleicht...

Gibt 0. Schief geschaut. Sorry.
Außerdem myFile( ?

Gruß Tommy

@Tommy56
Hat in arduino (c++) - (Minus) Vorrang vor / (: Geteilt durch)?. Ich dachte es wäre Punkt vor Strich in der Priorität?

Du hast völlig Recht. Mein Fehler.

Gruß Tommy

Bei positivem t geht wohl

Serial.print((int)t);
Serial.write(',');
Serial.println((int)t*10);

Da die Dezimalstelle nur zum Spaß angezeigt wird, tut es nicht weh, dass sie nicht gerundet sondern abgeschnitten wird.
Was bei negativen Temperaturen passiert, habe ich nicht geprüft.

Hallo,
wenn man mehrere Werte in einer Zeile unterbringen muss kann sie auch in ein String Objekt packen

buf += temp;
buf += "; ";
buf += hum;
buf += "; ";
buf += pres;
buf += "; ";
buf += VCC;
buf.replace(".", ",");

und den Punkt mit replace() tauschen

Heinz

Zu meiner Rettung...
Ich hab das gestern komplett falsch verstanden - und eigentlich war das nur beim zuklappen hingeschmissen worden.
Ich würde das nach diesem Muster bauen:

  float t = -16.1;
  Serial.println("open with success");
  myFile.print(int(t));
  myFile.print(',');
  myFile.print(abs(int(t * 10 - int(t) * 10)));}

Nimmt dann auch die negativen Werte mit und gut ist.

Wenn die Kommazahlen wirklich etwas Wert sein sollen, dann musst du einen Sensor nehmen der wenigstens auf 0,1 Grad Celsius genau ist.
Ein DHT-Sensor ist da nur ein Schätzeisen.

Hast du mal zwei DHT-Sensoren die direkt nebeneinander angeordnet sind ausgelesen?

Um Temperaturdrift zu beobachten mag das ja noch gehen mit der Kommastelle aber als Absolutwerte ist die Nachkommastelle einfach falsch.
Du müsstest schon einen TMP117 nehmen

vgs

Hallo,

formatiere dir eine immer gleichlange Zeichenkette mit dtostrf (Input, Gesamtstellen inkl. Komma, Nachkommastellen, Output) zusammen. Danach ersetzt du den Punkt durch ein Komma.

void ersetzeZeichenInCharArray (char *zeichenkette, const char search, const char replace)
{
  size_t i {0};
  while ( zeichenkette[i] != '\0')  {
    if ( zeichenkette[i] == search ) {  // suche nach Zeichen 'search' und
      zeichenkette[i] = replace;        // ersetze es durch 'replace'
    }
    i++;
  }
}
ersetzeZeichenInCharArray(data, '.', ',');

Die fertige Zeichenkette schreibst du auf die SD Karte.

1 Like

Vielen Dank fpr die Tipss. Manche Programme haben nicht so gut funktioniert. Aber habe nun eine Lösung gefunden. Die da wäre:

  Serial.println("open with success");
  myFile.print(int(t));
  myFile.write(',');
  myFile.print(abs(int(t * 10 - int(t) * 10)));
  myFile.println("");    
  }
  myFile.close();

Allerdings ferstehe ich die Formel nicht, wenn man Punkt vor STrich beachtet dann müsste doch dabei immer Null herauskommen.

  myFile.print(abs(int(t * 10 - int(t) * 10)));

Wie kommst Du da drauf?
Gerade weil * vor - gerechnet wird:

float t = -10.5;


void setup()
{
  Serial.begin(115200);
  Serial.println(F("\r\nStart...\r\n"));
  while (t < 11)
  {
    Serial.print(int(t));                           //  gibt den ganzzahligen Teil vor dem Komma aus
    Serial.print(',');                              // Das Komma selbst
 // Die Zahl wird mit 10 multipliziert t*10           => 10,5 * 10=105
 // Dann wird der ganzzahlige Teil mit 10 multipliziert  => 10*10=100
 // Der wird abgeziogen vom ersten Ergebnis => 105-100=5
 // und das Vorzeichen immer positiv gesetzt (abs())
 // und ausgegeben

    Serial.println(abs(int(t * 10 - int(t) * 10)));
    t += 0.1;
  }
}

void  loop()

{}
Ausgabe
22:34:02.976 -> Start...
22:34:02.976 -> 
22:34:02.976 -> -10,5
22:34:02.976 -> -10,4
22:34:02.976 -> -10,2
22:34:02.976 -> -10,1
22:34:02.976 -> -10,0
22:34:02.976 -> -9,9
22:34:02.976 -> -9,8
22:34:02.976 -> -9,7
22:34:02.976 -> -9,6
22:34:02.976 -> -9,5
22:34:02.976 -> -9,4
22:34:02.976 -> -9,3
22:34:02.976 -> -9,2
22:34:02.976 -> -9,1
22:34:02.976 -> -9,0
22:34:02.976 -> -8,9
22:34:02.976 -> -8,8
22:34:02.976 -> -8,7
22:34:02.976 -> -8,6
22:34:02.976 -> -8,5
22:34:02.976 -> -8,4
22:34:02.976 -> -8,3
22:34:02.976 -> -8,2
22:34:02.976 -> -8,1
22:34:02.976 -> -8,0
22:34:02.976 -> -7,9
22:34:02.976 -> -7,8
22:34:02.976 -> -7,7
22:34:02.976 -> -7,6
22:34:02.976 -> -7,5
22:34:02.976 -> -7,4
22:34:02.976 -> -7,3
22:34:02.976 -> -7,2
22:34:02.976 -> -7,1
22:34:02.976 -> -7,0
22:34:02.976 -> -6,9
22:34:02.976 -> -6,8
22:34:02.976 -> -6,7
22:34:03.010 -> -6,6
22:34:03.010 -> -6,5
22:34:03.010 -> -6,4
22:34:03.010 -> -6,3
22:34:03.010 -> -6,2
22:34:03.010 -> -6,1
22:34:03.010 -> -6,0
22:34:03.010 -> -5,9
22:34:03.010 -> -5,8
22:34:03.010 -> -5,7
22:34:03.010 -> -5,6
22:34:03.010 -> -5,5
22:34:03.010 -> -5,4
22:34:03.010 -> -5,3
22:34:03.010 -> -5,2
22:34:03.010 -> -5,1
22:34:03.010 -> -5,0
22:34:03.010 -> -4,9
22:34:03.010 -> -4,8
22:34:03.010 -> -4,7
22:34:03.010 -> -4,6
22:34:03.010 -> -4,5
22:34:03.010 -> -4,4
22:34:03.010 -> -4,3
22:34:03.010 -> -4,2
22:34:03.010 -> -4,1
22:34:03.010 -> -4,0
22:34:03.010 -> -3,9
22:34:03.010 -> -3,8
22:34:03.010 -> -3,7
22:34:03.010 -> -3,6
22:34:03.010 -> -3,5
22:34:03.010 -> -3,4
22:34:03.010 -> -3,3
22:34:03.010 -> -3,2
22:34:03.010 -> -3,1
22:34:03.010 -> -3,0
22:34:03.010 -> -2,9
22:34:03.010 -> -2,8
22:34:03.010 -> -2,7
22:34:03.010 -> -2,6
22:34:03.010 -> -2,5
22:34:03.010 -> -2,4
22:34:03.010 -> -2,3
22:34:03.010 -> -2,2
22:34:03.010 -> -2,1
22:34:03.010 -> -2,0
22:34:03.010 -> -1,9
22:34:03.010 -> -1,8
22:34:03.010 -> -1,7
22:34:03.010 -> -1,6
22:34:03.010 -> -1,5
22:34:03.010 -> -1,4
22:34:03.010 -> -1,3
22:34:03.010 -> -1,2
22:34:03.010 -> -1,1
22:34:03.010 -> -1,0
22:34:03.010 -> 0,9
22:34:03.010 -> 0,8
22:34:03.010 -> 0,7
22:34:03.010 -> 0,6
22:34:03.010 -> 0,5
22:34:03.010 -> 0,4
22:34:03.010 -> 0,3
22:34:03.010 -> 0,2
22:34:03.010 -> 0,1
22:34:03.010 -> 0,0
22:34:03.010 -> 0,0
22:34:03.010 -> 0,1
22:34:03.010 -> 0,2
22:34:03.010 -> 0,3
22:34:03.010 -> 0,4
22:34:03.010 -> 0,5
22:34:03.010 -> 0,6
22:34:03.010 -> 0,7
22:34:03.042 -> 0,8
22:34:03.042 -> 0,9
22:34:03.042 -> 1,0
22:34:03.042 -> 1,1
22:34:03.042 -> 1,2
22:34:03.042 -> 1,3
22:34:03.042 -> 1,4
22:34:03.042 -> 1,5
22:34:03.042 -> 1,6
22:34:03.042 -> 1,7
22:34:03.042 -> 1,8
22:34:03.042 -> 1,9
22:34:03.042 -> 2,0
22:34:03.042 -> 2,1
22:34:03.042 -> 2,2
22:34:03.042 -> 2,3
22:34:03.042 -> 2,4
22:34:03.042 -> 2,5
22:34:03.042 -> 2,6
22:34:03.042 -> 2,7
22:34:03.042 -> 2,8
22:34:03.042 -> 2,9
22:34:03.042 -> 3,0
22:34:03.042 -> 3,1
22:34:03.042 -> 3,2
22:34:03.042 -> 3,3
22:34:03.042 -> 3,4
22:34:03.042 -> 3,5
22:34:03.042 -> 3,6
22:34:03.042 -> 3,7
22:34:03.042 -> 3,8
22:34:03.042 -> 3,9
22:34:03.042 -> 4,0
22:34:03.042 -> 4,1
22:34:03.042 -> 4,2
22:34:03.042 -> 4,3
22:34:03.042 -> 4,4
22:34:03.042 -> 4,5
22:34:03.042 -> 4,6
22:34:03.042 -> 4,7
22:34:03.042 -> 4,8
22:34:03.042 -> 4,9
22:34:03.042 -> 5,0
22:34:03.042 -> 5,1
22:34:03.042 -> 5,2
22:34:03.042 -> 5,3
22:34:03.042 -> 5,4
22:34:03.042 -> 5,5
22:34:03.042 -> 5,6
22:34:03.042 -> 5,7
22:34:03.042 -> 5,8
22:34:03.042 -> 5,9
22:34:03.042 -> 6,0
22:34:03.042 -> 6,1
22:34:03.042 -> 6,2
22:34:03.042 -> 6,3
22:34:03.042 -> 6,4
22:34:03.042 -> 6,5
22:34:03.042 -> 6,6
22:34:03.042 -> 6,7
22:34:03.042 -> 6,8
22:34:03.042 -> 6,9
22:34:03.042 -> 6,9
22:34:03.042 -> 7,0
22:34:03.042 -> 7,1
22:34:03.042 -> 7,2
22:34:03.042 -> 7,3
22:34:03.042 -> 7,4
22:34:03.042 -> 7,5
22:34:03.042 -> 7,6
22:34:03.042 -> 7,7
22:34:03.042 -> 7,8
22:34:03.042 -> 7,9
22:34:03.042 -> 8,0
22:34:03.042 -> 8,1
22:34:03.042 -> 8,2
22:34:03.042 -> 8,4
22:34:03.076 -> 8,5
22:34:03.076 -> 8,6
22:34:03.076 -> 8,7
22:34:03.076 -> 8,8
22:34:03.076 -> 8,9
22:34:03.076 -> 9,0
22:34:03.076 -> 9,1
22:34:03.076 -> 9,2
22:34:03.076 -> 9,3
22:34:03.076 -> 9,4
22:34:03.076 -> 9,5
22:34:03.076 -> 9,6
22:34:03.076 -> 9,7
22:34:03.076 -> 9,8
22:34:03.076 -> 9,9
22:34:03.076 -> 10,0
22:34:03.076 -> 10,1
22:34:03.076 -> 10,2
22:34:03.076 -> 10,3
22:34:03.076 -> 10,4
22:34:03.076 -> 10,5
22:34:03.076 -> 10,6
22:34:03.076 -> 10,7
22:34:03.076 -> 10,8
22:34:03.076 -> 10,9
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.