Controlling robot over a wireless internet connection
This is how I made my robot [Salvius] able to be controlled using a wireless internet connection. The process is simple and costs very little if you don't have the parts already. One of the best things about using this method to control your robot is that you don't even need a specific remote control. You can use any wireless enabled device like a phone, iPod, tablet, or laptop computer to control your robot. Also, there will be no need for any programs to be installed on the device you are using as a remote because everything is installed on the robot in a similar configuration to a regular web server. All you have to do is type a URL in your address bar and press enter.
For this project you will need:
I purchased these parts from Trossen Robotics. They are an online vendor for robotic parts and I have found that they have a great selection. You can visit their site at: http://www.trossenrobotics.com.
WiFi-Enable Your Robot!
For this project you will need:
I purchased these parts from Trossen Robotics. They are an online vendor for robotic parts and I have found that they have a great selection. You can visit their site at: http://www.trossenrobotics.com.
- Arduino Uno ( http://www.trossenrobotics.com/p/arduino-uno.aspx )
- Arduino Ethernet Shield ( http://www.trossenrobotics.com/p/arduino-ethernet-shield.aspx )
For this example I will be showing you how to simply turn an LED on and off by using a web browser. I will post updates for doing this with more complex parts like motor controllers in a later post.
Here is a picture of this working:
In the photos the arduino is programmed to render a web page that has two buttons; one turns the LED on and the other turns it off.
Here is the code for the Arduino:
/*
The circuit:
* Arduino Uno
* Arduino Ethernet shield
* LED connected to GND and pin 5
This code is based on work done by Minde
http://www.sciencprog.com/
*/
#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
EthernetServer server(80); //server port
byte sampledata=50; //some sample data - outputs 2 (ascii = 50 DEC)
int ledPin = 5; // LED pin
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
pinMode(ledPin, OUTPUT); //Set pin 4 to output
Serial.begin(9600); //enable serial data print
}
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; //replaces readString.append(c);
}
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
//dirty skip of "GET /favicon.ico HTTP/1.1"
if (readString.indexOf("?") <0)
{
//skip everything
}
else
//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(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
//led has to be turned OFF
digitalWrite(ledPin, 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.print("<body style=background-color:#FFB880>"); // background color
client.println("<h1>Web Remote</h1>"); //send first heading
client.println("<hr />");
//controlling led via checkbox
client.println("<h2>LED Control</h2>");
//address will look like http://192.168.1.110/?L=1 when submited
if (LEDON)
client.println("<form method=get name=LED><input type=submit name=L value=ON CHECKED><input type=submit value=OFF></form>");
else
client.println("<form method=get name=LED><input type=submit name=L value=ON><input type=submit value=OFF></form>");
//printing LED status
client.print("<font size='4'>LED status: ");
if (LEDON)
client.println("<font color='green' size='4'>ON");
else
client.println("<font color='grey' size='4'>OFF");
client.println("<hr />");
client.println("<hr />");
client.println("</body></html>");
//clearing string for next read
readString="";
//stopping client
client.stop();
}
}
}
}
}
Here is a picture of this working:
LED off |
LED on |
Here is the code for the Arduino:
/*
The circuit:
* Arduino Uno
* Arduino Ethernet shield
* LED connected to GND and pin 5
This code is based on work done by Minde
http://www.sciencprog.com/
*/
#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
EthernetServer server(80); //server port
byte sampledata=50; //some sample data - outputs 2 (ascii = 50 DEC)
int ledPin = 5; // LED pin
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
pinMode(ledPin, OUTPUT); //Set pin 4 to output
Serial.begin(9600); //enable serial data print
}
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; //replaces readString.append(c);
}
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
//dirty skip of "GET /favicon.ico HTTP/1.1"
if (readString.indexOf("?") <0)
{
//skip everything
}
else
//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(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
//led has to be turned OFF
digitalWrite(ledPin, 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.print("<body style=background-color:#FFB880>"); // background color
client.println("<h1>Web Remote</h1>"); //send first heading
client.println("<hr />");
//controlling led via checkbox
client.println("<h2>LED Control</h2>");
//address will look like http://192.168.1.110/?L=1 when submited
if (LEDON)
client.println("<form method=get name=LED><input type=submit name=L value=ON CHECKED><input type=submit value=OFF></form>");
else
client.println("<form method=get name=LED><input type=submit name=L value=ON><input type=submit value=OFF></form>");
//printing LED status
client.print("<font size='4'>LED status: ");
if (LEDON)
client.println("<font color='green' size='4'>ON");
else
client.println("<font color='grey' size='4'>OFF");
client.println("<hr />");
client.println("<hr />");
client.println("</body></html>");
//clearing string for next read
readString="";
//stopping client
client.stop();
}
}
}
}
}