Hey Guys,
Im using the standard web client example sketch to do a get request from a local IP.
Im wanting to set up multiple get requests to work from multiple push buttons on input 2, 3, 4 for example. I can get the 1 button to do a get request but can't work out how to define different digital inputs to do different get requests.
Any help/example codes?
Here is what im currently using for the 1 button and 1 get request
#include <SPI.h>
#include <Ethernet.h>
// this must be unique
byte mac[] = {
0xDE, 0xAD, 0xB8, 0xEF, 0xFE, 0xED };
int systemState = 0;
// change to your network settings
IPAddress ip(192,168,1,113);
// change to your server
IPAddress server(192,168,1,42);
char serverName[] = "192,168,1,42";
int serverPort = 14999;
EthernetClient client;
int totalCount = 0;
char pageAdd[64];
const int buttonPin = 2;
void setup() {
Serial.begin(9600);
// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
pinMode(buttonPin, INPUT);
Serial.println("Starting ethernet…");
Ethernet.begin(mac, ip);
Serial.println(Ethernet.localIP());
delay(2000);
Serial.println("Ready");
}
void loop() {
if (systemState == 0){
pushButton();
}
}
byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
int inChar;
char outBuf[128];
Serial.print(F("connecting…"));
if(client.connect(ipBuf,thisPort) == 1)
{
Serial.println(F("connected"));
sprintf(outBuf,"GET %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",serverName);
client.println(outBuf);
client.println(F("Connection: close\r\n"));
}
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;
}
void pushButton(){
if(digitalRead(buttonPin) == HIGH){
sprintf(pageAdd,"/siren"); (!getPage(server, serverPort, pageAdd));
Serial.println("Button pressed");
systemState == 1;
}
}