Arduino Communications

From Combustory
(Difference between revisions)
Jump to: navigation, search
Line 157: Line 157:
 
''' Create a Folder on the Linux box and share the Folder over the network using ''samba'' - '''  There is not much to say here other than learn your ''samba'' and make it happen.  There are lterally tons of info on this subject on the web. Other than that make sure that the permissions are set to allow creating and writing files.
 
''' Create a Folder on the Linux box and share the Folder over the network using ''samba'' - '''  There is not much to say here other than learn your ''samba'' and make it happen.  There are lterally tons of info on this subject on the web. Other than that make sure that the permissions are set to allow creating and writing files.
 
==== Step 5 ====
 
==== Step 5 ====
''' Compile the Arduino-Serial software and place the executable in the shared folder from Step 4 - ''' This step gave me a little bit of an issue. I had to comment out a few baud speed lines.  It was not the difficult to figure out, because the compiler gave pretty clear error messages and these [http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/  Arduino-Serial] instructions even mentioned this as an issue.
+
''' Compile the Arduino-Serial software and place the executable in the shared folder from Step 4 - ''' This step gave me a little bit of an issue. I had to comment out a few baud speed lines.  It was not the difficult to figure out, because the compiler gave pretty clear error messages and these [http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/  Arduino-Serial] instructions even mentioned this as an issue and even though it is mentioned and supposedly fixed, there is still one more line he did not comment out. Make sure any line that looks like this:
  
 +
case 28800:  brate=B28800;  break; 
  
 +
Looks like this:
  
 +
//case 28800:  brate=B28800;  break;
 +
 +
==== Step 6 ====
 +
''' Load the sample BASH scripting text into an executable file in your shared folder from Step 4 on the Linux box - ''' Here is the BASH script.  It is a script that goes into an infinite loop and constantly polls for two files, a ''command'' file and a ''clear_log'' file.  When it sees those files it takes action to either send commands or clear the log file.  The files are sent from some other application that are used to control the communication to the Arduino.  In this example I use AutoIT as the controlling software (See Step 9).
 +
 +
#!/bin/bash
 +
# File name CommandPoll
 +
# This Script is used to control the communication to an Arduino board via the Arduino-Serial command line utility and the dev/ttyUSB0 terminal
 +
# Written by John Vaughters <http://www.combustory.com>
 +
 +
# Set the terminal to match the Arduino Serial Communications
 +
stty -F /dev/ttyUSB0 cs8 57600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
 +
 +
# Kill any existing tail commands logging the terminal
 +
exec  ps ax | grep "tail -f /dev/ttyUSB0" | grep ? | awk '{system("kill " $1)}'
 +
# Connect the terminal to a tail logging to a file by appending
 +
exec tail -f /dev/ttyUSB0 >> /home/jvaughters/arduino-0011/sketchbook/ArduinoSerial/arduino_log &
 +
 +
# create an infinite loop to test for files that will prompt action
 +
while [ 1 ]
 +
do
 +
  if [ -f command ]     # Does the command file exist
 +
    then
 +
    cat command | awk '{system("./arduino-serial -b 57600 -p /dev/ttyUSB0 -s " $1)}' # Send the commands in the command file via Arduino-Serial utility
 +
    rm command     # remove the command file
 +
  fi
 +
  if [ -f clear_log ]     #Clear test file
 +
    then
 +
    echo > arduino_log
 +
    rm clear_log
 +
  fi
 +
  sleep 1     # Sleep for one second or your processor will run 100% (optional)
 +
done
 +
exit 0
 
== Links ==
 
== Links ==

Revision as of 19:58, 8 August 2008

Arduino Communications Page

Contents

Summary

In my search for ways to communicate with the Arduino board, I found lots of ways that required me to learn new languages or learn serial communication programming. I do want to eventually pick up these skills, but I found a quicker way for my needs. I have learned over time that you can always find a better way to solve a problem, but I realize that the amount of time to learn that better way is sometimes greater than the project time-frame, and therefore just solve it in the best way you can with the tools you have. I believe that the solution below encompasses that spirit. Here you will find a quick, dirty yet effective solution for communicating with the Arduino.

This solution met my goals, but it may not meet yours. It is limited in the effect that a transfer in a message may take as long as 5 sec. This fits fine for my needs where I am just looking to form a distributed network of smart sensors/controllers that allow the setting of certain variables and the reporting of alarms and/or useful tracking information from the individual controllers.

Note: I owe a BIG debt of gratitude to all the Arduino hackers that provided me with the tools/code/knowledge to allow this solution

Functional Description of Method

This method creates a log file that is created by an Arduino board using serial communications that is sent to a terminal which is redirected to a file. The file can then be used by any software you desire to process the messages from the Arduino board. To send messages to the Arduino board the use of Arduino-Serial (a command line utility) is placed in a BASH script that is used to constantly poll for the existence of a command file. When a command file is found, the BASH script will send the commands in the file to the Arduino board. The response by the Arduino is to follow the command input and print response data out to the terminal, which is sent to the log file.

If your brain just got scrambled, join the club. There were several technical hurdles I was concerned about of as soon as I thought of it. I did not even think this would work at first, but it turns out to function just fine. (Until Further Notice! `,~)

Requirements

The method requires the following hardware/knowledge:

  • An Arduino Board or equivalent
  • Linux computer that is able to communicate with the Arduino
  • Your favorite development language
  • Basic Linux operational skill
  • Knowledge of samba or NIS if networking is desired


This example requires the following hardware/knowledge:

  • An Arduino Board or equivalent (I used an actual Arduino Board with the USB connection)
  • Linux computer that is able to communicate with the Arduino (I used an Ubuntu 8.x box)
  • A Windows computer that is able to support AutoIT (I used an XP box)
  • A network between the two computers
  • Development languages - BASH scripting for Linux and AutoIT for windows
  • Knowledge of samba for sharing folders over the network


Example of Method

Quick Guide:

Step 1 - Buy an Arduino Board - http://www.arduino.cc/en/Main/Buy

Step 2 - Load the Arduino software on your linux box - http://www.arduino.cc/playground/Learning/Linux

Step 3 - Load the sample Arduino code (see Step 3 below) into your Arduino board - http://www.arduino.cc/en/Guide/HomePage

Step 4 - Create a Folder on the Linux box and share the Folder over the network using samba - http://us1.samba.org/samba/

Step 5 - Compile the Arduino-Serial software and place the executable in the shared folder from Step 4 - http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/

Step 6 - Load the sample BASH scripting text (see Step 6 below) into an executable file in your shared folder from Step 4 on the Linux box - http://www.gnu.org/software/bash/

Step 7 - Build your Thermistor circuit based on the diagram (see Step 7 below) or something similar

Step 8 - Map your 'samba' share to a windows drive

Step 9 - Load AutoIT on your windows box - http://www.autoitscript.com/autoit3/

Step 10 - Use the sample AutoIT script (see Step 10 below) to test the solution

Step 11 - Revel in your new found capability to conquer the world, well the Arduino world anyway! `,~)

Detailed Guide:

Step 1

Buy an Arduino Board - There are lots of options to buy a board. I chose the standard USB version from the guidance of this Arduino link. If you follow this Freeduino link you will find many options including my favorite the Bare Bones Board from moderndevice.com and wulfden.com. Check out the RBBB assembly. These options are super cheap and I will definitely be buying my next Arduino based board from these sites. These folks have knocked down the price of micro-controller development boards.

Step 2

Load the Arduino software on your linux box - Here are the Linux Instructions. For my Arduino, I used Ubuntu 8.x. I also recommend you do a google search of Aruino and your linux type to find any type of specific hiccups that inevitably find there way into installs. The Ubuntu instuctions I ended up using was from principialabs,com and after all was said and done these instructions worked without a single problem for me.

Step 3

Load the sample Arduino code into your Arduino board - Here is the code I used. I will not claim it to be pretty, but it does work as a test for this method. Most likely you will have to modify some of the numbers around the input. I will give you my schematic, but I am sure that your set up will vary somewhat and that will change the Threshold and Voltage reading numbers.

/*
* AnalogInput with Thermistor
* by DojoDave <http://www.0j0.org> and John Vaughters <http://www.combustory.com>
*
* Turns on a light emitting diode(LED) connected to digital  
* pin 13 when the temperature rises above the threshold. The value obtained by analogRead(). 
* In the easiest case we connect a thermistor to analog pin 5.  The program also implements a 
* Serial Communication method that utilizes a char and a # ie. A0...A9, B0...B9, etc. Each Command will implement 
* a specific action in the Arduino.
* 
*/ 
int tempPin = 5;    // select the input pin for the Thermistor
int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor
int THRESHOLD = 580;
int statePin = HIGH;  // variable used to store the last LED status, to toggle the light
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
long polTime = 1000;  // The time to Pol the tempPin
long previousMillis = 0;        // will store last time Temp was updated


void setup() {
  Serial.begin(57600);
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(12, OUTPUT);      // Test Com Reset issue  
  digitalWrite(12,HIGH);
  delay(5000);
  digitalWrite(12,LOW);
} 

void loop() {
  if (millis() - previousMillis > polTime) {
    previousMillis = millis();   // remember the last time
    val = analogRead(tempPin);    // read the value from the sensor
     if (val >= THRESHOLD) {
      //statePin = !statePin;           // toggle the status of the ledPin (this trick doesn't use time cycles)
      digitalWrite(ledPin, statePin); // turn the led on or off
      Serial.print("~@ Hot ");          // send the string "Hot" back to the computer, followed by newline
      Serial.print("Temp = ");
      Serial.println(val);           // 
     }
    else {
      digitalWrite(ledPin, LOW);
    }
    if (Serial.available()) {      // Look for char in serial que and process if found
      command = Serial.read();
      if (command == 84) {          // If command = "T" print the Temp
        Serial.print("~& Temp = ");
        Serial.print(val);           // 
        Serial.print(" ");
        delay(100);
      }
      else if (command == 67) {      //If command = "C" Change Temp Threshhold
        if (Serial.available()) {
          command = Serial.read();
          if (command > 47 && command < 58) {          // If command is between 0-9 Increment the Threshold by number sent
            THRESHOLD += command - 48;                 // ASII math to get value sent   
                  Serial.print("~# THRESHOLD = ");
                  Serial.print(THRESHOLD);           // 
                  Serial.print(" ");
                  delay(100);
          }  
        }  
      }
      else if (command == 68) {      //If command = "D" Change Temp Threshhold
        if (Serial.available()) {
          command = Serial.read();
          if (command > 47 && command < 58) {          // If command is between 0-9 Decrement the Threshold by number sent
            THRESHOLD -= command - 48;                 // ASII math to get value sent   
                  Serial.print("~# THRESHOLD = ");
                  Serial.print(THRESHOLD);           // 
                  Serial.print(" ");
                  delay(100);
          }  
        }  
      }
      delay(100);
      Serial.println(command);     // Echo command char found in serial que
      command = 0;                 // reset command 
    }
  }
}
//*****************************************************The End***********************

Step 4

Create a Folder on the Linux box and share the Folder over the network using samba - There is not much to say here other than learn your samba and make it happen. There are lterally tons of info on this subject on the web. Other than that make sure that the permissions are set to allow creating and writing files.

Step 5

Compile the Arduino-Serial software and place the executable in the shared folder from Step 4 - This step gave me a little bit of an issue. I had to comment out a few baud speed lines. It was not the difficult to figure out, because the compiler gave pretty clear error messages and these Arduino-Serial instructions even mentioned this as an issue and even though it is mentioned and supposedly fixed, there is still one more line he did not comment out. Make sure any line that looks like this:

case 28800:  brate=B28800;  break;  

Looks like this:

//case 28800:  brate=B28800;  break;

Step 6

Load the sample BASH scripting text into an executable file in your shared folder from Step 4 on the Linux box - Here is the BASH script. It is a script that goes into an infinite loop and constantly polls for two files, a command file and a clear_log file. When it sees those files it takes action to either send commands or clear the log file. The files are sent from some other application that are used to control the communication to the Arduino. In this example I use AutoIT as the controlling software (See Step 9).

#!/bin/bash
# File name CommandPoll
# This Script is used to control the communication to an Arduino board via the Arduino-Serial command line utility and the dev/ttyUSB0 terminal
# Written by John Vaughters <http://www.combustory.com>

# Set the terminal to match the Arduino Serial Communications
stty -F /dev/ttyUSB0 cs8 57600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts

# Kill any existing tail commands logging the terminal
exec  ps ax | grep "tail -f /dev/ttyUSB0" | grep ? | awk '{system("kill " $1)}'
# Connect the terminal to a tail logging to a file by appending
exec tail -f /dev/ttyUSB0 >> /home/jvaughters/arduino-0011/sketchbook/ArduinoSerial/arduino_log &

# create an infinite loop to test for files that will prompt action
while [ 1 ]
do
  if [ -f command ] 								     # Does the command file exist
    then
    cat command | awk '{system("./arduino-serial -b 57600 -p /dev/ttyUSB0 -s " $1)}' # Send the commands in the command file via Arduino-Serial utility
    rm command									     # remove the command file
  fi
  if [ -f clear_log ] 								     #Clear test file
    then
    echo > arduino_log
    rm clear_log
  fi
  sleep 1 									     # Sleep for one second or your processor will run 100% (optional)
done
exit 0 

Links

Personal tools
Sponsers
Your Ad Here
Your Ad Here