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.
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);