A colleague an I are trying to get a datacollect mechanism to work. Right now we are stranded at a HTTP POST issue. We have used the sample code from Arduino Playground - WebClient and our code looks like this:
#include <SPI.h>
#include <Ethernet.h>
// this must be unique
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// change to your network settings
IPAddress ip(10,0,0,100);
IPAddress gateway(10,0,0,1);
IPAddress subnet(255, 255, 255, 0);
// change to your server
IPAddress server(123,456,789,000); // our servers ip address goes here
EthernetClient client;
int totalCount = 0;
int loopCount = 0;
char pageAdd[32];
void setup() {
Serial.begin(9600);
// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// Start ethernet
Serial.println("Starting ethernet...");
// Ethernet.begin(mac, ip, gateway, gateway, subnet);
// If using dhcp, comment out the line above
// and uncomment the next 2 lines
if(!Ethernet.begin(mac)) Serial.println("failed");
else Serial.println("ok");
digitalWrite(10,HIGH);
Serial.println(Ethernet.localIP());
delay(2000);
Serial.println("Ready");
}
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
// sprintf(pageAdd,"/",totalCount);
sprintf(pageAdd,"/insert.php?systemID=12345&data=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16"); // The 'systemID' and 'data' is to be sendt as a HTTP POST to the file insert.php, which grabs the data and puts it in a database
if(!getPage(server,pageAdd)) Serial.print("Fail ");
else Serial.print("Pass ");
totalCount++;
Serial.println(totalCount,DEC);
}
loopCount++;
}
byte getPage(IPAddress ipBuf,char *page)
{
int inChar;
char outBuf[128];
Serial.print("connecting...");
if(client.connect(ipBuf,8000))
{
Serial.println("connected");
sprintf(outBuf,"POST %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;
}
The Arduino connects with the server just fine, but the data isn't posted. What are we doing wrong?
/Carl