Sunday, April 28, 2013

Reading Temperature from a TMP102 Thermometer with pcDuino (C Version)

My previous post on reading the temperature from a TMP102 using a pcDuino used a Python script. Here is an example using C. For information on the connections and prerequisites, see the Python version of this post: Reading Temperature from a TMP102 Thermometer with pcDuino (Python Version).

Code


#include <stdio.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>

int main (void)
{
int deviceHandle;
int readBytes;
char buffer[2];
// initialize buffer
buffer[0] = 0x00;
int deviceI2CAddress = 0x48;
// open device on /dev/i2c-2
if ((deviceHandle = open("/dev/i2c-2", O_RDWR)) < 0) {
printf("Error: Couldn't open device! %d\n", deviceHandle);
return 1;
}
// connect as i2c slave
if (ioctl(deviceHandle, I2C_SLAVE, deviceI2CAddress) < 0) {
printf("Error: Couldn't find device on address!\n");
return 1;
}    
// begin transmission and request acknowledgement
readBytes = write(deviceHandle, buffer, 1);
if (readBytes != 1)
{
printf("Error: Received no ACK-Bit, couldn't establish connection!");
}
else
{
// read response
readBytes = read(deviceHandle, buffer, 2);
if (readBytes != 2)
{
printf("Error: Received no data!");
}
else
{
// get data
int msb = buffer[0];
int lsb = buffer[1];
double temp = (((msb << 8) | lsb) >> 4) * 0.0625;
// and print results
printf("Temp: %0.2f C\n", temp);
}
}
// close connection and return
close(deviceHandle);
return 0;
}

Compiling & Running


Compiling the program is easy. Assuming you have saved the code to a file named tmp102.c, change directory to the location of the code file and compile using gcc -

  gcc tmp102.c -otmp102

This produces an executable named tmp102.  Run the program by typing ./tmp102 

Reading Temperature from a TMP102 Thermometer with pcDuino (Python Version)

The TMP102 temperature sensor breakout from Sparkfun is an easy to use thermometer that connects via I2C.   This quick example shows how to use a TMP102 with a pcDuino and some simple Python 2 code to get the current temperature.

Prerequisites 


Run the following commands to install required packages:

  sudo apt-get install i2c-tools
  sudo apt-get install python-smbus


Connections


TMP102  pcDuino
GND     GND
SCL     SCL
SDA     SDA
V+      3.3V
ADD0    GND

When connected as above, the TMP102 uses the I2C address 0x48. To confirm this, run the following at the command line:

  i2cdetect -y 2

The resulting diagram should show the device at 0x48 by printing 48 in the corresponding cell.

Note that the SCL and SDA pins on the pcDuino are connected to I2C bus 2.


The TMP102 breakout board has built-in 1k pull-up resistors on the SDA and SCL lines; it works without external pull-up resistors.

Code


import smbus
import time

bus = smbus.SMBus(2)
data = bus.read_i2c_block_data(0x48, 0)
msb = data[0]
lsb = data[1]
# Print degrees Celsius
print (((msb << 8) | lsb) >> 4) * 0.0625


Saturday, April 27, 2013

Installing the Java Runtime Environment on the pcDuino

Here are my notes on installing JRE 1.7 on the pcDuino development board.

Before installing Java, I upgraded the Linux kernel to 3.4.29+  and Ubuntu following the steps described on the pcDuino 04-04-2013 update page.

Download the JRE (ejre-7u21-fcs-b11-linux-arm-vfp-client_headful-04_apr_2013.gz) from http://www.oracle.com/technetwork/java/embedded/downloads/javase/index.html. If you have a screen, keyboard, and mouse connected to your pcDuino, you can dowload directly to the device. I am using SSH to access my pcDuino, so I downloaded the file to my Windows desktop and used WinSCP to transfer the file to the pcDuino.

At the pcDuino command prompt, run the following commands to extract the JRE:

gunzip ejre-7u21-fcs-b11-linux-arm-vfp-client_headful-04_apr_2013.gz

tar xfv ejre-7u21-fcs-b11-linux-arm-vfp-client_headful-04_apr_2013

The JRE is now located in a subdirectory called ejre1.7.0_21. Note the full path for this directory.  

Add the following lines to your account's .bashrc directory (changing the path to the JRE directory path noted in the last step).


export JAVA_HOME=/usr/lib/jvm/ejre1.7.0_10
export PATH=$PATH:$JAVA_HOME/bin


This version of Java uses "soft float, so install the required libraries using this command -


sudo apt-get install -y libc6-armel libsfgcc1

Log out and then log back in so the new environment variables take effect. Confirm that Java runtime now works by running java -version