Please Guide me to add log in and password

HI,
I’m trying to turn on/off a switch (led and servo) over internet in my house.

I have this code that I found, changed a bit that works just fine.
but If I want to have this port open/forwarded through my router, then anyone can access it.

I like to add login and password to this:

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
int led = 4;
Servo microservo;
int pos = 0;
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip = { 192, 168, 1, 69 }; // ip in lan (that’s what you need to use in your browser. (“192.168.1.69”)
byte gateway = { 192, 168, 1, 1 }; // internet access via router
byte subnet = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(led, OUTPUT);
microservo.attach(7);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}

//if HTTP request has ended
if (c == ‘\n’) {
Serial.println(readString); //print to serial monitor for debuging

client.println(“HTTP/1.1 200 OK”); //send new page
client.println(“Content-Type: text/html”);
client.println();
client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
client.println(“Java Site”);
client.println("");
client.println("");
client.println(“

JAVA

”);
client.println("
");
client.println("
“);
client.println(“

Command

”);
client.println(”
“);
client.println(”<a href="/?button1on"">Turn On LED");
client.println("<a href="/?button1off"">Turn Off LED
“);
client.println(”
“);
client.println(”
“);
client.println(”<a href="/?button2on"">Press Open");
client.println("<a href="/?button2off"">Press Close
“);
client.println(“

Created by Java

”);
client.println(”
“);
client.println(”");
client.println("");

delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") >0){
digitalWrite(led, HIGH);
}
if (readString.indexOf("?button1off") >0){
digitalWrite(led, LOW);
}
if (readString.indexOf("?button2on") >0){
for(pos = 0; pos < 180; pos += 3) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
microservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
if (readString.indexOf("?button2off") >0){
for(pos = 180; pos>=1; pos-=3) // goes from 180 degrees to 0 degrees
{
microservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
//clearing string for next read
readString="";

}
}
}
}
}

I found a webserver code that was with password, did not work.
I would appreciate if you have a code that I can insert this one, or anyone could modify it so it would be with log in and password before entering the page to access the keys.
by the way I have Arduino uno with ethernet shield.
I really, really, in advance appreciate it.
Thank you
Dave

Here's one method you could use. Have two separate pages.

One that has username and password input fields that the user has to fill in. Another that has username and password fields that are hidden but contain the correct username and password.

When you recieve a page request, you simply check for the presence of the correct name and password. If they are present and correct you send the second page. If not you send the first.

thank you so much for your reply.

how could I do that? what's the code that I can use?

or if you can redirect me to it please?

I think you should use POST data with a form. this guy here made something could fit for you with a couple of changes http://forum.arduino.cc/index.php?topic=87656.0

for further reference you could google "arduino receive post data"

hope it helps :)

sure.
I’ll check it.

i’ve this far and does not work, page shows up but does not respond to command:

#include <SPI.h>
#include <Ethernet.h>
int led = 4;
int pos = 0;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,69);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println(“new client”);
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
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) {
client.println("");
client.println("");
client.println("");
client.println("<form action==‘demo_form.asp’> ");

client.println("");
client.println("");
client.println("USER name:
");
client.println("PASSWORD:
");

client.println("");
client.println("");
client.println("");

client.println(“Content-Type: text/html”);
client.println();
client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
client.println(“Java Site”);
client.println("");
client.println("");
client.println(“

JAVA

”);
client.println("
");
client.println("
“);
client.println(“

Command

”);
client.println(”
“);
client.println(”<a href="/?button1on"">Turn On LED");
client.println("<a href="/?button1off"">Turn Off LED
“);
client.println(”
“);
client.println(”
“);
client.println(”<a href="/?button2on"">Press Open");
client.println("<a href="/?button2off"">Press Close
“);
client.println(“

Created by Java

”);
client.println(”
“);
client.println(”");
client.println("");

break;
}
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
}
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”);
}
}

hmmmmm

means "submit this form to the page ./demo_form.asp". I doubt you have this page on your current working directory even because you haven't a filesystem in this case.

looking at the page I linked you’ll see “<FORM ACTION=“http://192.168.1.102:84” METHOD=“post”>” where the ip is probably his arduino. you could even send to 127.0.0.1 (meaning loopback ip) so if everything else is correct should be <FORM ACTION=“http://127.0.0.1:80” METHOD=“post”>

you’re also writing random opening and closing form tags.

the logic should be:
read post data
IF username and password are present and valid
{
//show the logged in page

page content } ELSE { //show login page fill in form to login ... }

and just an hint just to be html5 compliant: use lowercase synthax like “<form action =“http://127.0.0.1:80” method=“post”>” (gives a more professional look to the code :))

you should anyway setup a web server on your computer to see if html and logic are ok an then report stuff over arduino just to speed up the debugging and development process

Simple server test code with a GET text box.

//zoomkat 12-08-12
//get submit box code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html or use a '
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84);; //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server text box test1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Pin 5 'on5' or 'off5': <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change Pin 5!'>");

          client.println("</FORM>");

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("on5") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off5") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Thank you very much. this was what I've been looking for.

thank you again.