haha! i did ask for it so go ahead!
here is an abridged version of the code - it is supposed to do other stuff (like use dhcp and tell my via two leds what its ip address is, etc) but ive stripped it back to what you see - and ive taken out my thingspeak codes too.
hopefully you can make sense of it - at this stage its basically a couple of examples mashed inelegantly together!
i think its to do with client (for thingspeak) and webclient (for posting back a web page to a client) ......
#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <EthernetDHCP.h>
#include <Server.h>
#include <Udp.h>
String sVersion = "v8";
long lastTweetTime = 0;
int iRefresh=0;
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
byte ip[] = { 17, 2, 0, 16 }; // Must be unique on local network
byte gateway[] = { 17, 2, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
Server server_web(80);// Initialize the Ethernet server library
static unsigned long prevTime = 0; // Time between IP lease refreshes.
//Sensor Variables:
int iLoop = 0;
String readString = String(30);
int ledPin = 9;
boolean LEDON = false; //LED status flag
boolean ISDARK = false;
int torchPin = 8;
float hiTemp=0;
float lowTemp =0;
float hiLight=0;
float lowLight =0;
byte server[] = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API
String thingtweetAPIKey = ""; // Write API Key for a ThingSpeak Channel
Client client(server, 80);
const int updateThingSpeakInterval = 30000; // Time interval in milliseconds to update ThingSpeak (30000 = 30 seconds)
const long updateTwitterInterval = 5000; // Time interval in milliseconds to update Twitter (3600000 = 1 hour)
String writeAPIKey = "";
boolean lastConnected = false;
boolean alreadyserving =false;
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
// SetupDHCPEthernet();
// Serial.println(ip_to_str(ip));
//EthernetDHCP.begin(mac, 1);
// Ethernet.begin(mac, ip);// <-- if using a staic IP hard coded.
delay(1000);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A5, INPUT);
pinMode(2, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
analogWrite(3, 255);
analogWrite(5, 255);
analogWrite(6, 255);
}
boolean busy = false;
String sTwitterMsg="";
long lastConnectionTime = 0;
const int updateInterval = 5000;
void loop()
{
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println();
Serial.println("...disconnected.");
Serial.println();
client.stop();
}
lastConnected = client.connected();
//button is pressed and been a while since last tweet:
if (analogRead(A5)<200 && (millis() - lastTweetTime) > updateTwitterInterval)
{
String analogPin0 = String(analogRead(A0), DEC);
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
{
updateThingSpeak("field1="+analogPin0);
}
lastConnected = client.connected();
}
ServeBasicWebPage();
}
void updateThingSpeak(String tsData)
{
if (client.connect())
{
Serial.println("Connected to ThingSpeak...");
Serial.println();
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
resetCounter = 0;
}
else
{
Serial.println("Connection Failed.");
Serial.println();
resetCounter++;
// if (resetCounter >=5 ) {resetEthernetShield();}
lastConnectionTime = millis();
}
}
// Utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr)
{
static char buf[16];
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
return buf;
}
void ServeBasicWebPage()
{
// Create a client connection
Client webclient = server_web.available();
if (webclient) {
while (webclient.connected()) {
if (webclient.available()) {
char c = webclient.read();
//read char by char HTTP request
if (readString.length() < 100)
{
//store characters to string
readString += c; //replaces readString.append(c);
}
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
//dirty skip of "GET /favicon.ico HTTP/1.1"
// if (readString.indexOf("?") <0)
//{ Serial.print("Skipping");
//skip everything
//}
// else
//lets check if LED should be lighted
if(readString.indexOf("L=1") >0)//replaces if(readString.contains("L=1"))
{
//led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false;
}
// now output HTML data starting with standart header
webclient.println("HTTP/1.1 200 OK");
webclient.println("Content-Type: text/html");
webclient.println();
//set background to yellow
webclient.print("<body><font size=""2"" face=""Arial"" >");// style=background-color:yellow>");
webclient.println(readString);
webclient.println( LEDON);
webclient.println("<P></P>LED Control. 1=On, 0=Off.");
//address will look like http://192.168.1.110/?L=1 when submited
webclient.println("<form method=get name=LED><input type=submit name=L value=1><input type=submit name=L value=0></form>");
//printing LED status
webclient.print("LED Status: ");
if (LEDON)
webclient.println("<font color='green' size='5'>ON</font>");
else
webclient.println("<font color='grey' size='5'>OFF</font>");
iLoop++;
webclient.println(". Loop " +String(iLoop));
float temperature = analogRead(1) * .004882814; //getting the voltage reading from the temperature sensor
temperature = (temperature - .5) * 100; //converting from 10 mv per degree wit 500 mV offset
webclient.print(". Temperature : "); //to degrees ((volatge - 500mV) times 100)
webclient.print(temperature );
webclient.print(" degrees centigrade." );
webclient.println("<hr />" + sVersion);
webclient.println("<hr />IP Address : ");
// webclient.println( ip_to_str(ipAddress));
//webclient.println( ip_to_str(ip ));
webclient.println("<hr />Useful Links:
" );
// webclient.println("<hr />Log:
"+ sLog );
webclient.println("</body></html>");
//clearing string for next read
readString="";
//stopping client
webclient.stop();
}
}
}
}
}