I have tried another code:
#include <Wire.h>
#include <OneWire.h>
#include <Rtc_Pcf8563.h>
#include <SD.h>
#define loByte (byte)lowByte
#define hiByte (byte)highByte
#define ERROR_LED 4
#define ERROR_RTC_GET 2 // Unable to get RTC time and date
//init the real time clock
Rtc_Pcf8563 rtc;
OneWire ds(3); // Thermometer on pin 3
// Global variables
byte result;
const int period = 20000;
void SetError(int);
const int chipSelect = 8;
//char sWholetempbuf[5];
//char sFracttempbuf[3];
void setup()
{
char message[45]="";
pinMode(ERROR_LED, OUTPUT); // Set error LED
Serial.begin(9600);
Wire.begin();
pinMode(8, OUTPUT);
if (!SD.begin(chipSelect))
{
Serial.println(F("Card failed, or not present"));
// don't do anything more:
return;
}
Serial.println(F("Card initialized."));
digitalWrite(ERROR_LED, HIGH);
delay(150);
digitalWrite(ERROR_LED, LOW);
delay(150);
digitalWrite(ERROR_LED, HIGH);
delay(150);
digitalWrite(ERROR_LED, LOW);
File logFile = SD.open("errorlog.txt", FILE_WRITE);
if (logFile)
{
strcpy(message, rtc.formatDate(RTCC_DATE_ASIA));
strcat(message, " ");
strcat(message, rtc.formatTime());
logFile.println("Program start: ");
logFile.println(message);
logFile.close();
Serial.println(F("Program started: "));
Serial.println(message);
}
// if the file isn't open, pop up an error:
else
{
Serial.println(F("error opening errorlog.txt"));
}
}
void loop()
{
delay(period);
// make a string for assembling the data to log:
char dataString[30] = "";
// char sWhole[5] = "";
// char sFract[3] = "";
char sDate[11] = "";
char sTime[9] ="";
int SignBit, Tc_100, Whole, Fract;
int Temperature = getTemperature();
SignBit = Temperature & 0x8000; // test most sig bit
if (SignBit) // negative
{
Temperature = (Temperature ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (Temperature*100/2);
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
if (SignBit) // If its negative
{
Serial.print(F("-"));
}
Serial.print(Whole);
Serial.print(F("."));
if (Fract < 10)
{
Serial.print(F("0"));
}
Serial.print(Fract);
Serial.print(F("Celsius"));
Serial.print(F("\n"));
//-------------------------------------------------------------
// sWhole=String(Whole);
// sFract=String(Fract);
strcpy(sTime, rtc.formatTime()); //read formatted time from i2C RTC
strcpy(sDate, rtc.formatDate(RTCC_DATE_ASIA)); //read formatted date from 12C RTC
// strcpy(sWhole, Whole);
// strcpy(sFract, Fract);
Serial.print(sTime);
Serial.print(F(" "));
Serial.print(sDate);
Serial.println();
strcpy(dataString, itoa(Whole,sWholetempbuf,10));
strcat(dataString, ".");
strcat(dataString, itoa(Fract,sFracttempbuf,10));
strcat(dataString, ";");
strcat(dataString, sDate);
strcat(dataString, ";");
strcat(dataString, sTime);
strcat(dataString, ";");
//dataString += String(sWhole + "." + sFract + ";" + sDate + ";" + sTime + ";");
delay(100);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
digitalWrite(ERROR_LED, HIGH);
delay(500);
digitalWrite(ERROR_LED, LOW);
//delay(500);
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else
{
Serial.println(F("error opening datalog.txt"));
}
} // END LOOP
//**************************************************************************************
void SetError(int error) // Blinks forever the error led a number of times corresponding to error number
{
while(1) // Forever
{
for (byte index = 0; index < error; index++)
{
digitalWrite(ERROR_LED, HIGH);
delay(500);
digitalWrite(ERROR_LED, LOW);
delay(500);
}
delay(1000);
}
}
int getTemperature() //gets the actual Temperature from 1-wire interface
{
byte i;
byte present = 0;
byte data[12];
byte addr[8];
int HighByte, LowByte, TReading;
// if ( !ds.search(addr))
// {
//// Serial.print(F("No more addresses.\n"));
// ds.reset_search();
// delay(250);
// return 0;
// }
ds.search(addr);
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(900); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++)
{ // we need 9 bytes
data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
LowByte = data[0];
HighByte = data[1];
return TReading = (HighByte << 8) + LowByte;
}
Now with one ds18b20 sd writing is working fine. I would like to solve to put 2 or 3 more ds18b20 to onewire and put the values to the logfile.
Can you help me how to do this?
After can come the upload to the web...