Go Down

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

SurferTim

Back to basics. This does just the SD and ethernet, and works good on my Mega. I used my server with a php page, and pin 4 for my SD card. I sent the same parameters as below.
Code: [Select]
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

//variables

int temp0;
int temp1;
int temp2;
char pageAdd[64];

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

}


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

void loop(void)
{
  delay(5000);

  temp0 = 1;
  temp1 = 2;
  temp2 = 3;
 
  //datetime
  char sdate[11] = "2012-12-04";
  char stime[9] ="12:34:56";
  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;
}

This works, correct?

vespapierre

after deleting logfile, first two attempt was fail, than log ok on serial...logfile looks:

2012-12-04,12:34:56,1,2,3;

2012-12-04,12:34:56,1,2,3;


so this is not ok!

SurferTim

Here is mine using that code:
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;
2012-12-04,12:34:56,1,2,3;

vespapierre

#73
Dec 04, 2012, 10:06 pm Last Edit: Dec 04, 2012, 10:21 pm by vespapierre Reason: 1
ohh...bad news... means I have problems with the hw ?

edit: I'm using arduino 102....is it a problem?? or should I change the sd card? it is 128 mb.

SurferTim

I don't know about the smaller SD cards. I use a 2GB SanDisk, and it is working good.

I noticed the SD card adapter uses voltage divider resistors instead of a logic level converter. I hear that will cause problems too.

Go Up