Pages: [1] 2 3 4   Go Down
Author Topic: Problema login arduino ethernet  (Read 4278 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Come già detto nella sezione presentazione sono alle prime armi con arduino e quello che lo circonda.
Ieri mi è arrivato arduino uno ethernet, usando IDE 1.0.2 ho copiato il codice al seguente link
http://arduino.cc/forum/index.php?topic=53045.0,  e corretto
in true == login, cambiato ip, sottorete e i pin che uso sono 3, 5, 6.
Dopo eseguito il login se provo ad accendere uno dei tre led, (anche se premo direttamente su OFF) torno
alla pagina principale, dove appunto ho fatto il login con user e paswd, come se facessi back con il browser.
Però se alla fine del codice commento questa istruzione: readString="";
dopo il login riesco ad accendere i 3 led, ma non riesco a spegnerli.
Mentre se non uso il login riesco ad accendere e spegnere i 3 led senza problemi.
Per favore mi fate capire dove è il problema?
Logged

Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 112
Posts: 7112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Puoi postare il codice che usi tu. Nel topic indicato ci sono varie versioni.

E' uscito l'IDE 1.0.3 con alcune correzioni rispetto al 1.0.2 --> http://arduino.cc/en/Main/Software
« Last Edit: December 21, 2012, 03:13:53 pm by PaoloP » Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Questo è il codice che uso io:

Code:
#include <String.h>
#include <SPI.h>
#include <Ethernet.h>
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 220 }; // ip in lan
byte gateway[] = { 192, 168, 1, 220 }; // ip in lan
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
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;
 
void setup(){
Ethernet.begin(mac, ip, gateway, subnet);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, 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);
      if (c == '\n' && currentLineIsBlank) {
         Serial.print(readString);
        if(readString.indexOf("Nome=user&Pwd=pass") > 0) {
         login=true;
        } else {
         login=false;
        }
        if(login==true){
        if(readString.indexOf("L=1") > 0) {
          digitalWrite(3, HIGH); // set the LED on
          LED1ON = true;
          }else
          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;
          }else
          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;
          }else
          if(readString.indexOf("L=03") > 0)
          {
          //led has to be turned OFF
          digitalWrite(6, LOW); // set the LED OFF
          LED3ON = false;
        }
        }
        // INIZIO DICHIARAZIONE PAGINA HTML
        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>");
        // Finestra di login
        client.print("<form action='http://192.168.1.220/'>");
        client.print("Nome");    
        client.print("utente: <input name='Nome' value=''>");
        client.print("Password: <input type='Password' name='Pwd' value=''>");
        client.print("<input type='submit' value=' OK '>");
        client.print("</form>");
      
        if (login==true) {
        //Primo led
        client.println("<hr />");
        client.println("<h1>LED1</h1>");
        client.println("<br />");
        //printing LED status
        client.print("<span>STATO LED: </span>");
 
          if (LED1ON) {
              client.println("<span style='color:green'>ON</span>");
            }
            else
            {
              client.println("<span style='color:grey'>OFF</span>");
          }
        client.print("<h2><a href='/?L=1'>ON</a> | <a href='/?L=01'>OFF</a></h2>");
        
        client.println("<hr />");
        
        //Secondo led
        client.println("<h1>LED2</h1>");
        client.println("<br />");
        //printing LED status
        client.print("<span>STATO LED: </span>");
 
          if (LED2ON) {
              client.println("<span style='color:green'>ON</span>");
            }
            else
            {
              client.println("<span style='color:grey'>OFF</span>");
          }
        client.print("<h2><a href='/?L=2'>ON</a> | <a href='/?L=02'>OFF</a></h2>");
        client.println("<hr />");
        
        //terzo led
         client.println("<h1>LED3</h1>");
        client.println("<br />");
        //printing LED status
        client.print("<span>STATO LED: </span>");
 
          if (LED3ON) {
              client.println("<span style='color:green'>ON</span>");
            }
            else
            {
              client.println("<span style='color:grey'>OFF</span>");
          }
        client.print("<h2><a href='/?L=3'>ON</a> | <a href='/?L=03'>OFF</a></h2>");
        } //chiude if login=true
        client.println("</body></html>");
 
        //clearing string for next read
        readString="";
        //stopping client
        client.stop();
 
        }
    }
  }
}
}

grazie per la disponibilità
« Last Edit: December 22, 2012, 05:49:32 am by format » Logged

Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 112
Posts: 7112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Per prima cosa è sbagliata l'inizializzazione della Ethernet:
non è
Code:
Ethernet.begin(mac, ip, gateway, subnet);
ma è:
Code:
Ethernet.begin(mac, ip, gateway, gateway, subnet);
Il primo gatwae in effetti dovrebbe essere il server DNS ma nel 99% delle lan è uguale a gateway.
E' un errore frequentissimo --> http://arduino.cc/en/Reference/EthernetBegin
Quote
Syntax
Ethernet.begin(mac);
Ethernet.begin(mac, ip);
Ethernet.begin(mac, ip, dns);
Ethernet.begin(mac, ip, dns, gateway);
Ethernet.begin(mac, ip, dns, gateway, subnet);

Riedita il tuo messaggio precedente e inserisci i tag [code ]. Escono premendo il simbolo # sopra le faccine.
Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Infati non sapevo come si inserisce il codice nei tag...

cmq ho modificato anche in :
 Ethernet.begin(mac, ip, gateway, gateway, subnet);
e in:
Ethernet.begin(mac, ip, dns, gateway, subnet);

poi ho provato con solo mac e ip
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 220 }; // ip in lan
Ethernet.begin(mac, ip);

se commento questo:  // readString="";

dopo il login accendo i led ma non li spengo

altrimenti se non commento  readString="";
dopo il login se clicco su on oppure off torno all'inizio doce c'è il login

« Last Edit: December 22, 2012, 06:11:11 am by format » Logged

Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 112
Posts: 7112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ho separato il listato in modo da capire meglio quello che fa.
Ho inserito la funzione F in tutto il contenuto statico in modo da risparmiare memoria.

Code:
#include <String.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
  192, 168, 1, 220 }; // ip in lan
byte gateway[] = {
  192, 168, 1, 220 }; // ip in lan
byte subnet[] = {
  255, 255, 255, 0 }; //subnet mask
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;

void setup(){
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, 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);
        if (c == '\n' && currentLineIsBlank){
          WebPage(client);  
        }
      }
    }
  }
}

void WebPage (EthernetClient &client){
  Serial.print(readString);

  // Check Login
  if(readString.indexOf("Nome=user&Pwd=pass") > 0) {
    login=true;
  }
  else {
    login=false;
  }
  
  // Check Led
  if(login==true){
    if(readString.indexOf("L=1") > 0) {
      digitalWrite(3, HIGH); // set the LED on
      LED1ON = true;
    }
    else
      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;
    }
    else
      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;
    }
    else
      if(readString.indexOf("L=03") > 0)
      {
        //led has to be turned OFF
        digitalWrite(6, LOW); // set the LED OFF
        LED3ON = false;
      }
  }

  // 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
  client.print(F("<form action='http://192.168.1.220/'>"));
  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("<br />"));
    //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("<br />"));
    //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("<br />"));
    //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>"));
  } //chiude if login=true
  client.println(F("</body></html>"));

  //clearing string for next read
  readString="";
  //stopping client
  client.stop();
}

Secondo me c'è un errore nella logica di funzionamento poichè invii la pagina di login anche se è già stato effettuato.
Dovresti mettere un if:
Code:
 if (login==false) {
 // Finestra di login
  client.print(F("<form action='http://192.168.1.220/'>"));
  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>"));
  }
« Last Edit: December 22, 2012, 08:59:33 am by PaoloP » Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ho provato il nuovo codice, ma si comporta sempre allo stesso modo...
Logged

Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 112
Posts: 7112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Si, infatti non ho inserito l'if sul login.
Dovresti modificarlo tu.  smiley-wink
Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

no l' if c'è:
// Finestra di login
  if (login==false) {
  client.print(F("<form action='http://192.168.1.220/'>"));
  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>"));
}
Logged

Genova
Offline Offline
Faraday Member
**
Karma: 39
Posts: 3390
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ci sarebbe anche la lib base64 per le login non che sia una sicurezza,  oppure prova una  stringa md5 a 32 caratteri.
O ancora .... Scrivi 401 al posto di 200 il client ti apre la maschera di login user e passw
Ciao
Logged

no comment

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ho messo 401 al posto di 200 ma il login è sempre uguale...
Io ho anche scaricato la libreria WebServerAuth (da come ho capito si basa su Webduino)
ma non ho idea di come si usa.

Ho anche provato a passare la stringa tipo:
http://192.168.1.220/?L=1
dal controllo Web Browser tramite una form che ho fatto con VB.net, alla fine ho lo stesso risultato
riesco a accendere i led ma non li spengo.Se funzionava con il VB .net avevo risolto il login non mi serviva più
Logged

Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 112
Posts: 7112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ho tolto gli else dagli if dei led
Code:
#include <String.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
  192, 168, 1, 220 }; // ip in lan
byte gateway[] = {
  192, 168, 1, 220 }; // ip in lan
byte subnet[] = {
  255, 255, 255, 0 }; //subnet mask
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;

void setup(){
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, 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);
        if (c == '\n' && currentLineIsBlank){
          Serial.println(readString);
          WebPage(client);
          //clearing string for next read
          readString="";
          //stopping client
          client.stop(); 
        }
      }
    }
  }
}


void WebPage (EthernetClient &client){

  // Check Login
  if(readString.indexOf("Nome=user&Pwd=pass") > 0) {
    login=true;
    Serial.println("Login OK ");
  }
  else {
    login=false;
    Serial.println("Login ERR");
  }

  // Check Led
  if(login==true){
    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;
    }
  }

  // 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.1.220/'>"));
    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("<br />"));
    //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("<br />"));
    //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("<br />"));
    //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>"));
  } //chiude if login=true
  client.println(F("</body></html>"));
}
Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Niente da fare nemmeno senza gli else
Logged

Cagliari, Italy
Offline Offline
Tesla Member
***
Karma: 112
Posts: 7112
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sul serial monitor cosa visualizza?
Logged

Code fast. Code easy. Codebender --> http://codebender.cc/?referrer=PaoloP

Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dopo il login ho cliccato su ON per accendere il led e mi ritorna alla pagina principale, però c'è un errore sul serial monitor:
GET / HTTP/1.1

Login ERR
GET /?Nome=user&Pwd=pass HTTP/1.1

Login OK
GET /?L=1 HTTP/1.1

Login ERR
Logged

Pages: [1] 2 3 4   Go Up
Jump to: