Interfacing Arduino with PC (2-way communication)

I am looking to interface an Arduino unit with a computer over a network socket bridged to a serial port (Via linksys wifi router and DDWRT) in hopes of remote controlling a robot. Problem is, I want to do most of the processing on the Arduino end (I know I'm crazy :slight_smile: ) so that I can still hook up a wired controller for preliminary testing and for PC-free use. I also want to be able to send sensor data back to the PC from the Arduino :astonished:. I would like to use visual basic since the robot is the majority of my project, not the programming on the PC end. I am kinda clueless where to start =( so any advice would be great. :grin:
Thanks everyone!

I would like to use visual basic

I don't understand that, but, you are welcome to use it.

I am looking to interface an Arduino unit with a computer over a network socket bridged to a serial port (Via linksys wifi router and DDWRT)

The Arduino won't care what is on the other end of the serial port. However, adding 22 layers of complexity to an already challenging project is hardly the recommended path to success, especially in light of this:

not the programming on the PC end.
I am kinda clueless where to start

so any advice would be great.

Start simple. Connect the Arduino directly to the PC.

The whole reason for visual basic is because I know what I am doing in there, but if there is a better programming language to use, please let me know.
Is there a prebuilt protocol for interfacing a Arduino to some easier programming languages? I looked at firmata and it seemed attractive, but I really don't wanna try to learn a new programming language unless its REALLY user friendly. I want to be able to click things in a GUI and have the robot do what you tell it to. To me, the serial communication seems kind of confusing.

I want to be able to click things in a GUI and have the robot do what you tell it to.

If you are clicking in a GUI, and the robot does what I tell it to, won't that be confusing?

but if there is a better programming language to use, please let me know.

Well, I like C# for defining interfaces and the ease of communicating with the serial port. Getting data from the serial port displayed back in the GUI can be a little challenging, given the need to use multiple threads and get the threads to communicate, but once you get it all worked out (I did, send me a PM if interested in the sample program) it becomes transparent to the operation of the program.

I looked at firmata and it seemed attractive, but I really don't wanna try to learn a new programming language

Firmata isn't a programming language, isn't supported for the Arduino (it's available, but there is no support), and is somewhat limiting in that there is a servo sketch, a standard sketch, etc., but no one do-it-all sketch like you could write.

To me, the serial communication seems kind of confusing.

I don't see why. Think of it like posting on this forum. It takes a while to type a message (send some data). It may or may not actually get to the server (it may get lost). It may, or may not, actually get read (reading it is your responsibility), and it may, or may, not be easy to understand and implement the suggestions being made.

But, since you are controlling both the sender and the receiver, it can be quite rewarding to develop a protocol that both ends can understand, and get them to actually play well together.

PaulS:

I want to be able to click things in a GUI and have the robot do what you tell it to.

If you are clicking in a GUI, and the robot does what I tell it to, won't that be confusing?

Now I know who to yell at if I click forward and it goes backwards :stuck_out_tongue:

To me, the serial communication seems kind of confusing.

I don't see why. Think of it like posting on this forum. It takes a while to type a message (send some data). It may or may not actually get to the server (it may get lost). It may, or may not, actually get read (reading it is your responsibility), and it may, or may, not be easy to understand and implement the suggestions being made.

But, since you are controlling both the sender and the receiver, it can be quite rewarding to develop a protocol that both ends can understand, and get them to actually play well together.

What I see as confusing is the whole read one character at a time. The important part right now is that I need to figure out how to get the data to the Arduino. I can slowly work on phasing the rest of it in.

What I see as confusing is the whole read one character at a time.

Isn't that how everyone reads?

PaulS:

What I see as confusing is the whole read one character at a time.

Isn't that how everyone reads?

I guess it would be... I really need to watch what I say. I meant that I would like it to just read a whole string out of the serial buffer at once so that I can work with it.

I meant that I would like it to just read a whole string out of the serial buffer at once so that I can work with it.

There are a number of new methods in the Serial (Stream) class with 1.0. But, they basically read one character at a time.

The trickiest part of "just read a whole string" at once is defining what constitutes a packet.

With anything else we read, punctuation is important. For instance, we know that there are different packets in the stuff we read, called words, and packets called sentences, and packets called paragraphs, and packets called chapters, and packets called books.

If you read the data, yourself, one character at a time, you can recognize the various end-of-packet markers yourself, and take appropriate action when they arrive.

If you have a specific definition of a packet, run it by us for comment, and help writing code to read that packet, one character at a time, and to make use of that packet contents.

If you have a specific definition of a packet, run it by us for comment, and help writing code to read that packet, one character at a time, and to make use of that packet contents.

To me a serial data packet would be the variable and the value to set it to. What would be the best start/end of packet marker?

Would it be easier for the PC to tell the Arduino it needs data from a sensor then have the Arduino send it, or to just have the Arduino send it every time a command is received and processed?

What would be the best start/end of packet marker?

I don't know about best, but I use < and >, and this code to read the serial data.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says "Process the packet", you can deal with the name = value data.

Would it be easier for the PC to tell the Arduino it needs data from a sensor then have the Arduino send it, or to just have the Arduino send it every time a command is received and processed?

It would be easiest, but not necessarily best, to have the Arduino send a response after every command.

It would be easiest, but not necessarily best, to have the Arduino send a response after every command.

I need to keep latency down so that this will be pretty close to realtime, because with 2 3000w motors on a 25lb contaption, it could get moving pretty fast. I also will be running an if statement with a few mathematical equations thrown in there, because I need to vary the range of differential drive turning depending upon the forward speed so I don't throw tracks or flip the thing :astonished:.
Would that method of sending sensor data cause any noticeable slowdown?

PaulS:

What I see as confusing is the whole read one character at a time.

Isn't that how everyone reads?

Actually no. I believe that the prevailing model is still that most people (certainly in the English and European based languages) read a word (or more) at a time, based on the 'shape' of the words.

I have a similar problem to solve. I need an application to be able to do things like check for Arduino connected through ethernet and determine their IP address. I also need to be able to update their IP address from the application.

The next task would be to change some PWM channel values and display some general information to the user about the device (status, etc..).

Serial would be nice, but I may end up going with ethernet only and using UDP (?). One thought is to send UDP packets to update the controller.

I need an application to be able to do things like check for Arduino connected through ethernet and determine their IP address.

If your PC is acting as a server, your server software should have tools to manage that. Apache does, for instance.

I also need to be able to update their IP address from the application.

Good luck with that. The server gets a request, and servers up a response. Then, the connection is closed. Until the client connects again. A server can not initiate communication with a client.

The next task would be to change some PWM channel values and display some general information to the user about the device (status, etc..).

You can supply new values when a client requests them. You can not push new values to a client.

Serial would be nice, but I may end up going with ethernet only and using UDP (?). One thought is to send UDP packets to update the controller.

Everything up to this point assumed that the Arduino-as-client was connecting via ethernet...

hello.

i just want some advice/help :slight_smile:

my project is about sending data from pc-xbee(Tx)-xbee(Rx)-fan. in simple words, i just want to switch on/off the fan.. however, i find it very hard to start but i think http://tutorial.cytron.com.my/2011/08/13/project-14-wireless-temperature-logging/ was a very good tutorial for my project right ??

i have ..

2 Arduino Duemilanove(Compatible)-Main Board
2 Arduino-XBee Shield (without module)
2 XBee 1mW Wire Antenna - Series 2

can i use this tutorial Antipasto Hardware Blog: XBee Shield to XBee Shield Communication to make my xbee communicate to each other first?? if not, can someone help to link other tutorial (similar to my project)?? please teach me... thanx~

First question you need to answer is why you bought mesh-network radios for point-to-point use, instead of point-to-point radios for point-to-point use.

thanx PaulS :slight_smile: ,

errr.. i just put wrong device ...
XBee 1mW Wire Antenna - Series 1 not series 2.. sorry for mistake...

so..in your opinion, can i use the tutorial i mention before ?? or do u have any suggestion :~ please help.. =( thank you..

No, you can't use that tutorial. That is for series 2.5 devices. Using X-CTU, read the modem configuration. Then change PAN ID, MY and DL. Set PAN ID to the same value on both XBees. Set MY on one to some value other than 0. Set DL to a different value, other than 0. On the other XBee, use the same values, but in opposite fields. MY on one should be DL on the other.

That is all that you need to set.

Which XBee shields did you get?

ooo..thanx for helping...

i'm using Arduino-XBee Shield (without module)

4855.jpg

i'm using Arduino-XBee Shield (without module)

That will probably work better WITH a module attached. There are jumpers on that shield that can be moved to allow the programming of the Arduino will the shield still attached, and then moved to connect the XBee to the serial port pins.