Go Down

Topic: [SOLVED]Logging multiple temp sensors questions(step by step) (Read 6 times) previous topic - next topic

vespapierre

Paul,
yes it is producing correct output(at least as it seems on the serial monitor).degrees below zero is ok and different ds sensors are ok too to handle.
Nick,
Exactly I would use sd for only backup. In case if something wrong with the internet connection.It is good to have a backup.
Thanks for the advices!I know I will be back with the results of each step.

vespapierre

so now without the date+time, logging works ok! But I have some converting problems...
full code is here:
Code: [Select]
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Rtc_Pcf8563.h>
#include <Wire.h>

// Data wire is plugged into port 3 on the Arduino
#define ONE_WIRE_BUS 3
#define TEMPERATURE_PRECISION 9

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

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

// arrays to hold device addresses
DeviceAddress Thermometer1, Thermometer2 ;

//init the real time clock
Rtc_Pcf8563 rtc;

//variables

int temp0;
int temp1;
char pageAdd[140];

// **********************************SETUP*****************************************************
void setup(void)
{

  // start serial port
  Serial.begin(9600);

  //start SD
  Serial.print(F("Starting SD..."));
  if(!SD.begin(8)) Serial.println(F("failed"));
  else Serial.print(F("ok "));
  delay(2000);
  Serial.println(F("Ready"));

  // Start up the library
  sensors.begin();

  // locate devices on the bus
  Serial.print(F("Locating devices..."));
  Serial.print(F("Found "));
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(F(" devices."));

  // report parasite power requirements
  Serial.print(F("Parasite power is: "));
  if (sensors.isParasitePowerMode()) Serial.println(F("ON"));
  else Serial.println(F("OFF"));

  oneWire.reset_search();
  if (!oneWire.search(Thermometer1)) Serial.println(F("Unable to find address for Thermometer1"));
  if (!oneWire.search(Thermometer2)) Serial.println(F("Unable to find address for Thermometer2"));

  // show the addresses we found on the bus
  Serial.print(F("Device 0 Address: "));
  printAddress(Thermometer1);
  Serial.println();

  Serial.print(F("Device 1 Address: "));
  printAddress(Thermometer2);
  Serial.println();

  // set the resolution to 9 bit
  sensors.setResolution(Thermometer1, TEMPERATURE_PRECISION);
  sensors.setResolution(Thermometer2, TEMPERATURE_PRECISION);

  Serial.print(F("Device 0 Resolution: "));
  Serial.print(sensors.getResolution(Thermometer1), DEC);
  Serial.println();
  Serial.print(F("Device 1 Resolution: "));
  Serial.print(sensors.getResolution(Thermometer2), DEC);
  Serial.println();
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 16) Serial.print(F("0"));
    Serial.print(deviceAddress[i], HEX);
  }
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(F("Temp C: "));
  Serial.print(tempC);
}

// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
  Serial.print(F("Resolution: "));
  Serial.print(sensors.getResolution(deviceAddress));
  Serial.println();   
}

// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
  Serial.print(F("Device Address: "));
  printAddress(deviceAddress);
  Serial.print(F(" "));
  printTemperature(deviceAddress);
  Serial.println();
}

//********************************************LOOP**************************************

void loop(void)
{
  delay(5000);
  Serial.print(F("Requesting temperatures..."));
  sensors.requestTemperatures();
  Serial.println(F("DONE"));

  // print the device information
  //  printData(Thermometer1);
  //  printData(Thermometer2);

  Serial.print(F("Temperatures are "));
  Serial.print(sensors.getTempC(Thermometer1));
  Serial.print(F(" Celsius, "));
  Serial.print(sensors.getTempC(Thermometer2));
  Serial.print(F(" Celsius, "));
  Serial.print(F("\n\r"));

  temp0=(sensors.getTempC(Thermometer1));
  temp1=(sensors.getTempC(Thermometer2));

  //datetime
  char sdate[11] = "";
  char stime[9] ="";
  strcpy(stime, rtc.formatTime());
  strcpy(sdate, rtc.formatDate(RTCC_DATE_ASIA));
  Serial.print(stime);
  Serial.print(" ");
  Serial.print(sdate);
  Serial.println(); 
   
  if(!logFile(sdate,stime,temp0,temp1)) Serial.println(F("Log failed"));
  else Serial.println(F("Log ok"));

}
int logFile(int sdate, int stime, int temp0, int temp1) {
  File fh = SD.open("test.txt",FILE_WRITE);
  if(!fh) {
    Serial.println(F("Open fail"));
    return 0;
  }

  sprintf(pageAdd,"%u,%u,%u,%u;",sdate,stime,temp0,temp1);
  fh.println(pageAdd);
  fh.close();
  return 1;
}


Here I have problems with converting char* to int:
Code: [Select]
if(!logFile(sdate,stime,temp0,temp1)) Serial.println(F("Log failed"));
How should I fix this?

I would like to test the sd logging with this code...if that works fine, than I will add somehow the ethernet and uploading to thinsgpeak for example...


SurferTim

Do not use int type here. Leave the date and time as a character array. Pass them like this:
Code: [Select]
int logFile(char* sdate, char* stime, int temp0, int temp1) {
  File fh = SD.open("test.txt",FILE_WRITE);
  if(!fh) {
    Serial.println(F("Open fail"));
    return 0;
  }

  // note change to %s on first two
  sprintf(pageAdd,"%s,%s,%u,%u;",sdate,stime,temp0,temp1);
  fh.println(pageAdd);
  fh.close();
  return 1;
}


vespapierre

#13
Dec 03, 2012, 05:47 pm Last Edit: Dec 03, 2012, 05:51 pm by vespapierre Reason: 1
thanks Tim! seems it iw working...I'm just going to test!

I have changed to this:

Code: [Select]
sprintf(pageAdd,"%s,%s,%u,%u;",sdate,stime,temp0,temp1);

because I need negative values for temp0, temp1...is it ok?  

or is it possible to set it as float? like:

Code: [Select]
int logFile(char* sdate, char* stime, float temp0, float temp1)

SurferTim

Stay away from float when possible. If you want a signed integer for the temperatures, use this:
Code: [Select]
// note change from %u to %i
sprintf(pageAdd,"%s,%s,%i,%i;",sdate,stime,temp0,temp1);


I know this is C++ reference, but the format variable types are the same.
http://www.cplusplus.com/reference/cstdio/printf/

Go Up