Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Arduino Ethernet Shield + Accensione LED

Raccolta di codici sorgenti

Moderatore: Foto UtentePaolino

0
voti

[1] Arduino Ethernet Shield + Accensione LED

Messaggioda Foto Utenteopenmind » 3 gen 2013, 12:59

Salve ho un problema con il semplice circuito per l'accensione da remoto di un LED con Arduino Ethernet Shield. Il problema è che una volta acceso il LED nella pagina web del server questo rimane accesso (fin qui tutto bene) ma quando vado a riaprire la pagina web in un secondo momento per spegnerlo o semplicemente per vedere se è ancora acceso questo si spegne automaticamente, come se il circuito ripartisse da zero ogni volta che si connette un nuovo client. Come faccio a non far resettare il client??
Grazie in anticipo!

Questo è il codice:

Codice: Seleziona tutto
#include <String.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 3 }; // indirizzo IP del server
byte gateway[] = { 192, 168, 1, 1 }; // indirizzo ip del router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

int ledPin = 2; // LED pin
String readString; //string
boolean LEDON = false; //LED status flag

void setup(){
Ethernet.begin(mac, ip, gateway, subnet);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop(){
EthernetClient client = server.available();
if (client) {
  boolean currentLineIsBlank = true;
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
        readString.concat(c); //store characters to string
      //if HTTP request has ended
      if (c == '\n' && currentLineIsBlank) {
         Serial.print(readString);
        if(readString.indexOf("L=1") > 0) {//lets check if LED should be lighted
          //led has to be turned ON
          digitalWrite(ledPin, HIGH); // set the LED on
          LEDON = true;
          }else{
          //led has to be turned OFF
          digitalWrite(ledPin, LOW); // set the LED OFF
          LEDON = false;
        }
        // now output HTML data starting with standart header
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();
        client.print("<html><head><title>ARDUINO Controllo Led via WEB</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ></head><body>");
        //send first heading
        client.println("<h1>LED CONTROL by internet connection</h1>");
        client.println("<hr />");
        client.println("<h1>LED control</h1>");
        //printing LED status
        client.print("<span>LED status: </span>");
          if (LEDON) {
           client.println("<span style='color:green'>ON</span>");
            }
            else
            {
           client.println("<span style='color:grey'>OFF</span>");
          }
        client.print("<h2><a href='/?L=1'>ACCENDI</a> | <a href='/?L=0'>SPEGNI</a></h2>");
        client.println("</body></html>");
        readString="";
        client.stop();

        } //if c == /n
    } // if client available
  } // while client connesso
} // if client
} //loop

Avatar utente
Foto Utenteopenmind
1 1 3
 
Messaggi: 15
Iscritto il: 29 ott 2012, 18:13

0
voti

[2] Re: Arduino Ethernet Shield + Accensione LED

Messaggioda Foto Utenteopenmind » 4 gen 2013, 13:26

Ho risolto. Bastava spezzare l'if, cioè invece di fare un if-else basta fare due if così da non escludere la situazione nuovo client senza nessuna stringa nell' URL O_/
Avatar utente
Foto Utenteopenmind
1 1 3
 
Messaggi: 15
Iscritto il: 29 ott 2012, 18:13


Torna a Firmware e programmazione

Chi c’è in linea

Visitano il forum: Nessuno e 5 ospiti