Go Down

Topic: Webduino authentication and webserver servo control ? (Read 2323 times) previous topic - next topic

zhirantaha

hi
i want to combine two code
when i logged as admin see the webserver control
here the codes
Code: [Select]
//zoomkat 7-26-12
//simple button GET server code to control servo and arduino pins 5, 6, and 7
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//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
int pos = 0;
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.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(80); //server port

String readString;
Servo myservo;
//////////////////////
int toogle = 1;
void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  pinMode(2, OUTPUT); //pin selected to control
  pinMode(7, 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 multi LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
   myservo.attach(9);
   myservo.write(0);
  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("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
            client.println("<<img src="upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Rotating_earth_(large).gif/200px-Rotating_earth_(large).gif" alt="some_text"/>>");
          // DIY buttons
          client.println("<a href=\"/?on2\"\">ON</a>");
          client.println("<a href=\"/?off3\"\">OFF</a><br />");


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

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

          ///////////////////// control arduino pin
          if(readString.indexOf('2') >0)//checks for 2
          {
           myservo.write(90);
           delay(2100);
                   
 
            Serial.println("Led 5 On");
}
          if(readString.indexOf('3') >0)//checks for 3
          {
                 myservo.write(0);
          delay(1000);     // set pin 5 low
            Serial.println("Led 5 Off");
          }
         
          if(readString.indexOf('4') >0)//checks for 4
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
          }
          if(readString.indexOf('5') >0)//checks for 5
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
          }
           if(readString.indexOf('6') >0)//checks for 6
          {
            digitalWrite(7, HIGH);    // set pin 7 high
            Serial.println("Led 7 On");
          }
          if(readString.indexOf('7') >0)//checks for 7
          {
            digitalWrite(7, LOW);    // set pin 7 low
            Serial.println("Led 7 Off");
          }         
         
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}


and
Code: [Select]
/* 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);
}

please help

PaulS

What do the two sketches do? What should the combined sketch do? Where is your attempt to combine them? What was wrong with that attempt?

When is our homework due?

zhirantaha

I said wen you log in as admin you can control the servo, led etc
show web server code in admin privtae.html page

zhirantaha



zhirantaha


mkcinek

hi,

I am looking for the sam solution, I want to secure my arduino with 8 relays from unauthorized access from outside with a login and password.

How did you include Webduino authentication as a string ?

Can you present the code pease ?

zhirantaha

Code: [Select]
//Code by zhiran.taha@yahoo.com


#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, 178}; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // ip in lan
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString; //
boolean login=false;

/////////////////////////////////
void setup(){
Ethernet.begin(mac, ip);
Serial.begin(9600);
    Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  login = false;
}
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("User=user&Pass=user") > 0) {
login=true;
        }
           
        ////////////////////////////////////////////////////////////////////////////
       
        if(login==true){
     
        if(readString.indexOf("Logout") > 0) {

          login=false;
     
          }     
        }
        /////////////////////////////////////////////////////
if (login == false) {
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();
        client.print("<html>");
        client.println("<meta http-equiv=\"refresh\" content=\"60\">");
       client.println(" <head><title>website title</title><center></head><body> ");
       client.println("<h1>Welcome to Website</h1><hr />");
        if (login == false) {
        client.println("<h1>Please login to control!</h1>");
        client.print("<form action='192.168.1.178/'>");
        client.print("");
        client.print("Username: <input name='User' value=''>");
        client.print("Password: <input type='Password' name='Pass' value=''>");
        client.print("<input type='submit' value=' Login '>");
        client.print("<hr /></form><hr />");
               
        }
}
        ////////////////////////////////////////////////////////////////////////////
        if (login==true) {
      client.print("you get access");
        client.print("<h2><a href='/?Logout'>Logout</a>");
        client.println("<hr />");
        client.print("<span><strong>Project by Zhiran</strong?</span>");
       
        } //chiude if login=true
        client.println("</center></body></html>");

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

        }
    }
  }
}

}


Go Up