CIS 542 - Spring 2012

Lab Assignment #5

February 23, 2012

In this assignment, you will begin working on your group project by setting up the basic communication between an Arduino device, a server application written in C, and a client application written in Java.

You should work with members of your project team on this assignment. Since the work you do today will be part of your group project, this assignment will not be graded on its own. However, you should use this opportunity to make sure that you have the "communication" aspects of your system working correctly, and submit whatever work you have done at the end of the lab time.

Note: in the interest of time, you may want to have some members of your team work on Part 3 while others work on Parts 1 and 2.

Before you begin
Follow the "Getting Started with Arduino" tutorial for Windows or Mac OS X and be sure that you are able to get the "blink" program running on the board. We do not recommend using Linux for this step or for Part 1, as you may need root privileges for some functionality.

Part 1
First, you will create a simple Arduino program that writes data over the serial port. The following program reports the number of milliseconds that it has been running by sending the value to the serial port once every second:

unsigned long time;

// This method is called once
void setup(){
  // sets the transfer rate to 9600 baud

// This method is called repeatedly
void loop(){
  Serial.print("Time: ");
  time = millis();
  //prints time since program started
  // wait a second so as not to send massive amounts of data

Using the Arduino development environment that you installed, compile this program and then load it onto the board.

Then click on the "Serial Monitor" icon (the rightmost icon on the toolbar) and after a few seconds you should see the "time" output coming from the board.

At this point, your Arduino board is ready to start communicating with the outside world!


Part 2
Attach your Arduino board to a Linux or Mac machine. Find which serial port it is using by looking in the /dev directory for a recently-created file that will probably be named ttyUSB0 on Linux, and cu.usbserial-A9007LP1 on Mac.

Write a C program that gets the input coming from the Arduino board by reading from the serial port. Don't forget to configure the port for the appropriate baud rate using the cfsetispeed and tcsetattr commands. If you do not know how to read from the serial port, look at Tuesday's lecture notes.

Make sure that you include all of these headers when writing your C program:

#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

Take the input coming from the board and write it to the screen using printf. You should see the increasing values of time appearing once per second. Now you have integrated the Arduino board and a C program!

Note: If, for whatever reason, you cannot get your Linux/Mac machine to see the Arduino board, you can use this program to randomly generate temperature readings and write them to a file once a second. Then you can write a C program that reads from that file and deal with the Arduino component later.


Part 3
Modify the code from Part 2 so that it uses two threads. One thread should continue to read from the serial port and update a global variable with the value (i.e., the time that the board has been running) that is being read.

The other thread should create a socket server that listens for incoming connections on port 12345. You should use this skeleton code to get started (there is also client skeleton code in case you're interested, though you won't need it for this assignment).

Whenever the server receives a connection, it should simply send back the last value read from the Arduino board, close the connection, and then wait for another one (as opposed to streaming the data continuously to the same client, but you can do that, too, if you so prefer).

Last, write a Java program that makes a socket connection to port 12345, gets the reading over the network from the C program, and writes it to the screen using System.out.println. For simplicity, run the C and Java programs on the same Linux/Mac machine (i.e., the one into which you plugged the Arduino board).

Now the values on your Arduino board can be accessed "remotely" by a Java program! Your group project is off to a great start!

It is recommended that you finish this assignment during lab time today, while you have the help of the TAs. Please make sure that one member of your group submits all code (Arduino, C, Java) in a single .zip or .tar file in Blackboard so that we can check your progress.

Updated: Mon, Feb 20, 3:08pm