Go Down

Topic: Writing to text file or to csv (Read 302 times) previous topic - next topic

Robertjee7

Oct 01, 2018, 12:00 am Last Edit: Oct 01, 2018, 12:38 pm by Robertjee7
I would like to make a program that reads temperature and humidity, using the DHT22 sensor, and that the program it then writes to a .csv or .txt file. Preferably a .csv file. But when I now make my program I get an error.

First, sensor can't read anything, but that happens almost every time.
Second time, it reads values and writes it to the file
Third time, program can't open the file.

But if I put the SD-card in my PC, then the file exists, but the file itself is empty. I can't really find anything on the internet to help this, so that's why I'm asking it here.

I attached my serial monitor output, hope you can help me here.


Code: [Select]
#include <SD.h>
#include <SPI.h>
#include <DHT.h>

#define DHTPIN 2 //sensor is on pin 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
const int chipSelect = 4;
const int led_red = 8;
const int led_yellow = 9;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    digitalWrite(9, HIGH); //while waiting, turn on yellow LED
    delay(500);
  }
  digitalWrite(9,LOW); // turn it off when done waiting
  Serial.println("DHT22 sensor reading");

  if (!SD.begin(chipSelect)) {
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH); //if SD can't be read, then turn on red and yellow light
 
}

}

void loop() {
  delay(1000);

  float hum = dht.readHumidity();
  float temp = dht.readTemperature(); //in Celcius. For Fahrenheit use the argument true
 
 
  if (isnan(hum) || isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // calculate Heat Index, gevoelstemperatuur
  float hi = dht.computeHeatIndex(temp,hum, false); //isFahrenheit = false

 
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.print(" *C ");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *C ");

  //String dataString = "";
  //dataString = hum + ", " + temp + ", " + hi;


  Serial.println("Opening file"); 
  File dataFile = SD.open("data.txt", FILE_WRITE);
 

  if(dataFile){
    dataFile.println(String(hum) + ", " + String(temp) + ", " + String(hi));
    dataFile.close();
    Serial.println("Written to file, and closed it");
    Serial.println(String(hum) + ", " + String(temp) + ", " + String(hi)); // for testing, print it to the serial port too
  }
  else {
    digitalWrite(8, HIGH); // turn on red led if the file can't be opened
    Serial.println("got an error with the file");
    //while(1);

   
  }

 

}



Edit: Some additional information.
I'm using the Arduino DUE with the Ethernet Shield W5100. That's where I put my SD card. I have formatted the card using the right tool.
Also, when I check the SD-card on my computer after running the program, the file is created but is empty. Something I thought was interesting is that the 'date modified' part always is 1-1-2000 00:00. I'm guessing because I haven't implemented any time-related things, but I thought it was worth mentioning.

Nick_Pyner

#1
Oct 01, 2018, 03:18 am Last Edit: Oct 01, 2018, 03:18 am by Nick_Pyner
Have you checked the first post in this forum and formatted your card in the proper manner?

Juraj

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Robertjee7

Have you checked the first post in this forum and formatted your card in the proper manner?
Yes I did. Did it again, but still doesn't work. I also added some additional information.

Juraj

#4
Oct 01, 2018, 12:58 pm Last Edit: Oct 01, 2018, 01:02 pm by Juraj
some versions of SD library create a new file with FILE_WRITE (and perhaps fail if the file exists). use the latest version to append to file opened with FILE_WRITE
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Nick_Pyner

I assume you are using the standard out-of-the-box SD library, in which case

1. you get that date because there is no facility to date the file, not because of any mistake you are making. If you want to date the file, the simplest recourse is to use date as filename.

2. Using the standard library, FILE_WRITE  appends to existing, or creates new if it doesn't, and has been that way for years. I'm not aware of any other library by that name.

Your code looks reasonably right, you are clouding the matter with LED junk. Commenting
while(1);
is a really good idea, only exceeded by deleting it. Try using the standard datalogger included in the IDE.

I'm not at all familiar with the Due, but I assume it is OK to use the W5100 with it, provided you are powering the combination in the proper manner, i.e. 9v wall wart. If you are doing something smarty-farty with batteries, you may be muddying the water, and have SD on the wrong voltage. (Just a guess...)

Juraj

#6
Oct 01, 2018, 05:51 pm Last Edit: Oct 01, 2018, 07:11 pm by Juraj
2. Using the standard library, FILE_WRITE  appends to existing, or creates new if it doesn't, and has been that way for years. I'm not aware of any other library by that name.


see https://github.com/arduino-libraries/SD/issues/45

version 1.2.0 has it wrong. published in Jan 2017.  1.2.1 with the fix was published in Nov 2017
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Robertjee7

I updated my code, now it looks like this:

Code: [Select]
#include <SD.h>
#include <SPI.h>
#include <DHT.h>

#define DHTPIN 2 //sensor is on pin 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
const int chipSelect = 4;


void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  Serial.println("DHT22 sensor reading");

/*
if(SD.begin(chipSelect)){
    Serial.println("card init");
  } else {
    Serial.println("card not init");
    while(1);
  } */

}

void loop() {
  delay(1000);

  float hum = dht.readHumidity();
  float temp = dht.readTemperature(); //in Celcius. For Fahrenheit use the argument true
 
 
  if (isnan(hum) || isnan(temp)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // calculate Heat Index, gevoelstemperatuur
  float hi = dht.computeHeatIndex(temp,hum, false); //isFahrenheit = false

 
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.print(" *C ");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *C ");


  if(SD.begin(chipSelect)){
    Serial.println("card init");
  } else {
    Serial.println("card not init");
    return;
  }



  Serial.println("Opening file"); 
  File dataFile = SD.open("datafile.csv", FILE_WRITE);
 

  if(dataFile){
    dataFile.print(hum);
    dataFile.print(",");
    dataFile.print(temp);
    dataFile.print(",");
    dataFile.println(hi);
    dataFile.close();
   
    Serial.println("Written to file, and closed it");
    Serial.println(" ");
  }
  else
  {
    Serial.println("got an error with the file");
    //dataFile.close();   
  }



} //loop end


Still can't write to file. It does say "card init", but then "got an error with the file". I followed the SD example, so I don't know why it does not want to create/open the file.

Nick_Pyner

This is probably no help but, unless you are worried somebody is going to steal the card, you only need to do the initialisation check in Setup - once. Similarly, once done, you don't need to make the existence conditional, again,  for the datafile prints.  I recognise that that is what Igoe has done, maybe it was some legacy from 2010. What would help is getting rid of all that insulting whitespace in the code to make it more readable. Having said that, the code looks reasonable, and I wonder if you have something else going on. Are the serial.prints OK?

Juraj

did you check the version of the SD library?
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Go Up