This is what I have so far …
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0xC8, 0xDD };
//Change to your server domain
char serverName[] = "www.xxxxxxxxxxx.com";
// change to your server's port
int serverPort = 80;
// change to the page on that server
char pageName[] = "/arduino_post.php";
EthernetClient client;
int totalCount = 0;
// insure params is big enough to hold your variables
char params[32];
// set this to the number of milliseconds delay
// this is 10 seconds
#define delayMillis 5000UL
unsigned long thisMillis = 0;
unsigned long lastMillis = 0;
/// new stuff start
// Do not use pins 10, 11, 12 or 13.
int ledPin = 13; // LED connected to digital pin 13
int inPin1 = 6; // pushbutton connected to digital pin
int inPin2 = 7; // pushbutton connected to digital pin
int inPin3 = 8; // pushbutton connected to digital pin
int inPin4 = 9; // pushbutton connected to digital pin
int val1 = 0; // variable to store the read value
int val2 = 0; // variable to store the read value
int val3 = 0; // variable to store the read value
int val4 = 0; // variable to store the read value
void setup()
{
pinMode(inPin1, INPUT); // sets the digital pin as input
pinMode(inPin2, INPUT); // sets the digital pin as input
pinMode(inPin3, INPUT); // sets the digital pin as input
pinMode(inPin4, INPUT); // sets the digital pin as input
Serial.begin(9600);
// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Serial.print(F("Starting ethernet..."));
if(!Ethernet.begin(mac)) Serial.println(F("failed"));
else Serial.println(Ethernet.localIP());
delay(2000);
Serial.println(F("Ready"));
}
void loop()
{
val1 = digitalRead(inPin1); // read the input pin
val2 = digitalRead(inPin2); // read the input pin
val3 = digitalRead(inPin3); // read the input pin
val4 = digitalRead(inPin4); // read the input pin
if(val1==HIGH)
{
totalCount=1;
// If using a static IP, comment out the next line
Ethernet.maintain();
thisMillis = millis();
if(thisMillis - lastMillis > delayMillis)
{
lastMillis = thisMillis;
// params must be url encoded.
sprintf(params,"rx=%i",totalCount);
if(!postPage(serverName,serverPort,pageName,params))
Serial.print(F("Fail "));
else Serial.print(F("Pass "));
//totalCount++;
// Serial.println(totalCount,DEC);
}
}
if(val2==HIGH)
{
totalCount=2;
// If using a static IP, comment out the next line
Ethernet.maintain();
thisMillis = millis();
if(thisMillis - lastMillis > delayMillis)
{
lastMillis = thisMillis;
// params must be url encoded.
sprintf(params,"rx=%i",totalCount);
if(!postPage(serverName,serverPort,pageName,params))
Serial.print(F("Fail "));
else Serial.print(F("Pass "));
//totalCount++;
// Serial.println(totalCount,DEC);
}
}
if(val3==HIGH)
{
totalCount=3;
// If using a static IP, comment out the next line
Ethernet.maintain();
thisMillis = millis();
if(thisMillis - lastMillis > delayMillis)
{
lastMillis = thisMillis;
// params must be url encoded.
sprintf(params,"rx=%i",totalCount);
if(!postPage(serverName,serverPort,pageName,params))
Serial.print(F("Fail "));
else Serial.print(F("Pass "));
//totalCount++;
// Serial.println(totalCount,DEC);
}
}
if(val4==HIGH)
{
totalCount=4;
// If using a static IP, comment out the next line
Ethernet.maintain();
thisMillis = millis();
if(thisMillis - lastMillis > delayMillis)
{
lastMillis = thisMillis;
// params must be url encoded.
sprintf(params,"rx=%i",totalCount);
if(!postPage(serverName,serverPort,pageName,params))
Serial.print(F("Fail "));
else Serial.print(F("Pass "));
//totalCount++;
// Serial.println(totalCount,DEC);
}
}
else{}
}
byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData)
{
int inChar;
char outBuf[64];
// Serial.print(F("connecting..."));
if(client.connect(domainBuffer,thisPort) == 1)
{
// Serial.println(F("connected"));
// send the header
sprintf(outBuf,"POST %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",domainBuffer);
client.println(outBuf);
client.println(F("Connection: close\r\nContent-Type: application/x-www-form-urlencoded"));
sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData));
client.println(outBuf);
// send the body (variables)
client.print(thisData);
}
else
{
Serial.println(F("failed"));
return 0;
}
int connectLoop = 0;
while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
connectLoop = 0;
}
delay(1);
connectLoop++;
if(connectLoop > 10000)
{
Serial.println();
Serial.println(F("Timeout"));
client.stop();
}
}
Serial.println();
// Serial.println(F("disconnecting."));
client.stop();
return 1;
}