first 3 attempt was with open fail log failed...and sendint to web is ok... ??
current code is:
#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:
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;