hi, after a few hits (max 7 hits )on my webserver it hangs?, I type either 192.168.0.137/?0 or 192.168.0.137/?1
Anyone with good ideas? or is there something in the ethernet library that is making my program hangs?
/*
* Web Server
*
* A simple web server that control and show status of relay
*/
//Arduino uses digital pins 10, 11, 12, and 13 (SPI) to communicate
//with the W5100 on the ethernet shield. These pins cannot be used for general i/o.
#include <Ethernet.h>
#include <String.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 137 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
const int relaypin = 6; //The relay is attached to this pin.
boolean relaystatus_on = false; //Define if relay is switched ON=true, or OFF=false
char cbuf[30]; //String that contains received http response
int i=0; //Count up to 30 char for http response
Server server(80);
void setup()
{
// Initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
//Ethernet.begin(mac, ip);
// Start web server
server.begin();
// Initialize the relaypin as an output:
pinMode(relaypin, OUTPUT);
}
void loop()
{
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// Read only the first chars in wep response
if (i<30){
cbuf[i]=c;
i++;
}
// if we've gotten to the end of the line (received a newline character)
// and the line is blank, the http request has ended, so we can send a reply
if (c == '\n' && current_line_is_blank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("
");
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
}
else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
//Read enough of wep response
if (i==30){
cbuf[i]='\0'; //terminating the string with \0
i=0; // Reset so we only read the http respones one and first time.
}
// Switch relay ON
if ((((int)strstr(cbuf, "?1"))!=0) && (relaystatus_on==false)) {
digitalWrite(relaypin, HIGH); // Switch ON relay.
relaystatus_on=true;
client.println("Relay is switched ON");
}
// Switch relay OFF
else if ((((int)strstr(cbuf, "?0"))!=0) && (relaystatus_on==true)) {
digitalWrite(relaypin, LOW); // Switch OFF relay.
relaystatus_on=false;
client.println("Relay is switched OFF");
}
//If wrong commands or no commands write below
else {
if (relaystatus_on==true) {client.println("Relay status is ON"); }
if (relaystatus_on==false){client.println("Relay status is OFF"); }
}
client.println("
Switch relay OFF: 192.168.0.137/?0
Switch relay ON : 192.168.0.137/?1");
// give the web browser time to receive the data
delay(1);
client.stop();
}
//Http response is done to browser and time to continue program.
} //EOF