Save the temperature sensor data on the data logger shield

Hi, everybody. I intended to save the temperature sensor data on the data logger shield by this code that I attached,

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"


#define ONE_WIRE_BUS_1 2   



const int chipSelect = 10; //cs or the save select pin from the sd shield is connected to 10.
RTC_DS1307 RTC;
float celsius, fahrenheit;

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire_in(ONE_WIRE_BUS_1);

 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensor_inhouse(&oneWire_in);





File dataFile;
DateTime now;

// Addresses of 3 DS18B20s
uint8_t sensor1[8] = {0x28, 0xFF, 0x64, 0x1E, 0x14, 0x61, 0xBA, 0x6D };
uint8_t sensor2[8] = { 0x28, 0x0A, 0x1E, 0x79, 0xA2, 0x00, 0x03, 0x6F};
uint8_t sensor3[8] = { 0x28, 0xFF, 0x64, 0x1E, 0x14, 0x63, 0xD1, 0xB9};



void setup(void) {
  Serial.begin(9600);
  //setup clock
  Wire.begin();
  RTC.begin();
sensor_inhouse.begin();

//check or the Real Time Clock is on
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // uncomment it & upload to set the time, date and start run the RTC!
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
//setup SD card
   Serial.print("Initializing SD card...");

  // see if the SD 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.");

  //write down the date (year / month / day         prints only the start, so if the logger runs for sevenal days you only findt the start back at the begin.
    now = RTC.now();
    dataFile = SD.open("datalog.txt", FILE_WRITE);
    dataFile.print("Start logging on: ");
    dataFile.print(now.year(),DEC);
    dataFile.print('/');
    dataFile.print(now.month(),DEC);
    dataFile.print('/');
    dataFile.print(now.day(),DEC);
    dataFile.println(" ");
    dataFile.println("T    ");



    dataFile.close();
}

void loop(void) {
  
Serial.print("Requesting temperatures...");

    sensor_inhouse.requestTemperatures();
   
    Serial.print("Sensor 1: ");
  printTemperature(sensor1);
   
  Serial.print("Sensor 2: ");
  printTemperature(sensor2);
   
  Serial.print("Sensor 3: ");
  printTemperature(sensor3);



  Serial.println(" done");

    Serial.println();


  //open file to log data in.
   

  // if the file is available, write to it:
  // log the temperature and time.
  
}


void printTemperature(DeviceAddress deviceAddress)
{
  float tempC1=sensor_inhouse.getTempC(deviceAddress);

    Serial.print(tempC1);
 


  Serial.print("C");
  Serial.println();

  dataFile = SD.open("datalog.txt", FILE_WRITE);
  
  if (dataFile) {
   
    dataFile.print(tempC1);
    dataFile.print("                 ");
 





  now = RTC.now();
  
    
    dataFile.print(now.hour(),DEC);
    dataFile.print(":");
    dataFile.print(now.minute(),DEC);
    dataFile.print(":");
    dataFile.println(now.second(),DEC);
   
    dataFile.close();
    // print to the serial port too:
    Serial.println("data stored");
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
  //delay(60000); // this will log the temperature every minute.
  delay(3000);
}
 
  
  
  

  
 
  

I read temperatures by their address, and for some reason, I used one bus to read all of them.
As you can see in the code, I use " dataFile.print(tempC1); " to save the data on micro sd.
The problem with my code is I can't verify the temperatures because it saved all the temperatures, as you can see in the picture.
Capture

Is there any way that I can save temperatures separately, for example, like this:

sensor1=25
sensor2=26
sensor3=27

if it is possible, how should I change this line that I used:
dataFile.print(tempC1);

like here

    toFile.concat( "Correlation: " + String(lr.Correlation(), 6) + " Values: Y=" + String(values[0], 6) + " and *X + " + String(values[0], 6)  );
    toFile.concat( " Temperature:" + String(temperature) + "\n" );

I create a String of data and save the String appendFile(SD, "/data.txt", toFile.c_str() + '\n');

The data is saved as lines of data

thanks for your reply.
sorry, I couldn't completely understand where I should use the lines that you sent, should I change my code completely?

I was just trying to show how I save a series of data points to a single line. I doubt the code will work as posted. I was hoping you'd be able to understand what was done and adapt what was done to your code. I'm not giving you a final answer.

1 Like

such as what if you TRIED something like;

String boink = "words of things, " + tempC1 " words of things " + tempC2;
dataFile.print(boink + "\n" );

The most common method is to simply print commas between them and a println for the last one. This is called a CSV file. Printing the names of the sensors may depend on what you really intend to do with the data, but is probably a waste of time and resources.

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