(you haven't provided your code)
My code is to large. Every time I try to post it, I get a script error message that freezes my computer and forces me to re-start the computer to get the script error message of my computer. I spent all morning trying to chop the code up into smaller pieces that still allows it to work, but even that is to large to post.
You probably need to set a flag for your button push such that the client loop is not reenterd until the button is released and then pushed again. Your current code is probably creating a spew of client request upon the button press.
This is the code for the Arduino2. It is much smaller that the Arduino1 code and I hope it will fit. Does anyone have any examples of using flags that I could use as a learning reference to understand how flags work?
/*
Web client/Server for Arduino2
This sketch is a evolutionary experiment using the logic behind the code
Generously provided by Supporters of the arduino Form.
Without forum supporters Zoomkat and SufferTim
This code would not have been possible for me to comprehend
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
Pin 2 connected to button
Pin 3 and 4 connected to leds
created based on the sketch
zoomkat 10-02-14, combined client and server
modified for client server direct handshaking
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,174);
EthernetServer server(80);
EthernetClient client;
byte Arduino1Name[] = { 192, 168, 1, 177 };
String readString; //used by server to capture GET request
boolean RunForTheHills = 0;
boolean OnlyOnce = 0;
boolean Internet = 0;
boolean RepoMan = 0;
boolean ApplePie = 0;
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(2,INPUT); // button 1 (led on if pressed)
pinMode(4,OUTPUT); // led 2
pinMode(5, OUTPUT); // led 3
Ethernet.begin(mac, ip);
delay(1000); // give the Ethernet shield a second to initialize:
Serial.println(ip);
Serial.println("connecting...");
server.begin(); // start the web server on port 80
}
void loop()
{
/*========================= Button 1 Pressed ===================
=========== If overide from Arduino 1 not valid ==============
====================== Led 2 comes on ==================
*/
if (digitalRead(2) == HIGH)
{ RunForTheHills = HIGH; }
/*==================== Button 1 not Pressed ===================
========================== Led2 is not on ====================
*/
if (digitalRead(2) == LOW)
{ RunForTheHills = LOW; }
//---------------------------------------------------------------------
if (RunForTheHills == HIGH) {
if (ApplePie == LOW)
{ Serial.println("Arduino 2 button 2 pressed");
if (RepoMan == LOW)
{ digitalWrite(4,HIGH);
Serial.println("Arduino 2 led 2 on");
} else { Serial.println("Arduino2 is still in override mode");
Serial.println("led 2 will not turn on");
}
ApplePie = HIGH;
}
}
if (RunForTheHills == LOW)
{ digitalWrite(4,LOW);
ApplePie = LOW;
}
if (RepoMan == HIGH)
{ if (OnlyOnce == LOW)
{ digitalWrite(5, HIGH);
{ Serial.println("A1 button activated");
Serial.println("led 3 (pin4) should come on");
Serial.println("led 2 (pin3) should not come on if button pressed on A2");
Serial.println("---------------------------");
if ( client.connect(Arduino1Name, 80) )
{ OnlyOnce = HIGH;
Internet = HIGH;
Serial.println("");
Serial.println("connected");
// Make a HTTP request:
client.println("GET /OR"); // OR is an acronym for (OverRide Received)
client.println("Host: Arduino2");
client.println("Connection: close");
client.println();
// client.stop();
} else { // if you didn't get a connection to the server:
Serial.println("connection failed");
Serial.println("retrying");
}
}
}
}
if (RepoMan == LOW)
{ if (OnlyOnce == HIGH)
{ digitalWrite(5, LOW);
Serial.println("A1 button 2 activated");
Serial.println("led 3 (pin4) should go off");
Serial.println("led 2 (pin3) should come on if button pressed on A2");
Serial.println("---------------------------");
if ( client.connect(Arduino1Name, 80) )
{ Internet = HIGH;
OnlyOnce = LOW;Serial.println("");
Serial.println("connected"); // Make a HTTP request:
client.println("GET /OD"); //OD is an acronym for (OverRide Disabled)
client.println("Host: Arduino2");
client.println("Connection: close");
client.println();
} else { // if you didn't get a connection to the server:
Serial.println("connection failed");
Serial.println("Retrying");
}
}
}
while (client.available())
{ char c = client.read();
Serial.write(c);
// if (c == '\r')
//{ client.stop(); }
}
if (Internet == HIGH)
{ // if the server's disconnected, stop the client:
Internet = LOW;
Serial.println("A2 disconnecting from A1");
client.stop();
}
EthernetClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
if (RepoMan == HIGH)
{ client.print("Arduino1 has disabled Arduino2 button");
} else { client.print( "Arduino2 button is active");
}
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
if (currentLine.endsWith("GET /OvR")) // GET /OvR (OverRide) turns LED3 on and disabled LED2
{ RepoMan = HIGH;
}
if (currentLine.endsWith("GET /OvD")) // GET /OvD (OverRide-Disabled) turns LED3 off and enables use of LED2
{ RepoMan = LOW;
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}