Hi,
I wrote some code to let the arduino+ethernet shield send a plain website with two buttons to switch on or off an led which is placed behind a resistor on port 2. Generally, the code is based on the WebServer example. The command is posted as an extension of the url: $1 switches on, $2 switches off.
Modifying the url works like a charm to switch the led, but apparently the buttons work a kind of whacky:
To achieve the desired effect, I always have to click a button twice. When it's pressed only once, you can see it go to the associated state for blink of an eye and then immediately go back to what it was.
So, what's the error in my code?
Here it is:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,105);
boolean incoming = 0;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
int led=2;
String ipstring="http://192.169.0.105";
int power;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(led, OUTPUT);
pinMode(7, OUTPUT);
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("client connected");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
//reads URL string from $ to first blank space
if(incoming && c == ' '){
incoming = 0;
}
if(c == '
Thanks in advance and greetings
){
incoming = 1;
}
//Checks for the URL string $1 or $2
if(incoming == 1){
Serial.println(c);
if(c == '1'){
power=HIGH;
digitalWrite(led, power);
// for debugging purposes
// Serial.println("ON");
// digitalWrite(led, HIGH);
// delay(500);
}
if(c == '2'){
power=LOW;
digitalWrite(led, power);
// for debugging purposes
// Serial.println("OFF");
// digitalWrite(led, LOW);
// delay(500);
}
}
digitalWrite(led, power);
//Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.print("");
client.print("");
client.print("");
client.print("<script type="text/javascript">");
client.print("function btn_power_on()");
client.print("{");
//client.print("alert('ON!');");
client.print("parent.location='$1'");
client.print("}");
client.print("function btn_power_off()");
client.print("{");
//client.print("alert('OFF!');");
client.print("parent.location='$2'");
client.print("}");
client.print("");
client.print("");
client.print("");
client.print("");
client.print("<input type=button style="width:350px;height:200px" onClick="btn_power_on()" value='power on'>");
client.print("
");
client.print("
");
client.print("
");
client.print("
");
client.print("
");
client.print("<input type=button style="width:350px;height:200px" onClick="btn_power_off()" value='power off'>");
client.print("");
client.print("");
client.print("");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
digitalWrite(led, power);
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Thanks in advance and greetings