Adesso dovrebbe andare. Più o Meno. ![]()
#include <String.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
IPAddress ip(192, 168, 40, 15);
IPAddress gateway(192, 168, 40, 254);
IPAddress subnet(255, 255, 255, 0);
EthernetServer server(80); //server port
String readString; //string
boolean LED1ON = false; //LED1 status flag
boolean LED2ON = false; //LED2 status flag
boolean LED3ON = false; //LED3 status flag
boolean login = false;
unsigned int previousMillis = 0;
unsigned int currentMillis = 0;
unsigned int interval = 60*1000;
void setup(){
delay(2000);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.begin(9600);
Serial.print("Serial Start! at ");
Serial.print(millis());
Serial.println(" millisecond.");
Ethernet.begin(mac, ip, gateway, gateway, subnet);
// delay(2000);
Serial.println("Ethernet configured");
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
Serial.print("SubnetMask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("Gateway: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server: ");
Serial.println(Ethernet.dnsServerIP());
Serial.println("Wait client...");
}
void loop(){
currentMillis = millis();
EthernetClient client = server.available();
if (client) {
Serial.println("Client connect");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
readString.concat(c);
if (c == '\n' && currentLineIsBlank){
Serial.print("--> ");
Serial.println(readString);
CheckLogin();
if(login==true) CheckLed();
WebPage(client);
//clearing string for next read
readString="";
//stopping client
client.stop();
}
}
}
Serial.println("Client disconnect");
}
}
void CheckLogin() {
if (currentMillis - previousMillis > interval){
login = false;
previousMillis = currentMillis;
Serial.println("Login Timeout");
}
if (readString.indexOf("Nome=user&Pwd=pass") > 0) {
login=true;
previousMillis = currentMillis;
Serial.println("Login OK ");
}
if (readString.indexOf("L=Exit") > 0) {
login=false;
Serial.println("No Login");
}
}
void WebPage (EthernetClient &client){
// INIZIO DICHIARAZIONE PAGINA HTML
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println();
client.print(F("<html><head><title>ARDUINO Controllo Led via WEB</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ></head><body>"));
// Finestra di login
if (login==false) {
// client.print(F("<form action='http://192.168.40.220/'>"));
client.print(F("<form>"));
client.print(F("Nome"));
client.print(F("utente: <input name='Nome' value=''>"));
client.print(F("Password: <input type='Password' name='Pwd' value=''>"));
client.print(F("<input type='submit' value=' OK '>"));
client.print(F("</form>"));
}
// Login OK
if (login==true) {
//Primo led
client.println(F("<hr />"));
client.println(F("<h1>LED1</h1>"));
client.println(F("
"));
//printing LED status
client.print(F("<span>STATO LED: </span>"));
if (LED1ON) {
client.println(F("<span style='color:green'>ON</span>"));
}
else
{
client.println(F("<span style='color:grey'>OFF</span>"));
}
client.print(F("<h2><a href='/?L=1'>ON</a> | <a href='/?L=01'>OFF</a></h2>"));
client.println(F("<hr />"));
//Secondo led
client.println(F("<h1>LED2</h1>"));
client.println(F("
"));
//printing LED status
client.print(F("<span>STATO LED: </span>"));
if (LED2ON) {
client.println(F("<span style='color:green'>ON</span>"));
}
else
{
client.println(F("<span style='color:grey'>OFF</span>"));
}
client.print(F("<h2><a href='/?L=2'>ON</a> | <a href='/?L=02'>OFF</a></h2>"));
client.println(F("<hr />"));
//terzo led
client.println(F("<h1>LED3</h1>"));
client.println(F("
"));
//printing LED status
client.print(F("<span>STATO LED: </span>"));
if (LED3ON) {
client.println(F("<span style='color:green'>ON</span>"));
}
else
{
client.println(F("<span style='color:grey'>OFF</span>"));
}
client.print(F("<h2><a href='/?L=3'>ON</a> | <a href='/?L=03'>OFF</a></h2>"));
client.println(F("<hr />"));
//Exit
// client.println(F("<h1>Exit</h1>"));
// client.println(F("
"));
client.print(F("<h2><a href='/?L=Exit'>Exit</a>"));
} //chiude if login=true
client.println(F("</body></html>"));
}
void CheckLed() {
// Check Led
if(readString.indexOf("L=1") > 0) {
digitalWrite(3, HIGH); // set the LED on
LED1ON = true;
}
if(readString.indexOf("L=01") > 0)
{
//led has to be turned OFF
digitalWrite(3, LOW); // set the LED OFF
LED1ON = false;
}
if(readString.indexOf("L=2") > 0) {//lets check if LED should be lighted
//led has to be turned ON
digitalWrite(5, HIGH); // set the LED on
LED2ON = true;
}
if(readString.indexOf("L=02") > 0)
{
//led has to be turned OFF
digitalWrite(5, LOW); // set the LED OFF
LED2ON = false;
}
if(readString.indexOf("L=3") > 0) {//lets check if LED should be lighted
//led has to be turned ON
digitalWrite(6, HIGH); // set the LED on
LED3ON = true;
}
if(readString.indexOf("L=03") > 0)
{
//led has to be turned OFF
digitalWrite(6, LOW); // set the LED OFF
LED3ON = false;
}
}