#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xCD, 0x3E }; //Set your Ethernet Shield's MAC address
byte ip[] = { 192,168,1,131 }; // Set your shield's IP address
byte gateway[] = { 192,168,1,1 }; //if you need a gateway IP
byte subnet[] = { 255,255,255,0 }; // Change to your subnet address
EthernetServer server(85); // server port
// declare variables
int ledPin = 6; // LED1
int heatpin = 7; // ANTENNA
int aedpin = 5; // LED3
int fedpin = 8; // fE PWR
int medpin = 9; // me - anolog pin 9 for fan control
int sensorPin = A0; // analog in 0 forlight sense
int sensorValue = 0; // integer for the analog sensor
int PIRstate = 0; // variable for PIR sensor status
int PIR = 2; // PIR sensor is connected to digital pin 2
int motorlevel = 0; // motor
String readString = String(30); // string for fetching data from address
boolean LEDON = false; // LED1 status flag
boolean HEDON = false; // ANTENNA status flag
boolean AEDON = false; // led3 status AE
// SETUP NOW
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet ); //start Ethernet
server.begin(); //test
pinMode(ledPin, OUTPUT); //Set pin 6 to output
pinMode(heatpin, OUTPUT); // set pin 7 to output
pinMode(aedpin, OUTPUT); // set pin 5 to output
pinMode(fedpin, OUTPUT); // set pin 8 to output
pinMode(PIR, INPUT); // set pir to input
pinMode(medpin, OUTPUT); // set pin a3 to output
byte client_ip[4];
sensorValue = analogRead(sensorPin);
}
void loop(){
EthernetClient client = server.available(); // Create a client connection
if (client) {
boolean current_line_is_blank = true; //new
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// send a reply
if (readString.length() < 30) { //read char by char HTTP request
readString.concat(c);
} //store characters to string
// HTML Code
if (c == '\n' && current_line_is_blank) {
// HTML Code
client.println(F("HTTP/1.1 200 OK")); //output HTML data starting with standard header
client.println(F("Content-Type: text/html"));
client.println();
client.print(F("<body style=background-color:BLACK>")); //set background to BLACK
// Auto refresh webpage every 60 seconds
client.println(F("<META HTTP-EQUIV=REFRESH CONTENT=60 URL=>"));
client.println("<center>");
client.println(F("<font color=’green’><h1>INTERNET -- CONTROL </font></h1>/"));//send first heading
client.println(F("<hr />"));
client.println(F("<font color=’#0D8112’ size=’5?> "));
client.println("</center>");
motorlevel = 0;
}
//indexOf("L=")
if (c == '\n') { //if HTTP request has ended
int Le = readString.indexOf("L="); //here is a key component of where
int He = readString.indexOf("H="); //the status is being read by the arduino
int Ae = readString.indexOf("A=");
int Fe = readString.indexOf("F=");
// Check if anything should be lighted or turned on
if (Le > 1){
if (readString.substring(Le,(Le+3)) == "L=1") { //led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}
if (readString.substring(Le,(Le+3)) == "L=0") {
//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false;
}
}
if (Fe > 1){
if (readString.substring(Fe,(Fe+3)) == "F=1") { //PWR has to be turned ON
digitalWrite(fedpin, HIGH); // set the PWR on
FEDON = true;
}
if (readString.substring(Fe,(Fe+3)) == "F=0") { //PWR has to be turned OFF
digitalWrite(fedpin, LOW); // set the PWR OFF
FEDON = false;
}
}
sensorValue = analogRead(sensorPin);
// detemine / print is room is dark or light
client.print(sensorValue); // a test to see the photo sensor value
if (sensorValue < 550) {
client.println( F("<center><font color ='deepskyblue' >THE ROOM IS NOW - *DARK* - </font></center>") );
}
else {
client.println( F("<center><font color ='gold' >THE ROOM IS NOW - *BRIGHT* - </font></center>") );
}
client.println(F("<hr />"));
client.println(F("<center></center>"));
client.println(F("<font color=’green’> </font>"));
client.println("<center>");{
client.println(F("<form method=get name=LED>"));
client.println(F("<button name=F value=1 type=submit style=height:100px;width:130px>PWR On</button>"));
client.println(F("<button name=F value=0 type=submit style=height:100px;width:130px>PWR Off</button>"));
client.println(F("</form>
"));
client.println(F("<hr />"));
client.println("</center>");
client.print(F("<form method=get name=LED>"));
client.print(F("<input type='radio' name='A' value='1'>LED3 ON
"));
client.print(F("<input type='radio' name='A' value='0'>LED3 OFF
"));
client.println(F("<input type=submit value=submit></form>"));
client.println(F("<hr />"));
}
// DETERMINE WHAT STATUS TO PRINT
client.print(F("<font size=’5?>LED1 status: "));
if (LEDON == true) {
client.println(F("<font color='#FF0000' size=’5?>ON</font>")); //red
}
else {
client.println(F("<font color='#0099FF' size=’5?>OFF</font>")); //blue
}
client.println(F("
"));
//printing ant status
client.print(F("<font color='#00B200'>ANT status: ")); //green
if (HEDON == true) {
client.println(F("<font color='#FF0000' size=’5?>ON</font>")); //red
}
else {
client.println(F("<font color='#0099FF' size=’5?>OFF")); //blue
}
client.println(F("
"));
//printing AEant status
client.print(F("<font size=’5? font color='#00B200'>LED3 status: ")); //green
if (AEDON == true) {
client.println(F("<font color='#FF0000' size=’5?>ON")); //red
// Serial.print("LED3 on");
}
else {
client.println(F("<font color='#0099FF' size=’5?>OFF"));//blue
}
client.println(F("
"));
// WEB PAGE Footer
client.println(F("<hr><center><a href=http://xxxxxx:xxx>WEBPAGE</a>
"));
client.println(F("<p><font color=darkcyan>This Page Will Refresh Every 60 seconds.</font></center>"));
client.println(F("<hr />"));
client.println(F("</body></html>"));
readString="";
delay(2);
client.stop(); //stopping client
client.flush();
}
}
}
}
}
I have a working Arduino web server which serves an html web page allowing me to see the status and to control devices.
If I connect from a single PC it works fine. I can turn devices on and off and see status changes.
If I now go to another PC and connect, it causes the Arduino Webserver to become non responsive.
if i connect on one pc and forget to close the browser and then decide to go connect from another pc - the server will not answer..
Or if i am connected and someone else connects, within a few seconds the server becomes unresponsive
Any suggestions how to fix the problem?