Qualcuno sa come inserire una password per il login da web su questo sketch ? 
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 177 };
EthernetServer server(80);
String readString;
byte out_1 = 2;
byte out_2 = 3;
void setup()
{
Ethernet.begin(mac, ip);
pinMode(out_1, OUTPUT);
pinMode(out_2, OUTPUT);
}
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("on_1") > 0) digitalWrite(out_1, HIGH);
if(readString.indexOf("off_1") > 0) digitalWrite(out_1, LOW);
if(readString.indexOf("on_2") > 0) digitalWrite(out_2, HIGH);
if(readString.indexOf("off_2") > 0) digitalWrite(out_2, LOW);
// PAGINA HTML
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><head><title>ARDUINO Controllo WEB</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ></head><body>");
client.print("
");
client.print("<span>Tasto 1 </span>");
if (digitalRead(out_1)== true) client.print("<input disabled=\"disabled\" maxLength=\"50\" size=\"12\" value=\"ON\" STYLE=\"background-color:#00FF00\"/>");
else client.print("<input disabled=\"disabled\" maxLength=\"50\" size=\"12\" value=\"OFF\" STYLE=\"background-color:#C0C0C0\"/>");
client.print("<p><p/>");
client.print("<input type=\"button\" style=\"width:120px; height:80px\" value=\"On\" onclick =\" location.href='/?on_1'\">");
client.print("<input type=\"button\" style=\"width:120px; height:80px\" value=\"Off\" onclick =\" location.href='/?off_1'\">");
client.print("<p><p/>");
client.print("<span>Tasto 2 </span>");
if (digitalRead(out_2)== true) client.print("<input disabled=\"disabled\" maxLength=\"50\" size=\"12\" value=\"ON\" STYLE=\"background-color:#00FF00\"/>");
else client.print("<input disabled=\"disabled\" maxLength=\"50\" size=\"12\" value=\"OFF\" STYLE=\"background-color:#C0C0C0\"/>");
client.print("<p><p/>");
client.print("<input type=\"button\" style=\"width:120px; height:80px\" value=\"On\" onclick =\" location.href='/?on_2'\">");
client.print("<input type=\"button\" style=\"width:120px; height:80px\" value=\"Off\" onclick =\" location.href='/?off_2'\">");
client.print("<p><p/>");
client.print("<p><p/>");
client.print("</body></html>");
readString="";
delay(1);
client.flush();
client.stop();
}
}
}
}
}
premetto che non sono convinto di aver inteso la domanda
Io ho capito che l'idea è quella di accedere alla pagina web "riservata" dopo aver inserito le credenziali in un form.
Non so se che l'HTML su Arduino consenta il passaggio di variabili tra una pagina e l'altra (che sarebbe sempre la stessa, ma con un flusso di tag diverso a seconda che si tratti di un form login/pwd o della pagina riservata a seguito verifica).
Login/pwd potrebbero essere memorizzate nel codice Arduino, un po' come se fosse php, asp ecc. Quindi anche scaricando il sorgente della pagina, non si troverebbe traccia delle credenziali.
Rimane il dubbio su come inserire codice html che consenta di leggere una stringa passata tra 2 pagine html (Method GET e POST e le variabili | Guida PHP | HTML.it), cioè se Arduino interpreta solo i TAG HTML, come credo.
Tipo:
print INTESTAZIONI
--------------> se (credenziali non vuote AND coincidono a "xyz")
print LA_TUA_PAGINA_RISERVATA
altrimenti
print PAGINA_CON_FORM_DI_LOGIN (vale per il primo accesso alla pagina o per credenziali non corrette)
print FOOT_PAGINA
Credo anche che non esista il concetto di sessione, per cui ogni volta che la pagina si ricarica (banalmente perchè si inserisce un refresh automatico), le credenziali verrebbe richieste nuovamente (--> alternativa: simulare una "sessione Arduino" con un timeout interno, oltre il quale si mette a false una variabile "utente riconosciuto")
==============================================
EDIT: Arduino Playground - HomePage
Pages on flash memory
There are 5 pages in this sketch (page 1, 2, 3, 4 and login). In every page, on the top, there is a menu that allows a web user to select what page he/she wants to see.
Ho trovato questa libreria che fa a il caso mio è funziona, ora come ci inserisco la html che ho postato prima
Questo è un esempio
/* Web_Authentication.ino - Webduino Authentication example */
/* This example assumes that you're familiar with the basics
* of the Ethernet library (particularly with setting MAC and
* IP addresses) and with the basics of Webduino. If you
* haven't had a look at the HelloWorld example you should
* probably check it out first */
/* you can change the authentication realm by defining
* WEBDUINO_AUTH_REALM before including WebServer.h */
#define WEBDUINO_AUTH_REALM "Weduino Authentication Example"
#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
/* CHANGE THIS TO YOUR OWN UNIQUE VALUE. The MAC number should be
* different from any other devices on your network or you'll have
* problems receiving packets. */
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
/* CHANGE THIS TO MATCH YOUR HOST NETWORK. Most home networks are in
* the 192.168.0.XXX or 192.168.1.XXX subrange. Pick an address
* that's not in use and isn't going to be automatically allocated by
* DHCP from your router. */
static uint8_t ip[] = { 192, 168, 1, 210 };
/* This creates an instance of the webserver. By specifying a prefix
* of "", all pages will be at the root of the server. */
#define PREFIX ""
WebServer webserver(PREFIX, 80);
void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
server.httpSuccess();
if (type != WebServer::HEAD)
{
P(helloMsg) = "<h1>Hello, World!</h1><a href=\"private.html\">Private page</a>";
server.printP(helloMsg);
}
}
void privateCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
/* if the user has requested this page using the following credentials
* username = user
* password = user
* display a page saying "Hello User"
*
* the credentials have to be concatenated with a colon like
* username:password
* and encoded using Base64 - this should be done outside of your Arduino
* to be easy on your resources
*
* in other words: "dXNlcjp1c2Vy" is the Base64 representation of "user:user"
*
* if you need to change the username/password dynamically please search
* the web for a Base64 library */
if (server.checkCredentials("dXNlcjp1c2Vy"))
{
server.httpSuccess();
if (type != WebServer::HEAD)
{
P(helloMsg) = "<h1>Hello User</h1>";
server.printP(helloMsg);
}
}
/* if the user has requested this page using the following credentials
* username = admin
* password = admin
* display a page saying "Hello Admin"
*
* in other words: "YWRtaW46YWRtaW4=" is the Base64 representation of "admin:admin" */
else if (server.checkCredentials("YWRtaW46YWRtaW4="))
{
server.httpSuccess();
if (type != WebServer::HEAD)
{
P(helloMsg) = "<h1>Hello Admin</h1>";
server.printP(helloMsg);
}
}
else
{
/* send a 401 error back causing the web browser to prompt the user for credentials */
server.httpUnauthorized();
}
}
void setup()
{
Ethernet.begin(mac, ip);
webserver.setDefaultCommand(&defaultCmd);
webserver.addCommand("index.html", &defaultCmd);
webserver.addCommand("private.html", &privateCmd);
webserver.begin();
}
void loop()
{
char buff[64];
int len = 64;
/* process incoming connections one at a time forever */
webserver.processConnection(buff, &len);
}
x iscrizione. Mi interessano le possibili risposte.
puoi usare il code HTTP 1.1 401 che chiede le credenziali invece di HTTP 1.1 200 OK
Ho risolto modificando questo sketch che ho trovato sul forum
,che funziona a meraviglia
posto il codice che puo essere di aiuto ad altri
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 177);
EthernetServer server(80);
String readString;
byte out_1 = 2;
void setup() {
Serial.begin(9600);
//while (!Serial) {
// ;
//}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(out_1, OUTPUT);
}
void SendOKpage(EthernetClient &client)
{
// send a standard http response header
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 WEB</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ></head><body>"));
client.print(F("
"));
client.print(F("<span>STATO LUCE 1 </span>"));
if (digitalRead(out_1)== true) client.print(F("<input disabled=\"disabled\" maxLength=\"50\" size=\"12\" value=\"ON\" STYLE=\"background-color:#00FF00\"/>"));
else client.print(F("<input disabled=\"disabled\" maxLength=\"50\" size=\"12\" value=\"OFF\" STYLE=\"background-color:#C0C0C0\"/>"));
client.print(F("<p><p/>"));
client.print(F("<input type=\"button\" style=\"width:120px; height:80px\" value=\"On\" onclick =\" location.href='/?on_1'\">"));
client.print(F("<input type=\"button\" style=\"width:120px; height:80px\" value=\"Off\" onclick =\" location.href='/?off_1'\">"));
client.print(F("<p><p/>"));
client.print(F("</body></html>"));
readString="";
delay(1);
//client.flush();
//client.println(F("Connnection: close"));
//client.println();
//client.println(F("<!DOCTYPE HTML>"));
//client.println(F("<html>"));
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
/*
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
*/
}
void SendAuthentificationpage(EthernetClient &client)
{
client.println("HTTP/1.1 401 Authorization Required");
client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<HTML> <HEAD> <TITLE>Error</TITLE>");
client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}
char linebuf[80];
int charcount=0;
boolean authentificated=false;
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
//---------------------------aggiunta------------------------------------------
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("on_1") > 0) digitalWrite(out_1, HIGH);
if(readString.indexOf("off_1") > 0) digitalWrite(out_1, LOW);
//--------------------------------------------------------------------------------
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
authentificated=false;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
if (authentificated)
SendOKpage(client);
else
SendAuthentificationpage(client);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
//user-user
if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"dXNlcjp1c2Vy")>0)
authentificated=true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
}
}
}
}
non hai detto che il login è user:user, non può funzionare a meraviglia, mi sa che dovresti rivederlo
lo provato ,funziona, se non dicito user e user non mi apre la pagina
Non avendo l'ethernet, ho modificato l'ultimo sketch per la wifi e a me sembra che funzioni.
Il code 401 è un po' noioso da usare, (fidatevi ci sono stato molto tempo sopra)
ha diversi comportamenti in risposta ai 4 possibili giochetti di un utente,
- user e pass errati con relativa pressione OK
- user e pass corretti con relativa pressione del tasto OK
- campi user e pass vuoti con relativa pressione OK
- campi user e pass vuoti o riempiti con pressione tasto ANNULLA
provate a stressarlo un po' con le differenti casualità, leggete cosa accade sul serial quando premete annulla (i 2 non comunicano mentre uno aspetta l'altro) ... ci vogliono pochi passaggi per leggere "La pagina non risponde" e correre a casa a resettarlo 
Inoltre in questo sketch una volta abilitata l'autenticazione il server è aperto a tutti, una nuova connessione da un altro client non chiede più le credenziali, del resto se la variabile boolean authentificated=true venisse messa false, a ogni refresh chiederebbe la password e non va bene.
Per fargliela chiedere di nuovo bisogna resettarlo, e non va bene.
e ancora, alla disconnessione dell'utente (chiusura del browser) la variab boolean authentificated=true dovrebbe essere portata in false in tal modo, quando ci ricollega in un secondo momento vengono di nuovo chieste le credenziali.
Questo non è possibile farlo essendo un webserver che apre e chiude continuamente la connessione. L'unico pagliativo è inserire un timer, che se entro 30 sec non arrivano più richieste dal client la variabile authentificated viene portata in false.
Il server resta scoperto per 30 sec, sarebbe comunque meglio di niente.
Oppure aggiungere 2 righe di javascript che scrivono un cookie con una chiave di riconoscimento che viene distrutto alla chiusura del browser.
Per farlo bene ci vuole una manipolazione dei socket dove si identifica l'ip del client, le porte ecc senza client.stop (che comporta molte complicazioni)
Fatto così, mio commento personale, non serve a una cippa
, il grado di sicurezza dell' webserver è pari a 0
ciao
un altro trucco è di inserire un campo nascosto/coockie (e quì nasce la gestione cookie.... auguri) che viene inserito da arduino in ogni pagina, se il codice verificato è arrato il client viene disconnesso. Visto che non si possono identificare univocamente i client, allora sarà da usare un valore unico per tutti... e quindi va cambiato ogni tot (diciamo mezzora), il che costringe tutti i client a riautenticarsi, ma almeno si evita che una pagina in cache possa accedere senza uare user+pass (se più vecchia del cambio codice)