Go Down

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

SurferTim

Yes. It saves memory to reuse global arrays. Be careful to not call a routine that uses that array from a routine that also uses it. As long as you call one, then call the other, you are good to go!

Best way is to test it. How does it do?

vespapierre

first 3 attempt was with open fail log failed...and sendint to web is ok... ??

current code is:
Code: [Select]
#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=XXXXXXXXXXXXXXX&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;
}


logfile is not ok:
Code: [Select]
2012-12-03,21:06:30,8,4,4;

2012-12-03,21:06:42,8,4,4;

2012-12-03,21:06:54,8,4,4;
2012-12-03,21:07:00,8,4,4;

2012-12-03,21:07:12,8,4,4;

2012-12-03,21:07:47,8,4,4;

SurferTim

OK. Comment out the call to the getPage() function. How does the log look then? Now you are down to the final stuff.

Are you using a Mega or an Uno?


vespapierre

#33
Dec 03, 2012, 09:18 pm Last Edit: Dec 03, 2012, 09:26 pm by vespapierre Reason: 1
I'm commenting out:

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

//  if(!getPage(server,pageAdd)) Serial.print(F("Fail "));
//  else Serial.print(F("Pass "));


I'm using Mega

SurferTim

With a Mega, you should be ok on memory then. Does the log look ok now, or is it still malfunctioning?

vespapierre

after removing the lines what I have sent before, the first attempt is failed, and then ok.(second to sixth...)
and logfile ok too

SurferTim


after removing the lines what I have sent before, the first attempt is failed, and then ok.(second to sixth...)
and logfile ok too

So the SD log works as long as you don't call the getPage function? And getPage works all the time?

vespapierre

yes.

this is not commented out:
Code: [Select]
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;
}

SurferTim

OK, we are down to two main things. It is either the sprintf, or the call to getPage. Uncomment the sprintf statement, but leave the call to getPage commented out. Does the SD log do ok?

vespapierre

with that sprintf , the first log attempt is failed, than ok. logfile ok too! ( just getpage function is commented out)

SurferTim

OK. Uncomment the getPage function and try it with a very simple page, like Google home page.

vespapierre

#41
Dec 03, 2012, 09:54 pm Last Edit: Dec 03, 2012, 09:56 pm by vespapierre Reason: 1
you mean change the IP address to google.com 's IP and use getpage as the same? :

Code: [Select]
if(!getPage(server, pageAdd)) Serial.print(F("Fail "));
//  else Serial.print(F("Pass "));

SurferTim

Use Google's home page with "/" for a page. Comment out your sprintf, and use a simple one for a test. It is something right in there causing the fail.

vespapierre

sorry Tim, I don't understand ... can you send me the function?

new infos: if I put the getpage before logfile, log failed all the time...

vespapierre

#44
Dec 03, 2012, 10:22 pm Last Edit: Dec 03, 2012, 11:00 pm by vespapierre Reason: 1
changed the ip to google.com's ip
and function is:
Code: [Select]
sprintf(pageAdd, "/");

if(!getPage(server,pageAdd)) Serial.print(F("Fail "));
else Serial.print(F("Pass "));


return code from google is 302 and

logfile is:
2012-12-03,22:19:51,4,4,4;



2012-12-03,22:20:59,5,3,3;
2012-12-03,22:21:16,5,3,3;


I tried to change the size of the pageAdd from 140 to 139 and 141 with the original server....az API said max size can be 140...
what the hell can be the problem...I do not beleive that it cannot be solved...voaaaa....

there was no wire.begin() during the code...

Go Up