Tuesday, November 24, 2015

Ethernet Control Experiment

The Web Server



This robot is controlled using your web server.




ARDUINO SKETCH:

/* ===============================================================
      Project Homotronics: Robot controlled by Ethernet
      Author: J. B. Wylzan with libraries from the Arduino website
      Website: http://www.iHackRobot.blogspot.com
      Abstract: Control a robot via the ethernet.
================================================================== */
#include <SPI.h>
#include <Ethernet.h>

const int MLWF = 4;             
const int MLBB = 7;  
const int MRYF = 6;             
const int MRRB = 5;  

byte mac[] = { your mac address };   
byte ip[] = { your ip address  };                      
byte gateway[] = { 192, 168, x, x };                  
byte subnet[] = { 255, 255, x, x };                 
EthernetServer server(80);                             //or 8081     
String gString;

void setup() {

  Serial.begin(9600);

  pinMode(MLWF, OUTPUT);  
  pinMode(MLBB, OUTPUT); 
  pinMode(MRYF, OUTPUT); 
  pinMode(MRRB, OUTPUT);
  
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("my server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  
  EthernetClient client = server.available();
  if (client == true) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
        if (gString.length() < 100) {
          gString = gString + c;
         }

         if (c == '\n') {          
           Serial.println(gString); 
     
           client.println("HTTP/1.1 200 OK"); 
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<!DOCTYPE HTML>");
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<TITLE>iHackLab</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY bgcolor='#ff4d4d'>");
          client.println("<center>");
           client.println("<H1>iHackLab Homotronics Website</H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<H2>Robotics with Arduino Ethernet Shield</H2>");
           client.println("<br />");  
           client.println("<a href=\"/fwd\"\">Move Forward</a>");
              client.println("<br />");   
               client.println("<br />");  
           client.println("<a href=\"/rvs\"\">Move Backward</a><br />");   
           client.println("<br />"); 
            client.println("<br />");  
           client.println("<a href=\"/rht\"\">Turn Right</a>");
              client.println("<br />");   
               client.println("<br />");  
           client.println("<a href=\"/lft\"\">Turn Left</a><br />"); 
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<br />");    
           client.println("<a href=\"/stp\"\">Stop</a><br />"); 
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<br />");   
           client.println("<hr />");
           client.println("<www.myhackspaceproject.blogspot.com>"); 
           client.println("<hr />");
           client.println("<br />"); 
           client.println("</center>");
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);  //page loading delay
           client.stop();
          Serial.println("client disconnected");

           if (gString.indexOf("fwd") >0){
               Serial.println("Forward");
        digitalWrite(MLBB, 0);        
    digitalWrite(MLWF, 255);
    digitalWrite(MRRB, LOW);        
    digitalWrite(MRYF, HIGH);
           }
           if (gString.indexOf("rvs") >0){
                Serial.println("Reverse");
         digitalWrite(MLBB, 255);         
    digitalWrite(MLWF, 0);
    digitalWrite(MRRB, HIGH);        
    digitalWrite(MRYF, LOW);
           }
           if (gString.indexOf("lft") >0){
                 Serial.println("Left");
        digitalWrite(MLBB, 0);        
    digitalWrite(MLWF, 255);
    digitalWrite(MRRB, LOW);        
    digitalWrite(MRYF, LOW);
           }
           if (gString.indexOf("rht") >0){
              Serial.println("Right");
         digitalWrite(MLBB, 0);        
    digitalWrite(MLWF, 0);
    digitalWrite(MRRB, LOW);        
    digitalWrite(MRYF, HIGH);
           }
           if (gString.indexOf("stp") >0){
               Serial.println("Stop");
        digitalWrite(MLBB, 0);        
    digitalWrite(MLWF, 0);
    digitalWrite(MRRB, LOW);        
    digitalWrite(MRYF, LOW);
           }
            gString="";  
           
         }
       }
    }
}

/* ================================================================== */
 Circuit: (optional)

 * Ethernet shield prepares to be attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5
 * On-board SS micro-SD card slot must be on pin 4 

The shield contains a number of informational LEDs: 
  • PWR: indicates that the board and shield are powered 
  • LINK: indicates the presence of a network link and flashes when the shield transmits or receives data
  • FULLD: indicates that the network connection is full duplex 
  • 100M: indicates the presence of a 100 Mb/s network connection (as opposed to 10 Mb/s) 
  • RX: flashes when the shield receives data 
  • TX: flashes when the shield sends data 
  • COLL: flashes when network collisions are detected.



 ==================================================================
"Information comes in and comes out as information" 
~ Joey Lawsin
==================================================================

NOTICE: Articles on this site are composed on random thoughts. The transcript may not be in its final form. It maybe edited, updated or even revised in the future based on the outcomes of  the author's experiments.

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled Biotronics: The Silver Species. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: The Biotronics Project, Joey Lawsin, 1988, USA.

================================================================== 
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®
 ==================================================================





No comments:

Post a Comment