I'm not sure if you've had any luck yet, I will post my code for a pachube client and a web server.. It seems to work ok but I'm sure it needs work, lol.. In fact if anyone want's to give me some tips to refine it, let me know.. I am totally unfamiliar with the ethernet library and kind of self taught to begin with..
/*
Pachube sensor client and Web server
This sketch connects analog 0 connected to an lm34 temp sensor
Sends temp info to pachube and has a web server on port 80 using a Wiznet 810mj
Circuit:
* LM34 sensor output attached to analog in 0
* Ethernet shield attached to pins 10, 11, 12, 13
created using pachube and web server examples originally created by Tom Igoe
modified Nov 9th 2011 by tek1229
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Enter a MAC address
byte ip[] = { 192,168,4,215 }; // The IP address will be dependent on your local network:
byte pachube[] = { 173,203,98,29 }; // pachube, orignal provided was incorrect..
Client client(pachube, 80); // ethernet client port
Server homeFeed(80); // ethernet server port
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const int postingInterval = 20000; //delay between updates to Pachube.com..
const byte webRefresh = 5 ; // setting in seconds for the web page refresh-this page will auto refresh
void setup() {
Ethernet.begin(mac, ip); // start the Ethernet connection:
Serial.begin(9600); // start the serial library:
delay(1000); // give the Ethernet shield a second to initialize:
homeFeed.begin(); // start web server
Serial.println("Initialized..."); // Simply a debug to show reset
}
void loop() {
// Pachube timing loop.. uses generic millis timer to send data to pachube when needed
int sensorReading = (analogRead(0)*.4882)-3; // read the analog sensor and convert to temp-adjusted for slight variance
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(sensorReading); // if you're not connected, and delay set above have passed then send data to pachube
}
//////// Web server section /////
httpRequest(sensorReading); // http request function--see if web page is being requested
}
///////////////////////// end of loop ///////////////////////////////////////////////////
void httpRequest(String tempData) { // this function checks for a http request and if one is requested serves up a web page with temp..
Client client = homeFeed.available();
if (client) {
boolean currentLineIsBlank = true; // an http request ends with a blank line
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<font size = '6'>");
client.print("<META HTTP-EQUIV='refresh' CONTENT='");
client.print(webRefresh, DEC);
client.print("'>");
client.println("<br />");
client.println("<br />");
client.print("<H1 ALIGN='CENTER'>Temperature of Garage is ");
client.print(tempData);
client.println(" degrees</H1");
client.println("<br />");
Serial.println("Web page accessed");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
}
}
void sendData(String thisData) { // this function makes a HTTP connection to the pachube server:
Serial.print(thisData); // debug
Serial.println(" degrees "); // debug
if (client.connect()) { // if there's a successful connection:
Serial.println("connecting to pachube..."); // debug
client.print("PUT /api/XXXXX.csv HTTP/1.1\n"); // send the HTTP PUT request. Change x's to your feed
client.print("Host: www.pachube.com\n"); // fill in your feed address here:
client.print("X-PachubeApiKey: <<<<<API KEY HERE>>>>>>\n"); // fill in your Pachube API key here:
client.print("Content-Length: ");
client.println(thisData.length(), DEC);
// last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n");
client.println("Connection: close\n");
// here's the actual content of the PUT request:
client.println(thisData);
Serial.println("Send temp to Pachube "); // debug
client.stop();
lastConnectionTime = millis(); // note the time that the connection was made:
}
else {
Serial.println("connection failed"); // if you couldn't make a connection:
}
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
int digits = 1; // there's at least one byte:
int dividend = someValue /10; // continually divide the value by ten,
while (dividend > 0) { // adding one to the digit count for each
dividend = dividend /10; // time you divide, until you're at 0:
digits++;
}
return digits; // return the number of digits:
}