Arduino blog

Arduino blog

Arduino thinkering

This blog is about things I come by related to Arduino which I feel are inspiring/important/funny enough to write about.

Controlling Arduino over the web

Arduino sketch experiancesPosted by Jan Mon, September 09, 2013 03:06:22
I have been working some years now on building my own lawn mowing robot. I know you can buy a lawnmower robot in the shop but after intense market search I didn't find anything suiting my needs.
The main problem is that the inhabitants of my garden like to make piles which makes for a very unflat surface. After some trials to make it flat I gave up and started designing Marvin.
Marvin is a 4by4 frontmower lawnmower robot. He is planned to become fully autonomous but currently still needs to be brought to his charging station. Marvin is currently taking care of around 3000 square meters. But is planned to be able to handle 5000 and probably even more.
you can read more on Marvin in this post on the arduino forum. http://forum.arduino.cc/index.php?topic=186329.0

But here I want to focus a bit on the "communication part"
Having a robot running in your garden is cool. But when you are developing the robot you want to be able to connect to the robot wit the serial monitor and upload a new sketch without having to run around with a laptop and a USB cable.
There are several options to do this. I have opted to hack a linksys router install openwrt and upload from there.
Wait .... did I say openwrt??? Isn't that what yun will be running? Would this way be compatible with the yun? Well likely but it probably will need some tuning.
To explain this all I have to tell you a long story so you can understand how it works.
I want to tell you why you would listen to the whole story first.

What do you get to learn?
How can I upload a sketch remotely (I mean over the internet if needed)
How can I change parameters of my sketch easily (I mean over the internet if needed)
How can I reboot my sketch remotely (I mean over the internet if needed)
How can I program parameters in progmem (I mean over the internet if needed)
First things first. What do I mean with (I mean over the internet if needed).
The entrance point to your arduino becomes the system running openwrt (or any linux for that part). Accessing that system from your local network is easy. Accessing it from the internet requires some more work and it may have a financial impact (a other subscription wih your network provider). This set of articles is not going into this area as I assume you only need to access your arduino from your local network.

What do you need?
1) A arduino.
2) A machine able to run openwrt (or linux) that has a serial port and an Ethernet connection. If you want to go wireless this needs to be a wireless connection and that rules the pi out (unless you go for pi with wireless dongle) but any wireless router that has been hacked by the openwrt community should do.
I have used a linksys WRT54GL but that one is kind of short on "disk space" I hope the yun (which is in the same price range) will do better.
3) A router your machine running openwrt can connect to. (That is just like the pc you are using is connected to your local network)

That is all the hardware you need.

Look at this (slightly outdated drawing) to get the picture.
Note that the yun promises to be 1 and 2 together.
Also note that I did not succeed to hack the router with a sd card which left me with very little space which again should be solved by the yun.


What needs to be done to setup the same for your system?
1) Adapt your Arduino code so it is compliant.
2) Adapt the Arduino hardware so it is compliant.
3) Install and configure the linux scripts and software.

The first one is needed so that serial commands send to arduino are processed properly. For instance sending "RESET" should make the arduino reset. Some code is needed on Arduino to do so.
The second is needed because a RESET must be a hardware reset. The easiest is to connect a pin to the reset pin of the arduino. That finishes the hardware modifications smiley
The third one is needed to send commands to arduino via the network and to make the output of arduino available on the network. For instance you want to install avrdude to upload your sketch and minicom to have a remote serial monitor.

Step 2 and 3 are easy for a user. Step 1 requires a good understanding on what is going on with Arduino and is what major bulk of this set of articles is about.

Lets end this blog with and start the explanation with :"Sending line commands over the serial monitor"
The big bulk of examples of serial communication towards the arduino are character based commands. This is easy as each character is self containing. You do not need to know what was send before or will be send after each character.
For instance when you send 's' arduino starts. When you send 'p' arduino pauses.
But what if you send "Fast play". Arduino would understand start and then pause.
To be able to give long readable commands like "SET led.pwm=20" we need a line interpreter and not a character interpreter.
The first thing you need for a line interpreter is to agree on the "end of the line character sequence". As you can see in the Arduino serial monitor you have several options to add to the characters you send. Namely "no line ending", "carriage return", "newline" and "both NL & CR".
If you want to do line interpretation "no line ending" is not an option (though default in arduino) you need a special string to mark the end of the line. "carriage return", "newline" and "both NL & CR" are all commonly used (One is popular in linux land the other in windowsland).
Therefore the basis of all the above promises starts with a class that does the line interpretation for you. This class handles "carriage return", "newline" and "both NL & CR" seamlessly so you do not need to bother. This class can be downloaded from github at https://github.com/jantje/libraries/tree/master/SerialStringReader

The example looks like

#include "SerialStringReader.h"
SerialStringReader mySerialReader;


//The setup function is called once at startup of the sketch
void setup()
{
Serial.begin(9600);
mySerialReader.setup();
}

// The loop function is called in an endless loop
void loop()
{
mySerialReader.loop();
if (mySerialReader.messageReceived())
{
Serial.println("You have send a message to Arduino.");
Serial.println("And the message is:");
Serial.println(mySerialReader.getMessage());
}
}


I'll go into some detail as it contains some techniques you will see coming back.
First of all I use C++ classes. this allows me to declare the variable
SerialStringReader mySerialReader;
and then call methods on it.
mySerialReader.setup();
mySerialReader.loop();

All the classes have a setup() and loop(). setup() should be called in the main setup and loop in the main loop.
I do part of the initialization in the constructor and part in the setup. (No need to worry f you do not understand this)
All the repetitive action are in loop.
Upload this example to a Arduino and open the serial monitor. As you will see processing line by line in Arduino was never so easy.

In the next blog I'll build on this to add some commands.










  • Comments(0)//blog.baeyens.it/#post7