I'm expertimenting with the arduino webserver. I try to switch a digital pin connected with one simple led on and off.
I use this code:
/*
* Web Server
*
* A simple web server that shows the value of the analog input pins.
*/
#if ARDUINO > 18
#include <SPI.h> // needed for Arduino versions later than 0018
#endif
#include <Ethernet.h>
#include <TextFinder.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { xxx, xxx, 1, 200 };
byte gateway[] = { 192, xxx, 1, xxx};
byte subnet[] = { 255, xxx, 255, 0 };
Server server(80);
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.println("ready");
}
void loop()
{
Client client = server.available();
if (client) {
TextFinder finder(client );
while (client.connected()) {
if (client.available()) {
// counters to show the number of pin change requests
int digitalRequests = 0;
int analogRequests = 0;
if( finder.find("GET /") ) {
// find tokens starting with "pin" and stop on the first blank line
while(finder.findUntil("pin", "\n\r")){
char type = client.read(); // D or A
int pin = finder.getValue();
int val = finder.getValue();
if( type == 'D') {
Serial.print("Digital pin ");
pinMode(pin, OUTPUT);
digitalWrite(pin, val);
digitalRequests++;
}
else if( type == 'A'){
Serial.print("Analog pin ");
analogWrite(pin, val);
analogRequests++;
}
else {
Serial.print("Unexpected type ");
Serial.print(type);
}
Serial.print(pin);
Serial.print("=");
Serial.println(val);
}
}
Serial.println();
// the findUntil has detected the blank line (a lf followed by cr)
// so the http request has ended and we can send a reply
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// output the number of pins handled by the request
client.print(digitalRequests);
client.print(" digital pin(s) written");
client.println("
");
client.print(analogRequests);
client.print(" analog pin(s) written");
client.println("
");
client.println("
");
// output the value of each analog input pin
for (int i = 0; i < 6; i++) {
client.print("analog input ");
client.print(i);
client.print(" is ");
client.print(analogRead(i));
client.println("
");
}
break;
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
One problem. If I type http://192.xxx.1.200/?pinD7=1 it works and my led switch on but when I type http://192.168.1.200/?pinD7=2 my led didn`t switch off. Whats the problem (same problem with a speakertone; only on and not off).