Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: 1 [2] 3 4 ... 6   Go Down
Author Topic: [SOLVED]Logging multiple temp sensors questions(step by step)  (Read 6623 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Stuffing all the data into a string and then writing the string to the file is no faster than just writing the data one piece at a time to the file. The only advantage to using sprintf() is that it allows formatted output of the data, but that requires something between the % and the type. Since you aren't putting anything there, the formatting will be exactly the same as the print() and println() methods of the File class.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

log now seems :
2012-12-03,17:58:32,26,26;
2012-12-03,17:59:02,26,26;
2012-12-03,17:59:07,26,26;
2012-12-03,17:59:12,26,26;
2012-12-03,17:59:17,26,26;
2012-12-03,17:59:23,26,26;
2012-12-03,17:59:28,26,26;
2012-12-03,17:59:33,26,26;
2012-12-03,17:59:38,26,26;
2012-12-03,17:59:43,26,26;
2012-12-03,17:59:48,26,26;

I'm feeling better:)  Can I define the temp values to store it as 26,5 instead of 26  ?

on serial monitor it is ok, but on Sd is not.

serial looks like:

Requesting temperatures...DONE
Temperatures are 26.75 Celsius, 26.00 Celsius,

18:02:48 2012-12-03
Log ok
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Paul,

a bit later maybe it will be useful ( sprintf) when I have to upload the datas to the web ( like thingspeak, etc...). But at this point I want to be sure, everything is working fine. But maybe you are right!
Logged

Miramar Beach, Florida
Offline Offline
Tesla Member
***
Karma: 159
Posts: 6316
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS: That is correct, but you should recognize what is about to happen. The OP will want to put this in a log type format, so I expect that sprintf to change to this shortly:
Code:
sprintf(pageAdd,"%s at %s temp0=%i temp1=%i",sdate,stime,temp0,temp1);

The int type does not support a fractional part. Only whole numbers. Does the temperature sensor support a fractional temperature? If so, I can probably help you convert that.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

aah ok Tim. Not I leave it as is. SD log is just running...If that is right, I would like to put the ethernet stuff in... smiley-red
Logged

Miramar Beach, Florida
Offline Offline
Tesla Member
***
Karma: 159
Posts: 6316
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you integrate the ethernet module, insure you use sprintf with that. Every call to these functions creates another packet.
client.print()
client.println()
client.write()

You want as much in each packet as you can get in there within reason.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Gentlemen,

now it is running fine with 3 sensors. Log is beautiful! No empty lines, no other problems. smiley-lol
next step should be to put this to for example thingspeak... No I'm thinking on how to start this...
first: just added the libraries ethernet and spi... and it is still corretcly running.
Logged

Miramar Beach, Florida
Offline Offline
Tesla Member
***
Karma: 159
Posts: 6316
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now you should try initializing the ethernet and insure every thing still runs ok.
Post your current setup() function if you want help setting it up.

Warning: If the SD and w5100 are not initialized correctly in setup(), they can interfere with the other device begin() function, causing occasional startup failures that are difficult to troubleshoot.
« Last Edit: December 03, 2012, 01:58:57 pm by SurferTim » Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great, thanks Tim! I appretiate your help!

Full code is:

Code:
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Rtc_Pcf8563.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.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, Thermometer3 ;

//init the real time clock
Rtc_Pcf8563 rtc;

//variables

int temp0;
int temp1;
int temp2;
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"));
  if (!oneWire.search(Thermometer3)) 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();
  
  Serial.print(F("Device 1 Address: "));
  printAddress(Thermometer3);
  Serial.println();

  // set the resolution to 9 bit
  sensors.setResolution(Thermometer1, TEMPERATURE_PRECISION);
  sensors.setResolution(Thermometer2, TEMPERATURE_PRECISION);
  sensors.setResolution(Thermometer3, 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();
  Serial.print(F("Device 1 Resolution: "));
  Serial.print(sensors.getResolution(Thermometer3), 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(sensors.getTempC(Thermometer3));
  Serial.print(F(" Celsius, "));
  Serial.print(F("\n\r"));

  temp0=(sensors.getTempC(Thermometer1));
  temp1=(sensors.getTempC(Thermometer2));
  temp2=(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,temp2)) Serial.println(F("Log failed"));
  else Serial.println(F("Log ok"));

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

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

importants from the old one:

Code:
sprintf(pageAdd,"/update?key=XXXXXXXXXXXXXXXXXX&field1=%u&field2=%u&field3=%u",temp0,temp1,temp2);

    if(!getPage(server,pageAdd)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
   
    if(!logFile(temp0,temp1,temp2)) Serial.println(F("Log failed"));
    else Serial.println(F("Log ok"));
Code:
byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,80))
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  }
  else
  {
    Serial.println(F("failed"));
    return 0;
  }
« Last Edit: December 03, 2012, 02:08:47 pm by vespapierre » Logged

Miramar Beach, Florida
Offline Offline
Tesla Member
***
Karma: 159
Posts: 6316
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Put this in your variables section
Code:
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

Then change your setup function. It disables the w5100 SPI while setting up your SD.
Code:
void setup(void)
{
  // start serial port
  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

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

  // Start w5100
  Serial.print(F("Starting w5100..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.println(F("ok "));

  // this corrects a bug in the Ethernet.begin() function
  digitalWrite(10,HIGH);

  Serial.print(F("IP: "));
  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));

  // rest of your setup code
Does everything still run ok with this?

edit: I forgot a closing parenthesis and a couple line feeds.
« Last Edit: December 03, 2012, 02:20:23 pm by SurferTim » Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

first attempt is not ok...
serial:
Code:
Starting SD...ok Starting w5100...ok IP: 192.168.9.109
Ready
Locating devices...Found 3 devices.
Parasite power is: OFF
Device 0 Address: 1092942902080056
Device 1 Address: 286C81C5030000FA
Device 1 Address: 2811E8E803000027
Device 0 Resolution: 9
Device 1 Resolution: 9
Device 1 Resolution: 9
Requesting temperatures...DONE
Temperatures are 16.75 Celsius, 8.50 Celsius, 6.00 Celsius,

20:27:04 2012-12-03
Open fail
Log failed
Requesting temperatures...DONE
Temperatures are 16.50 Celsius, 8.50 Celsius, 6.00 Celsius,

20:27:10 2012-12-03
Log ok
Requesting temperatures...DONE
Temperatures are 16.31 Celsius, 8.00 Celsius, 6.00 Celsius,

20:27:16 2012-12-03
Log ok
logfile seems ok:

Code:
2012-12-03,20:23:33,25,24,24;
2012-12-03,20:27:10,16,8,8;
2012-12-03,20:27:16,16,8,8;
2012-12-03,20:27:22,16,8,8;
2012-12-03,20:27:27,15,8,8;
2012-12-03,20:27:33,15,7,7;
2012-12-03,20:27:39,15,7,7;
2012-12-03,20:27:45,15,7,7;
2012-12-03,20:27:51,15,7,7;
2012-12-03,20:27:57,14,7,7;
2012-12-03,20:28:03,14,7,7;
2012-12-03,20:28:08,14,7,7;
« Last Edit: December 03, 2012, 02:28:25 pm by vespapierre » Logged

Miramar Beach, Florida
Offline Offline
Tesla Member
***
Karma: 159
Posts: 6316
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Normally if I power down the Arduino for a few seconds, then power it up, that will stop the fail.

Sometimes when I run someone else's test code, I will not notice they didn't disable one while setting up the other. Then my old code will fail until I power cycle it.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, so we can say, it is still working. where should I add the rest of the code? just to be sure! we are almost reached the finish line smiley
After or before logfile section should I add the getpage stuff  in the loop?
« Last Edit: December 03, 2012, 02:36:05 pm by vespapierre » Logged

Miramar Beach, Florida
Offline Offline
Tesla Member
***
Karma: 159
Posts: 6316
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Add the getPage function to your sketch. I would call it after the SD log write because I know the SD part works now. That is just me. It is really up to you which is more important to you.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok. full code before testing is:

Code:
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Rtc_Pcf8563.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.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, Thermometer3 ;

//init the real time clock
Rtc_Pcf8563 rtc;

//variables

int temp0;
int temp1;
int temp2;
char pageAdd[140];
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// change to your server
IPAddress server(184,106,153,149);
EthernetClient client;

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

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

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

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

  // Start w5100
  Serial.print(F("Starting wi5100..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.print(F("ok "));

  // this corrects a bug in the Ethernet.begin() function
  digitalWrite(10,HIGH);

  Serial.print(F("IP: "));
  Serial.println(Ethernet.localIP());

  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"));
  if (!oneWire.search(Thermometer3)) 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();
  
  Serial.print(F("Device 1 Address: "));
  printAddress(Thermometer3);
  Serial.println();

  // set the resolution to 9 bit
  sensors.setResolution(Thermometer1, TEMPERATURE_PRECISION);
  sensors.setResolution(Thermometer2, TEMPERATURE_PRECISION);
  sensors.setResolution(Thermometer3, 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();
  Serial.print(F("Device 1 Resolution: "));
  Serial.print(sensors.getResolution(Thermometer3), 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(sensors.getTempC(Thermometer3));
  Serial.print(F(" Celsius, "));
  Serial.print(F("\n\r"));

  temp0=(sensors.getTempC(Thermometer1));
  temp1=(sensors.getTempC(Thermometer2));
  temp2=(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,temp2)) Serial.println(F("Log failed"));
  else Serial.println(F("Log ok"));
  
  sprintf(pageAdd,"/update?key=XXXXXXXXXXXXXXXXX&field1=%u&field2=%u&field3=%u",temp0,temp1,temp2);

  if(!getPage(server,pageAdd)) Serial.print(F("Fail "));
  else Serial.print(F("Pass "));
  
}
int logFile(char* sdate, char* stime, int temp0, int temp1, int temp2) {
  File fh = SD.open("test.txt",FILE_WRITE);
  if(!fh) {
    Serial.println(F("Open fail"));
    return 0;
  }

  sprintf(pageAdd,"%s,%s,%i,%i,%i;",sdate,stime,temp0,temp1,temp2);
  fh.println(pageAdd);
  fh.close();
  return 1;
}
byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,80))
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  }
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}

Is that ok if I use the same pageadd in case of getpage and logfile?
« Last Edit: December 03, 2012, 02:51:53 pm by vespapierre » Logged

Pages: 1 [2] 3 4 ... 6   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: