#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
//назначаем МАС и IP
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
EthernetServer server(80);
//на каких ногах переключатели
int pinscount=8;
int pins[]={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
int pins_status[]={LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
int command = 0;
int setpin=0;
int setpinstatus=0;
void setup() {
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());
for (int i=0; i < pinscount; i++){
pinMode(pins[i], OUTPUT);
};
}
void loop(){
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
TextFinder response(client);
if(response.find("GET /")) {
if (response.find("command=")) {
command = response.getValue();
Serial.println(command);
}
};
if (command==1) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.print("<!DOCTYPE HTML>");
//Выводим информацию об устройстве
client.print("{\"ip\":\"");
client.print(Ethernet.localIP());
client.print("\",\"pins\":[");
for (int i=0; i < pinscount; i++){
client.print(pins[i]);
if (i<pinscount-1){client.print(",");};
};
client.print("],\"pinsstatus\":[");
for (int i=0; i < pinscount; i++){
client.print(pins_status[i]);
if (i<pinscount-1){client.print(",");};
};
client.print("]}\n");
};
if (command==99){
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.print("<!DOCTYPE HTML>");
if (response.find("setpin=")) {
setpin = response.getValue();
Serial.println(setpin);
}
if (response.find("setpinstatus=")) {
setpinstatus = response.getValue();
Serial.println(setpinstatus);
}
digitalWrite(pins[setpin], setpinstatus);
pins_status[setpin]=setpinstatus;
client.print("{\"status\":\"ok\"}\n");
};
client.stop();
}
}
delay(1);
client.stop();
Serial.println("client disonnected");
}
}