Also ich kann dir das hier empfehlen... du brauchst nichts weiter zu beachten außer das der Mega 3 oder 4 andere Pins verwendet und du sie deshalb nicht verwenden kannst:
Also in dem fall packst du an Pin 22 ne LEd (mit widerstand ;-)) und kannst über den Browser einfach die ip deines Arduino angeben z.b 192.168.2.100/?2 und absenden
dann geht die LED an.
sendest du 192.168.2.100/?3 geht sie aus.
mfg at_on
#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte ip[] = { 192, 168, xxx, xxx }; //ip address to assign the arduino
byte gateway[] = { 192, 168, xxx, xxx }; //ip address of the gatewa or router
//Rarly need to change this
byte subnet[] = { 255, 255, xxx, xxx };
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Server server = Server(80); //port 80
////////////////////////////////////////////////////////////////////////
void setup(){
//Pins 10,11,12 & 13 are used by the ethernet shield <= trifft nicht auf den Mega zu
pinMode(22, OUTPUT);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
}
void loop(){
// listen for incoming clients, and process qequest.
checkForClient();
}
void checkForClient(){
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected()) {
if (client.available()) {
if(!sentHeader){
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
sentHeader = true;
}
char c = client.read();
if(reading && c == ' ') reading = false;
if(c == '?') reading = true; //found the ?, begin reading the info
if(reading){
Serial.print(c);
switch (c) {
case '2':
//add code here to trigger on 2
triggerON(22, client);
break;
case '3':
//add code here to trigger on 3
triggerOFF(22, client);
break;
}
}
if (c == '\n' && currentLineIsBlank) break;
if (c == '\n') {
currentLineIsBlank = true;
}else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
}
}
void triggerON(int pin, Client client){
//turn on a pin - Client needed just for HTML output purposes.
client.print("Turning on pin ");
client.println(pin);
client.print("
");
digitalWrite(pin, HIGH);
}
void triggerOFF(int pin, Client client){
//turn off a pin - Client needed just for HTML output purposes.
client.print("Turning off pin ");
client.println(pin);
client.print("
");
digitalWrite(pin, LOW);
}