Arduino communications

so I'm trying to use an arduino with an Ethernet shield connected to a router running openwrt and was wondering what the best possible way to communicate to the arduino via a web page. Either making the arduino a web server or hosting the web server in the router.

If I take the second option, how do I get the web page to send commands to the arduino over Ethernet?

Thanks :slight_smile:

use temboo :slight_smile: you don't even need a router with openwrt
or nearbus
read this article :slight_smile: for easy setup

Sorry, I should of stayed that the project is a wireless rc car, so the arduino is connected to a router to display a webpage and control the movements of the car wirelessly :slight_smile:

asasen5:
how do I get the web page to send commands to the arduino over Ethernet?

Don't you need to have a browser to display the web page and take user input that goes to the server which sends it to the Arduino?

Of course a server can't "send" data, it can only respond to requests so the Arduino would need to poll the server to see if there is data waiting. Look up "long polling".

You may be interested in my EzScrn demo which does something similar. Even if it is not exatly what you want it may give you some ideas.

...R

i get that it can't send data. So would I have to impliment a wrapper if hosting the web server on the router?

Seems like its easier to get an SD card and host the webserver on that haha.

asasen5:
Sorry, I should of stayed that the project is a wireless rc car, so the arduino is connected to a router to display a webpage and control the movements of the car wirelessly :slight_smile:

An arduino with ethernet shield and a wireless router maybe all you need similar to below.

http://www.lynxmotion.net/viewtopic.php?f=20&t=6343

asasen5:
So would I have to impliment a wrapper if hosting the web server on the router?

I don't understand this.

If the Arduino can only connect to the server with an "internet" connection the requests to the server have to come from the Arduino.

If you can connect the Arduino to the server with a serial connection more options are available.

...R

Robin2:
If you can connect the Arduino to the server with a serial connection more options are available.

What do you define as a serial connection? a physical serial cable connected to a serial port?

There's some other simlar projects that host the server on the router have, implemented wrappers for the arduino to interface with the router.

Example is the attached image.

You can host the webserver on the router running openwrt, and communicate with arduino over tcp/ip. You can configure Arduino ethernet as a Rest Api, and the server can send tcp socket when you , as client, send a request.

What do you define as a serial connection? a physical serial cable connected to a serial port?

Yup USB, you will have internet on your arduino but you must build API and run it on your PC
check my blog if you need open source serial communicator software

asasen5:
Example is the attached image.

There are other Threads (in the Interfacing section) about attempts to talk to an Arduino using a shell script and it does not seem to work easily or at all.

...R

There are other Threads (in the Interfacing section) about attempts to talk to an Arduino using a shell script and it does not seem to work easily or at all.

I hate shell commands :cry: tried to make that before with Cisco e900 for now i can't make it work....

Would that be because it's using a USB connection instead of an Ethernet one?

asasen5:
Would that be because it's using a USB connection instead of an Ethernet one?

You have lost me now. How could it use an Ethernet connection? I thought the purpose of your shell script was to convert a browser request to direct communication with an Arduino over /dev/ttyUSB0

I think you need to give us a more detailed description of what you are thinking of doing.

...R

Robin2:
You have lost me now. How could it use an Ethernet connection? I thought the purpose of your shell script was to convert a browser request to direct communication with an Arduino over /dev/ttyUSB0

I think you need to give us a more detailed description of what you are thinking of doing.

...R

Sorry been a bit all over the place lately.

What I am trying to do is control an RC car over a wireless network using a wifi router and ethernet shield attached to an Eleven (Freetronics Arduino), which is all on board and attached to the RC car. The motors are controlled by just sending HIGH and LOW to the pins of the IC chip of the RC car's circuit board.

What I am trying to work out what is the best way to store the webserver/webpage that will forward commands to the Eleven to control and move the car. Others either connect via a makeshift serial connection or via a usb which is send serial commands (pretty sure thats the best way to describe it).

Example

If I host the web page on the router, do I send commands through something like a GET method? or is it eisier to just host the webserver/webpage on an SD card on the Ethernet shield?

I all want to be able to use the arrow keys on a keyboard to drive the RC car if possible.

Would doing that just require using javascript?

Thanks for all your help so far as well, I really appreciate it :smiley:

If you need anymore information just let me know :slight_smile:

What I am trying to work out what is the best way to store the webserver/webpage that will forward commands to the Eleven to control and move the car.

If the control GUI is going to be a web page displayed by a browser, then the page can be served from the arduino server, served from another server, or you can use an html page file saved on your desk top.

I all want to be able to use the arrow keys on a keyboard to drive the RC car if possible.

This may be a challange for you if the keyboard is attached to a pc. You would probably have to develop an application on the pc to do this. I'd suggest you start with a web page control setup and work from there.

asasen5:
If I host the web page on the router, do I send commands through something like a GET method? or is it eisier to just host the webserver/webpage on an SD card on the Ethernet shield?

I all want to be able to use the arrow keys on a keyboard to drive the RC car if possible.

Would doing that just require using javascript?

I have the impression, based on these questions, that you need to do a lot more learning about how the web works.

As I said earlier, a server cannot send commands. It can, however, respond to GET (or POST) requests either from your browser or from web code running on your Arduino.

Javascript is code that the server sends to the browser (after a request from the browser) and which is executed by the browser. It is commonly used to allow the web application to do more complex things such as updating some of the data in webpage without having to update the whole page.

And Arduino does not have the capacity to run Javascript.

The code in my EzScrn demo is not designed to send data to an Arduino using the web (it uses Serial) but otherwise it is an example of a not-too-complex project with much of the competence you require. And it does use Javascript for the Browser. The project will work on a Yun so it may also work on your router.

As @zoomkat has said it would be more difficult (but not impossible) to get your browser to respond to the arrow keys and it would probably be sensible to leave that for later.

However, if you are going to be using a PC it would make things much simpler to have the server on the PC and just use the router for routing.

...R

So I've tried using @Zoomkat's wifi router bot and have modified to to work with what I have, but its only doing the first part of an if-else if loop.

IF loop code

          if ((n1 = "L")&& (n2= "F")){
             leftFor();
          }else if ((n1 = "R") && (n2 = "F")){
              rightFor();
          }else if ((n1 = "F") && (n2 = "F")){
              forward();
          }else if ((n1 = "B") && (n2 = "B")){
              backward();
          }else if ((n1 = "L") && (n2 = "B")){
              leftBack();
          }else if ((n1 = "R") && (n2 = "B")){
              rightBack();
          }else if ((n1 = "S") && (n2 = "S")){
              fullStop();
          }
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
byte gateway[] = { 192, 168, 1, 1};
EthernetServer server(80);  // create a server at port 80
String readString, LR, FB; //strings to hold data

int Left = 5;
int Right = 3;
int Forward = 9;
int Back = 6;
int Morph = 7;


void left(){
 digitalWrite(Left, HIGH);
 digitalWrite(Right, LOW);
}

void right(){
 digitalWrite(Right, HIGH);
 digitalWrite(Left, LOW);
}

void forward(){
 digitalWrite(Forward, HIGH);
 digitalWrite(Back, LOW);
 digitalWrite(Left, LOW);
 digitalWrite(Right, LOW);
}

void backward(){
 digitalWrite(Back, HIGH);
 digitalWrite(Forward, LOW);
 digitalWrite(Left, LOW);
 digitalWrite(Right, LOW);
}

void morph(){
 digitalWrite(Morph, HIGH);
}

void fullStop(){
 digitalWrite(Back, LOW);
 digitalWrite(Forward, LOW);
 digitalWrite(Right, LOW);
 digitalWrite(Left, LOW);
 digitalWrite(Morph, LOW);
}

void rightFor(){
 digitalWrite(Forward, HIGH);
 digitalWrite(Back, LOW);
 digitalWrite(Left, LOW);
 digitalWrite(Right, HIGH);
}

void leftFor(){
 digitalWrite(Forward, HIGH);
 digitalWrite(Back, LOW);
 digitalWrite(Left, HIGH);
 digitalWrite(Right, LOW);
}

void rightBack(){
 digitalWrite(Forward, LOW);
 digitalWrite(Back, HIGH);
 digitalWrite(Left, LOW);
 digitalWrite(Right, HIGH);
}

void leftBack(){
 digitalWrite(Forward, LOW);
 digitalWrite(Back, HIGH);
 digitalWrite(Left, HIGH);
 digitalWrite(Right, LOW);
 }


//////////////////////
void setup(){
  //start Ethernet
  Ethernet.begin(mac,ip,gateway);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  //myservo1.attach(7);
 // myservo2.attach(6);
//  pinMode(light1, OUTPUT);
//  pinMode(light2, OUTPUT);
  pinMode(Left, OUTPUT);
  pinMode(Right, OUTPUT);
  pinMode(Forward, OUTPUT);
  pinMode(Back, OUTPUT);
  pinMode(Morph, OUTPUT);
  left();
  delay(1000);
  right();
  delay(1000);
  fullStop();
  
  Serial.println("routerbot-11-20-12"); // 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; 
        } 
        //if HTTP request has ended
        if (c == '\n') {
          ///////////////
          //readString looks like "GET /?-1500-1500 HTTP/1.1"
          Serial.print(readString);
          //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println(F("HTTP/1.1 204"));
            client.println();
            client.println();  
          }
          else {   
            client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
            client.println(F("Content-Type: text/html"));
            client.println();
            client.println(F("<HTML><HEAD>"));
            client.println(F("<H3>Wi-Fi RC Car
Web Control Page
11-20-12</H3>"));
            client.println(F("<TITLE>Wi-Fi RC Car Control Page</TITLE>"));
            client.println(F("</HEAD><BODY>"));
            client.println(F("Foward&nbsp;&nbsp;&nbsp;-*Stop*-&nbsp;&nbsp;&nbsp;Reverse
"));
            client.println(F("<a href=/?-L-F target=inlineframe>Left&For</a>|"));
            client.println(F("<a href=/?-F-F target=inlineframe>*-Forward-*</a>|"));
            client.println(F("<a href=/?-R-F target=inlineframe>Right&For</a>|

"));
            client.println(F("Turn-L&nbsp;&nbsp;&nbsp;-*Stop*-&nbsp;&nbsp;&nbsp;Turn-R
"));
            client.println(F("<a href=/?-L-B target=inlineframe>Left&back</a>|"));
            client.println(F("<a href=/?-B-B target=inlineframe>*-Back-*</a>|"));
            client.println(F("<a href=/?-R-B target=inlineframe>Right&Back</a>|
"));
            client.println(F("Turn-L&nbsp;&nbsp;&nbsp;-*Stop*-&nbsp;&nbsp;&nbsp;Turn-R
"));
            client.println(F("<a href=/?-S-S target=inlineframe>&nbsp;&nbsp;&nbsp;*-Stop-*</a>|"));
            client.println(F("<IFRAME name=inlineframe style='display:none'>")); 
            client.println(F("</IFRAME>

"));
            client.println(F("<a href=\"#\"onClick=\"MyWindow=window.open('http://192.168.1.20:80/',"));
            client.println(F("'MyWindow','width=200,height=200');\">Popout Control Window</a>"));
            client.println(F("</body></html>"));
          }
          delay(1);
          //stopping client
          client.stop();

          if (readString.length() >0) {
            Serial.println(readString);
            
            LR = readString.substring(7, 8);
            FB = readString.substring(9, 10);
           
            String n1 = LR;
            String n2 = FB;


            Serial.println("the numbers are :");
            Serial.println(LR);  //print to serial monitor to see number results
            Serial.println(FB);
            Serial.println("");
           // myservo1.writeMicroseconds(n1); //set servo position 
//           // myservo2.writeMicroseconds(n2);

          if ((n1 = "L")&& (n2= "F")){
             leftFor();
          }else if ((n1 = "R") && (n2 = "F")){
              rightFor();
          }else if ((n1 = "F") && (n2 = "F")){
              forward();
          }else if ((n1 = "B") && (n2 = "B")){
              backward();
          }else if ((n1 = "L") && (n2 = "B")){
              leftBack();
          }else if ((n1 = "R") && (n2 = "B")){
              rightBack();
          }else if ((n1 = "S") && (n2 = "S")){
              fullStop();
          }
     
            
//           digitalWrite(light1, n1);
//           digitalWrite(light2, n2);
            //clearing string for next read            
            readString="";
            LR = "";
            FB = "";
          } 
        }
      }
    }
  }
}

Worked out what was wrong with that method.

In the IF statements i only had "=" not "=="