Arduino Communications

From Combustory
Revision as of 23:35, 9 August 2008 by Combustor (Talk | contribs)
Jump to: navigation, search

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 as soon as I thought of this method. I did not even think this would work at first, but it turns out to function just fine. (Until Further Notice! `,~)

Arduino Com diagram.jpg

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' shared folder on your linux box 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). I will leave it to the reader to decode these commands.

#!/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 arduino_log 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 

Step 7

Build your Thermistor and LED circuits based on the diagrams or something similar - Thats it, just follow the diagrams.

Arduino com circuit.jpg

Step 8

Map your samba shared folder on your linux box to a windows drive - For this example you have to map a drive on your windows box. I think I will leave the details on how to accomplish this task to a google search on mapping drives in windows. It is fairly straight forward. I mapped my drive to P: for no particular reason, but what ever you map it to you will be able to select the drive from within the AutoIT GUI (See Step 9).

Step 9

Load AutoIT on your windows box - Go to the AutoIT site and load this very powerful and free software. This software has been around for a while and it has a fairly active development crew, which creates constant improvements, extensions and user libraries. It is a very powerful GUI scripting tool and it is very easy to create a GUI application as well. I have created many great utilities using this software. It is not for super powerful software needs, but with today's computers, it does quite a bit. LOVE this tool.

Step 10

Use the sample AutoIT script to test the solution - This code is a utility that will allow you to select the mapped shared windows drive/path and a working directory. There is a drop down menu with four commands that that will make the Arduino respond. This program will create a command file, fill it with the command string and place it in the Network Dir. The Software will wait 5 seconds and then retrieve the results and place it in the test window. Then the program will place the clear_log file in the Network Dir. I will not cover the use of this software here, refer to the user section. For now just get this loaded into AutoIT and hit F5 to get it to run. You can run AutoIT programs interpreted or compiled. Pressing F5 runs it as interpreted.

; Arduino Communications and Control
; File name: Arduino_Com_v.01.au3
; 31 Jul 2008 - John Vaughters <http://www.combustory.com>
; This is a Utility to Communicate with the Arduino Control board

#include <GuiConstants.au3>
#include <GuiEdit.au3> 
#include <file.au3>
#include <Date.au3>
#include <IE.au3> 
 
;*************
;  _Load_Results() loads the temp file into the desired control then deletes the temp file
; $w_dir is the working directory where the file exists
; $ctl_to_load is the place to load the file contents
; $temp_file is the temporary file to load the data from
; The function returns the number of lines loaded from the file

Func _Load_Results ($w_dir, $ctl_load_to, $temp_file)
	Dim $aRecords
	If Not _FileReadToArray($w_dir & $temp_file,$aRecords) Then
			MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
			 Exit
		EndIf
		
		For $x = 1 to $aRecords[0]
			if StringLen($aRecords[$x]) > 0 then GuiCtrlSetData($ctl_load_to, $aRecords[$x] & @CRLF, 1)
		Next
		;Delete the temporary file
		RunWait(@ComSpec & " /c " & "del " & $temp_file, $w_dir & "",@SW_HIDE)
		Return ($aRecords[0]-1)
EndFunc

;Initialize Variable Defaults
$working_dir = @DesktopDir & "\"
$working_file = "arduino_log"
$poll_delay = 5000                             ; This is the delay the program waits before collecting the command  results
$network_dir = "P:\"                           ; Default network directory
$process_results = False ;Processing Flag
$command_get_begin = TimerInit() ;Initialize Timer
; GUI
GuiCreate(" Arduino Communications and Control", 700, 600)

; MENU 
$filemenu = GuiCtrlCreateMenu("&File")
$fileitem = GUICtrlCreateMenuitem ("Open",$filemenu)
GUICtrlSetState(-1,$GUI_DEFBUTTON)
$exititem = GUICtrlCreateMenuitem ("Exit",$filemenu)
$helpmenu = GuiCtrlCreateMenu("Help")
$infoitem = GUICtrlCreateMenuitem ("Info",$helpmenu)


; LOGO PIC
GuiCtrlCreatePic("logo.jpg",0,0, 100,140)

; AVI for letting the user know the system is processing
$processing = GuiCtrlCreateAvi("sampleAVI.avi",0, 405, 140, 32, 32)

; Tabbed Result Window
$tab_result_start_x = 20
$tab_result_start_y = 175
$tab_result_size_x = 650
$tab_result_size_y = 400
$tab_result_title_2 = "Command Results"
$tab_result_title_4 = "Sys Info"
GuiCtrlCreateTab($tab_result_start_x, $tab_result_start_y, $tab_result_size_x , $tab_result_size_y)
GuiCtrlCreateTabItem($tab_result_title_2)
$edit_ctl_tab2 = GuiCtrlCreateEdit(@CRLF & "", $tab_result_start_x + 10 , $tab_result_start_y + 40, $tab_result_size_x - 20, $tab_result_size_y - 50)
GuiCtrlCreateTabItem($tab_result_title_4)
$edit_ctl_tab4 = GuiCtrlCreateEdit(@CRLF & "", $tab_result_start_x + 10 , $tab_result_start_y + 40, $tab_result_size_x - 20, $tab_result_size_y - 50)
GuiCtrlCreateTabItem("")

; Combo Arduino Command File Type
$combo_ctl_search_file = GuiCtrlCreatecombo("*", 240, 145, 120, 100)
GUICtrlSetData(-1,"C5|D5|T1|","C5") ; add other item snd set a new default
GuiCtrlCreateLabel("Arduino Command", 245, 170, 150, 20)

; Current Working Directory Label
$combo_ctl_working_dir = GuiCtrlCreateLabel($working_dir, 240, 80, 450, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
GuiCtrlCreateLabel("Working Directory", 245, 105, 200, 20)

; Current Network Directory Label
$combo_ctl_network_dir = GuiCtrlCreateLabel($network_dir, 240, 20, 450, 22,$WS_DLGFRAME)
GUICtrlSetBkColor(-1,0xffffff)
GuiCtrlCreateLabel("Network Directory", 245, 45, 200, 20)

; BUTTON
$search_btn = GuiCtrlCreateButton("Go", 370, 143, 25, 25)
$file_btn = GuiCtrlCreateButton("Working Dir", 125, 77, 100, 25)
$network_btn = GuiCtrlCreateButton("Network Dir", 125, 18, 100, 25)
$site_btn = GuiCtrlCreateButton("www.combustory.com", 0, 145, 135, 22)
$clear_btn = GuiCtrlCreateButton("Clear", 620, 170, 50, 22)

; List System Info
GuiCtrlSetData($edit_ctl_tab4, "Computer:             " & @CRLF & _
								"----------------------------------------------------------" & @CRLF & _
								"IP Address:             " & @IPAddress1 & @CRLF & _
								"Computer Name:     " & @ComputerName & @CRLF & _
   								"OS:                         " & @OSVersion & @CRLF & _
								"Sys Dir:                   " & @SystemDir & @CRLF & @CRLF)
; GUI MESSAGE LOOP
GuiSetState()

; Main Event Loop
While 1
  ; After every loop check if the user clicked something in the GUI window
   $msg = GUIGetMsg()
    if $process_results = True Then
		if $poll_delay < TimerDiff($command_get_begin) Then
			RunWait(@ComSpec & " /c " & "type arduino_log>results.txt", $network_dir,@SW_HIDE)
			; Load command results into Command Results tab
			GuiCtrlSetData($edit_ctl_tab2, "------------------------------------------------------------------" & @CRLF,1)
			GuiCtrlSetData($edit_ctl_tab2, "Command:  " & GUICtrlRead($combo_ctl_search_file) & @CRLF,1)
			GuiCtrlSetData($edit_ctl_tab2, "Results:  " & @CRLF,1)
			_Load_Results ($network_dir, $edit_ctl_tab2, "results.txt")
			GuiCtrlSetData($edit_ctl_tab2, "******************************************************************" & @CRLF,1)
			$process_results = False
			RunWait(@ComSpec & " /c " & "echo>clear_log", $network_dir,@SW_HIDE) ; Send message to Linux polling script to clear the log
			GUICtrlSetState ($processing, 0)
		EndIf
	EndIf	
   Select
   
    ; Check if user clicked on the close button 
	Case $msg = $GUI_EVENT_CLOSE Or $msg = $exititem
        ; Destroy the GUI including the controls
         GUIDelete()
       ; Exit the script
         Exit
         
	; Check if user clicked on the File Open button
	Case $msg = $fileitem
		$working_dir = FileSelectFolder("Choose Folder...","",4,"") & "\"
		GuiCtrlSetData($combo_ctl_working_dir, $working_dir)
	
	; Check if user clicked on the Help Info button
	Case $msg = $infoitem
		MsgBox(64, "Info", "Arduino Communications Control v0.1" & @CRLF & "By: John Vaughters")
	
	; Check if user clicked on the "File" button
	Case $msg = $file_btn
		$working_dir = FileSelectFolder("Choose Folder...","",4,"") & "\"
		GuiCtrlSetData($combo_ctl_working_dir, $working_dir)
			
	Case $msg = $network_btn
		$network_dir = FileSelectFolder("Choose Folder...","",4,"") & "\"
		GuiCtrlSetData($combo_ctl_network_dir, $network_dir)

	Case $msg = $search_btn
		
		; Start processing AVI 
		GUICtrlSetState ($processing, 1)
		
		$file = FileOpen($working_dir & "command", 2)
		; Check if file opened for reading OK
		If $file = -1 Then
			MsgBox(0, "Error", "Unable to open file.")
			Exit
		EndIf
		
		; Set File Type
		$working_file = GUICtrlRead($combo_ctl_search_file)
		FileWrite($file, $working_file)
		FileClose($file)  ; The File must be closed before you can copy it anywhere
		RunWait(@ComSpec & " /c " & "copy command " & $network_dir, $working_dir,@SW_HIDE)
		$command_get_begin = TimerInit() ;Start the timer until you can retrieve your results
		$process_results = True ; Set Processing flag
			
		
	Case $msg = $site_btn
		_IECreate ("www.combustory.com")
		
	Case $msg = $clear_btn
		_GUICtrlEdit_SetSel ($edit_ctl_tab2, 0, -1) 
		_GUICtrlEdit_ReplaceSel ($edit_ctl_tab2, "")


   EndSelect

WEnd

Step 11

Put all the steps together and see if it works - Well ok! I deviated from the quick guide, but if you follow this step and it WORKS! then you can revel. `,~) Now we just want to get it all to work. Check list of things that need to be in place:

  • Make sure the Arduino is plugged into the linux box and that the Serial monitor on the Arduino environment is not connected
  • Make sure you have loaded the Arduino program from Step 3
  • Now you are ready to start the CommandPoll script from Step 6
  • Make sure your Windows box can browse to the mapped drive and you can see the files on your linux box
  • Start the Arduino_Com_v.01.au3 program by pressing F5 and set the Network Dir to your mapped drive
  • Change the Working Dir if you do not want to use the Desktop (optional)
  • Now click the GO button and see if you get a response (it takes about 5 sec to respond)

If all goes well it will look something like this:

Arduino com pic 1.jpg

Links

Personal tools
Sponsers
Your Ad Here
Your Ad Here