Ja das War mir auch bewusst, dass 10 und 4 dadurch geblockt sind.
Mit diesem Code läuft der Pin eine ganze weile. Nach ca 15 min Inaktivität auf der Seite, oder wenn ein anderes Gerät/client auf die Seite zugreift schaltet dieser jedoch wieder aus.
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
int redPin = 3;
int greenPin = 5;
int bluePin = 6;
int relay = 2;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,178,77);
IPAddress dns1(192,168,178,1);
IPAddress gateway(192,168,178,1);
IPAddress subnet(255,255,255,0);
EthernetServer server(80);
File webFile;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip, dns1, gateway, subnet);
server.begin();
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(relay, OUTPUT);
//digitalWrite(relay, HIGH);
digitalWrite(10, HIGH);
if (!SD.begin(4)) {
return;
}
setColour(0,0,0);
}
void loop() {
int BUFSIZ = 255;
int index = 0;
char clientline[BUFSIZ];
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c != '\n' && c != '\r') {
clientline[index] = c;
index++;
if (index >= BUFSIZ)
index = BUFSIZ -1;
continue;
}
clientline[index] = 0;
char* filename = processFile(clientline);
Serial.print("Requested: ");
Serial.println(filename);
/* Setup AJAX Requests Here:
* These are just direct output values that are requested
* with AJAX.
*/
if (strstr(clientline, "GET /?rgb=") != 0) {
char* rgb;
rgb = clientline + 10;
char *str;
char *p = rgb;
int rgbi[3];
int index=0;
while ((str = strtok_r(p, ",", &p)) != NULL) { // delimiter is the semicolon
rgbi[index] = atoi(str);
index++;
Serial.println(str);
}
setColour(rgbi[0],rgbi[1],rgbi[2]);
//Serial.println(rgb);
break;
}
digitalWrite(relay, HIGH);
if (SD.exists(filename)) {
code200(client);
webFile = SD.open(filename);
if (webFile) {
while(webFile.available()) {
client.write(webFile.read());
}
webFile.close();
}
break;
} else {
if (strlen(filename) < 2) {
webFile = SD.open("index.htm");
if (webFile) {
while(webFile.available()) {
client.write(webFile.read());
}
webFile.close();
}
} else {
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<html><head><title>404 - Not Found</title></head><body><h1>404 - Not Found</h1></body></html>");
break;
}
}
break;
}
}
delay(1);
client.stop();
}
}
void setColour(int r,int g,int b) {
r = map(r, 0, 255, 0, 255);
g = map(g, 0, 255, 0, 90);
b = map(b, 0, 255, 0, 100);
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
void code200(EthernetClient client) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
}
char* processFile(char clientline[255]) {
char *filename;
filename = clientline + 5;
(strstr(clientline, " HTTP"))[0] = 0;
return filename;
}