Robot Web Based Remote Control

I have been working a lot lately on making it so that I can control Salvius using a wireless internet connection. I still have not finished hooking up the motors and the motor controller but I have managed to get the wireless control code working enough to turn the robot's headlights on and off. More on the relay shield that I used can be found here: http://www.seeedstudio.com/wiki/index.php?title=Relay_Shield


And as always because Salvius is an open source robot here is the code as seen working in the video:


/*  Programmer: Gunther Cox
    http://robotrecycled.blogspot.com
    Created: 12/25/11
    Last Modified: 02/25/12
    Hardware setup instructions:
 
    Plug Ethernet shield into Arduino Uno.
    Plug Seeed Relay Shield into Ethernet Shield.
    Attach 9 volt power supply to GND and +9V plugs of Relay Shield.
    PIR + to arduino 5V, PIR - to arduino GND, PIR OUT to HUB2 D3.
    Connect ethernt shield to wireless router with ethernet cable.
     
    Parts of this code is based on work done by others:
    * Button controls based on code by Minde
    * Navigation buttons based on work by Su Ray Sh
*/

#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>

// change these to fit your setup:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x00, 0x0F }; // physical mac address
byte ip[] = { 192, 168, 1, 177 };             // ip in lan
byte gateway[] = { 192, 168, 0, 1 };     // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                //subnet mask

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

byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)

int pirPin = 3;    // input pin (for PIR sensor)
int relayPin1 = 4; // relay connected to digital pin 4 (D3 on relay shield - on switch)
int relayPin2 = 7; // relay connected to digital pin 5 (D2 on relay shield) - Inferred LEDs
int relayPin3 = 6; // relay connected to digital pin 6 (D1 on relay shield) - Ultraviolte LEDs
int relayPin4 = 5; // relay connected to digital pin 7 (D0 on relay shield) (not used)
//pin 10 - reserved for ethernet shield
//pin 11 - reserved for ethernet shield
//pin 12 - reserved for ethernet shield
//pin 13 - reserved for ethernet shield

// Begin variables for motion sensor
//int pirState = LOW; // we start, assuming no motion detected
//int val;            // variable for reading the pin status
//int pirMode = 0;    // determines if the relay closed already (0 = not closed yet)
// End variabled for motion sensor

String readString = String(30); //string for fetching data from address
boolean FORWARD = false; //relay status flag
boolean BACKWARD = false; //relay status flag
boolean LEFT = false; //relay status flag
boolean RIGHT = false; //relay status flag
boolean LEDON = false; //LED status flag
boolean LED2ON = false; //LED status flag
void setup(){
//start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  pinMode(relayPin3, OUTPUT);  //Set pin 7 to output
  pinMode(relayPin4, OUTPUT);

//  pinMode(pirPin, INPUT);        // declare sensor as input
  pinMode(relayPin1, OUTPUT);    // declare relay1 as output
  pinMode(relayPin2, OUTPUT);    // declare relay2 as output
  pinMode(relayPin3, OUTPUT);    // declare relay3 as output
  pinMode(relayPin4, OUTPUT);    // declare relay4 as output

  //digitalWrite(relayPin1, LOW);
  //digitalWrite(pirPin, LOW);
  //pirState = digitalRead(pirPin); // read the initial state
  //delay(60000);                   // pause before calibrating
}

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) {
        readString += c; //store characters to string
      }
     
        if (c == '\n') {  //if HTTP request has ended
       
          if (readString.indexOf("?") <0) //dirty skip of "GET /favicon.ico HTTP/1.1"
          {
            //skip everything
          }
          else
       
          // begin drive control
          if(readString.indexOf("N1=Forward") >0)
           {
             //item has to be turned ON
             digitalWrite(relayPin1, HIGH);    // set the item on
             FORWARD = true;
           }else{
             //item has to be turned OFF
             digitalWrite(relayPin1, LOW);    // set the item off
             FORWARD = false;          
           }
         
           if(readString.indexOf("N2=Backward") >0)
           {
             //item has to be turned ON
             digitalWrite(relayPin2, HIGH);    // set the item on
             BACKWARD = true;
           }else{
             //item has to be turned OFF
             digitalWrite(relayPin2, LOW);    // set the item off
             BACKWARD = false;          
           }
         
           if(readString.indexOf("N3=LEFT") >0)
           {
             //item has to be turned ON
             digitalWrite(relayPin3, HIGH);    // set the item on
             LEFT = true;
           }else{
             //item has to be turned OFF
             digitalWrite(relayPin3, LOW);    // set the item off
             LEFT = false;          
           }
         
           if(readString.indexOf("N4=RIGHT") >0)
           {
             //item has to be turned ON
             digitalWrite(relayPin4, HIGH);    // set the item on
             RIGHT = true;
           }else{
             //item has to be turned OFF
             digitalWrite(relayPin4, LOW);    // set the item off
             RIGHT = false;          
           }
          // end drive control
       
          //lets check if LED should be lighted
        if(readString.indexOf("L=ON") >0)//replaces if(readString.contains("L=1"))
           {
             //led has to be turned ON
             digitalWrite(relayPin3, HIGH);    // set the LED on
             LEDON = true;
           }
           else if(readString.indexOf("L2=ON") >0)//replaces if(readString.contains("L2=1"))
           {
             //led has to be turned ON
             digitalWrite(relayPin4, HIGH);    // set the LED on
             LED2ON = true;
           }
           //led has to be turned OFF
           else{
             //led has to be turned OFF
             digitalWrite(relayPin4, LOW);    // set the LED OFF
             LED2ON = false;
             digitalWrite(relayPin3, 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.println("<body style=background-color:#FFB880><h1>Web Remote</h1><hr />"); // background color and heading
       
   client.println("<h2>Navigation</h2>");
   client.println("<form method=get name=NAV><table CELLPADDING='10' style=background-color:#CC6600>");
   client.println("<tr><td align=center></td><td align=center><input type=submit name=N1 value=FORWARD></td> <td align=center></td></tr>");
   client.println("<tr><td align=center><input type=submit name=N3 value=LEFT></td><td align=center><input type=submit value=STOP></td><td align=center><input type=submit name=N4 value=RIGHT></td></tr>");
   client.println("<tr><td align=center></td><td align=center><input type=submit name=N2 value=BACKWARD></td> <td align=center></td></tr>");
                  //controlling leds via buttons (address will look like http://192.168.1.177/?L=ON when submited)
          if (LEDON)
          client.println("<tr><td align=center><form method=get name=LED><input type=submit value=OFF></form></td>");
          else
          client.println("<tr><td align=center><form method=get name=LED><input type=submit name=L value=ON></form></td>");

          if (LED2ON)  // begin second LED
          client.println("<td align=center><form method=get name=LED2><input type=submit value=OFF></form></td></tr>");
          else
          client.println("<td align=center><form method=get name=LED2><input type=submit name=L2 value=ON></form></td></tr>");
   client.println("</table></form>");
     
          // Begin print drive status
          if (FORWARD)
              client.println("<font size='4'>DRIVING FORWARD</font>");
           
          else if (BACKWARD)
              client.println("<font size='4'>DRIVING BACKWARD</font>");

          else if (LEFT)
              client.println("<font size='4'>TURNING LEFT</font>");

          else if (RIGHT)
              client.println("<font size='4'>TURNING RIGHT</font>");
           
          else
          client.println("<font size='4'>PROBABLY NOT DRIVING</font>");
          // End print drive status
           
          //printing LED status
          client.print("<font size='4'>LED status: ");
          if (LEDON)
              client.println("<font color='purple' size='4'>ULTRAVIOLET ON</font>");
           
          else if (LED2ON)
              client.println("<font color='red' size='4'>INFERRED ON</font>");
           
          else
          client.println("<font color='grey' size='4'>OFF</font>");
          // end second LED
       
          client.println("<hr />");
          client.println("<hr />");
          client.println("</body></html>");
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
            }
          }
        }
      }
 }

//void loop() {
//  // code to do auto activation
//  val = digitalRead(pirPin);  // read input value and store it in val
//  if(digitalRead(pirPin) == HIGH) {
//    if (pirMode == 0) {
//      if (val != pirState) {            //look for 2 inconsistant readings
//          // turn main computer on
//        digitalWrite(relayPin1, HIGH);  // sets relay1 on
//        delay(1000);                    // waits for 1 second
//        digitalWrite(relayPin1, LOW);   // sets relay1 off
//        pirMode = pirMode + 1;   // relay has been on    
//        //delay(50);
//      }    
//    }
//  }
//}
◄ Newer Post Older Post ►
 

Copyright 2011 robot recycled parts is proudly powered by blogger.com