CIS 542 - Summer 2011

Group Project Assignment #1

July 14, 2011

Introduction
In this assignment, you will begin working on your group project by setting up the basic communication between the sensor/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, there is nothing to submit and 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.


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
  Serial.begin(9600);
}

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

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.

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).

Now 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!


Academic Honesty
You should be working with the other two members of your group project team on this assignment. However, you may not discuss or share solutions with students in any other groups, nor should you be receiving any help from outside sources, including students not taking this course or online resources. If you run into problems, please ask a member of the teaching staff for help.


Submission
You do not have to submit anything for this assignment, since all three parts will eventually find their way into your group project. However, it is recommended that you finish this assignment within a week (before July 21) so that you can give yourself more time to make the necessary modifications for the project, as well as implement your Android front-end.


Updated: Wed, July 13, 3:20pm