Mega with wiznet shield (wire hack)+thingspeak+sd problem

Of all the things I enjoy seeing in the serial monitor, this is my favorite:

Pass 1

I have defined the temp0, temp1 ... values as "int". How can put them to sprintf(...)? Manual send was : &field1=200&field2=200 ....
instead of 200 i have to put the temp0, temp1 ....

That sounds like this:

sprintf(pageAdd,"/update?key=MyKey&field1=%u&field2=%u",temp0,temp1);

edit: Insure the character array pageAdd has enough storage for the string.

I'm going to be happy :slight_smile:
current code is here:
SKETCH

answer is:
Starting ethernet...
192.168.9.110
Starting SD...ok Ready
Locating devices...Found 1 devices.
Parasite power is: OFF
Found device 0 with address: 1092942902080056
Getting temperatures...

Temperatures are 85 Celsius, 0 Celsius, 0 Celsius,

connecting...connected
HTTP/1.1 200 OK
Server: nginx/0.8.53
Date: Wed, 21 Nov 2012 19:36:14 GMT
Content-Type: text/html; charset=utf-8
Connection: close
Vary: Accept-Encoding
Status: 200
ETag: "d53697441ef12a45422f6660202f9840"
Cache-Control: max-age=0, private, must-revalidate

2818
disconnecting.
Pass 1

85 degrees is maybe some kind of delay I guess...
we are almost at the start of writing to SD :slight_smile:

edit: I have modified the char array to 140. As API documentation said that is the maximum. Can I modify later the temp as float?

Watch out for type float. If you try to use that type in sprintf, the result will be "?" where you expected the value to be. You need to convert that float to a string first.

Is the value returned from the temperature sensor a float type? Or integer?

edit: Some devices return the temperature in fractions of a degree, like half or quarter of a degree. 85 may be in quarter degrees. That would be 21.25 degrees Celsius.

I have tried that, I will stay at integer ( i gues return value can be a float)...I hope it will show negative values.
Facing to SD writing...I have put the SD code what you wrote to the setup().
Can you help how should I go ahaed?

it was my original SD code:

#include <Wire.h>
#include <OneWire.h>
#include <Rtc_Pcf8563.h>
#include <SD.h>

//init the real time clock
Rtc_Pcf8563 rtc;

// Global variables
const int period = 30000;
const int chipSelect = 8;

//**************************SETUP*****************************
void setup()
{
  String message="";
  Serial.begin(9600);
  Wire.begin();
  pinMode(8, OUTPUT);
   if (!SD.begin(chipSelect)) 
    {
    Serial.println("Card failed");
    return;
    }
  Serial.println("Card init");
  File logFile = SD.open("errorlog.txt", FILE_WRITE);
  if (logFile) 
  { 
    message = String(rtc.formatDate(RTCC_DATE_ASIA)) + " " + String(rtc.formatTime());
    logFile.println("Progstart: " + message);
    logFile.close();
    Serial.println("Progrstart: "  + message);
  }  
// if the file isn't open, pop up an error:
  else 
  {
    Serial.println("cannot open errorlog.txt");
  } 
}

//********************LOOP**********************************
void loop()
{
  delay(period);  
// make a string for assembling the data to log:
  String dataString = "";
  String sDate = "";
  String sTime ="";
//-------------------------------------------------------------  
    sTime=rtc.formatTime();               //read formatted time from i2C RTC
    sDate=rtc.formatDate(RTCC_DATE_ASIA); //read formatted date from 12C RTC

  Serial.print(sTime);
  Serial.print(" ");
  Serial.print(sDate);
  Serial.println();
   dataString += String(sDate + ";" + sTime + ";");
    delay(100);
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) 
  {
    dataFile.println(dataString);
    dataFile.close();
// print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else 
  {
    Serial.println("cannot open datalog.txt");
  } 
}
// END LOOP

or should I use your ftp code?? Seems I will need your further help :blush:

Did you see my edit to my last post? The value returned may be in 1/4 degrees Celsius.
85 / 4 = 21.25 degrees.
Is that about right?

oh yes...I will double check the DS18b20...now I'm excited about the SD :slight_smile:

SurferTim:
Did you see my edit to my last post? The value returned may be in 1/4 degrees Celsius.
85 / 4 = 21.25 degrees.
Is that about right?

85 is the value the DS18B20 returns when it hasn't read the temperature. Discussed again recently in this thread:

wildbill:
85 is the value the DS18B20 returns when it hasn't read the temperature. Discussed again recently in this thread:
Boot time and DS18B20 problem - #3 by wildbill - Programming Questions - Arduino Forum

And this seems to be the final piece of the puzzle for you.
Thanks, wildbill! :slight_smile:

Thanks wildbill, I will put initial sensor readings to setup.

Tim,

can you help me out of this SD code? What and where should I put? Should I stay at my code or better to use your ftp like?

edit: after initial reading of sensors in setup() 85 degrees at the start disappeared, thanks again!

Since you have the values in the loop() function, I would put the SD routine immediately after the getPage() call. Like this:

if(!logFile(temp0,temp1)) Serial.println("Log failed");
else Serial.println("Log ok");

Here is logFile:

int logFile(int temp0, int temp1) {
  File fh = SD.open("test.txt",FILE_WRITE);

  if(!fh) {
    Serial.println("Open fail");
    return 0;
  }

  // Use the same character array here to send stuff to the file.
  // pageAdd array is global
  sprintf(pageAdd,"Field1=%u Field2=%u",temp0,temp1);
  fh.println(pageAdd);
  fh.close();
  return 1;
}

problems coming... test.txt created manually, but have errors.

Temperatures are 29 Celsius, 0 Celsius, 0 Celsius,

connecting...failed
Fail Open fail
Log failed
1

Post your current loop() function. That may help me troubleshoot it.
edit: Power it down for a few seconds. Try it again.

powerdown not helped.

void loop()
{
  if(loopCount < 30)
  {
    // if loopCount is less than 30, just delay a second
    delay(1000);
  }
  else
  {
    // every thirty seconds this runs
    loopCount = 0;

    // Modify next line to load different page
    // or pass values to server
//get temperatures
Serial.print("Getting temperatures...\n\r");
    sensors.requestTemperatures();            //issue global request for each temp sensor on network to return a temp
       if(sensors.getAddress(tempDeviceAddress, 0))
          temp0=(sensors.getTempC(tempDeviceAddress)); //read temp from sensor 0
       if(sensors.getAddress(tempDeviceAddress, 1))
          temp1=(sensors.getTempC(tempDeviceAddress)); //read temp from sensor 1 
       if(sensors.getAddress(tempDeviceAddress, 2))
         temp2=(sensors.getTempC(tempDeviceAddress));  //read temp from sensor 2   
  
    Serial.print("Temperatures are ");
    Serial.print(temp0);
    Serial.print(" Celsius, ");
    Serial.print(temp1);
    Serial.print(" Celsius, ");
    Serial.print(temp2);
    Serial.print(" Celsius, ");
    Serial.print("\n\r");
    
   sprintf(pageAdd,"/update?key=XXXXXXXXXXXXXXXXXXXX&field1=%u&field2=%u&field3=%u",temp0,temp1,temp2); 
 // sprintf(pageAdd,"/",totalCount);

    // sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

    if(!getPage(server,pageAdd)) Serial.print("Fail ");
    else Serial.print("Pass ");
    if(!logFile(temp0,temp1,temp2)) Serial.println("Log failed");
    else Serial.println("Log ok");
    
    totalCount++;
    Serial.println(totalCount,DEC);
  }    

  loopCount++;
}
int logFile(int temp0, int temp1, int temp2) {
  File fh = SD.open("test.txt",FILE_WRITE);

  if(!fh) {
    Serial.println("Open fail");
    return 0;
  }

  // Use the same character array here to send stuff to the file.
  // pageAdd array is global
  sprintf(pageAdd,"Field1=%u Field2=%u Field3=%u",temp0,temp1,temp2);
  fh.println(pageAdd);
  fh.close();
  return 1;
}





byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];

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

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

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  } 
  else
  {
    Serial.println("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("Timeout");
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

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

  return 1;
}

UPDATE: strange...without the logfile modification, it is not connecting...that was working fine before...maybe thingspeak issue...

Back up then. Remove the call to logFile and the logFile function. Does it work then?

backed up... connection failed...tried to ping the server..got back answer after ping

The Thingspeak should not affect whether the SD writes or not. If the connection failed, and the SD write failed, something is not right. Are you certain that pageAdd array is big enough to handle all that?

after router restart :

Found device 1 with address: 286C81C5030000FA
Found device 2 with address: 2811E8E803000027
Getting temperatures...

Temperatures are 16 Celsius, 8 Celsius, 6 Celsius,

connecting...connected
HTTP/1.1 200 OK
Server: nginx/0.8.53
Date: Wed, 21 Nov 2012 21:49:10 GMT
Content-Type: text/html; charset=utf-8
Connection: close
Vary: Accept-Encoding
Status: 200
ETag: "1ff1de774005f8da13f42943881c655f"
Cache-Control: max-age=0, private, must-revalidate

24
disconnecting.
Pass Open fail
Log failed
1

this is with the SD version

without SD it is fine.

UPDATE: in the txt I can find the values!!! but failed message appears on serial monitor

let's have a rest... :blush:
again thanks for your help/support Tim!
I'll back to here tomorrow...(meanwhile trying to figure out, how to put date,time and ";" values to the log.it was working alone in a previous sketch what can be found in Reply #23)
If you could help me in that too I would be more than happy!:slight_smile: I have learned a lot today.