Brad's Raspberry Pi Blog

Thursday, June 11, 2015

Microsoft's sample documentation for Windows IoT on the Raspberry Pi 2 includes a C# example that shows how to use a timer to blink an LED (called Blinky). To get started with Windows 10 IoT Core and Windows Universal applications, I wanted to try something even simpler. I also wanted to use C++. Below is a very minimal example that shows how to use a XAML RadioButton to turn an LED off and on.

This example assumes that you have Windows 10 IoT Core set up already and have configured Visual Studio to work with your device. This is covered in the MS getting started documentation. For what it's worth, I'm using the preview version of Visual Studio 2015 installed on a Windows 10 preview VM running under Parallels on my Mac.

Connections

For this example, connect the LED's anode (positive pin/longer pin) with an appropriate resister to the RPi's 3.3V and the cathode (negative/shorter pin) to the the RPi 2's GPIO 5 pin. Setting pin 5 to high will turn off the LED and setting it to low will turn it on.

Add Reference to Visual Studio Project

You will need to add the reference to the Windows IoT Extension SDK to be able to use GPIO. In the Solution Explorer, right click on References and select "Add Reference." Under Windows Universal, click on "Extensions." Check the box for the "Windows IoT Extension SDK." Note that you have to mouse over the entry in the list to see the checkbox.

Additional Notes:

Remember to set architecture to ARM via the dropdown near the top of the Visual Studio window.

You won't be able to test/debug a program that uses GPIO on your local development computer.

You may need to set the computer name for your RPi running Win 10 IoT in the Debugging section of the project's properties. This should match the device name that shows on the screen for your RPi after it boots.

Make sure that authentication is turned off in the project's debugger settings.

So far, I have only run the code via the debugger. I haven't deployed the application as the starter on my RPi 2.

Tuesday, July 15, 2014

Mono is an open source implementation of the Microsoft .NET framework that allows you to compile and run .NET programs on non-Windows platforms. That includes the Raspberry Pi. It actually proved to be quite easy to install Mono (v. 3.2.8) on the RPi and get some C# code running. This example shows how to call the i2cget utility from a C# program to read the temperature from a TMP102 and print the results in degrees Celsius and Fahrenheit in the terminal window.

Saturday, July 12, 2014

Here is an example of using PHP to read the current temperature from a TMP102 I2C temperature sensor and display it (in degrees Fahrenheit) in a Web page.

Prerequisites

Make sure that you have installed the i2c-tools package because this package provides i2cget to read the registers on the TMP102. The following apt-get command will install it for you:

sudo apt-get install i2c-tools

Install the Apache2 Web server:

apt-get install apache2

Then install PHP5:

apt-get install php5

Granting Access to I2C System Device

By default, and for the sake of security, the account used to run the Apache2 Web server doesn't have permission to access the /dev/i2c-1 device. To fix this, edit the udev configuration file that controls the file permissions for /dev/i2c-1.

Use nano or your favorite text editor to edit /lib/udev/rules.d/60-i2c-tools.rule. Replace the contents of the file with these two lines.

KERNEL=="i2c-0", GROUP="i2c", MODE="0660"

KERNEL=="i2c-1", GROUP="i2c", MODE="0666"

This leaves the i2c-0 device with the default permissions and changes the access to i2c-1 to allow a non-privileged user to read the device.

PHP Code

Here is a simple PHP page that reads the temperature data using i2cget, converts it to degrees Fahrenheit and displays it. Put this code in /var/www/temp.php -

<?php

$output = `/usr/sbin/i2cget -y 1 0x48 0 w`;

$msb = hexdec(substr($output, 4, 2));

$lsb = hexdec(substr($output, 2, 2));

$tempF = ((($msb << 8) | $lsb) >> 4) * 0.0625 * 1.8 + 32;

echo "Temp: $tempF&deg;F";

?>

The backticks (` `) tell PHP to run the enclosed command in a (Linux) command shell. The backtick is on the key to the left of the 1 key near the top of the keyboard (on the same key as the ~).

-y cancels the user prompt that asks if you really want to read from the device

1 actually refers to the 2nd I2C bus, /dev/i2c-1

0x48 is the default I2C device address for the TMP102

0 is the register with the temperature data

w means we want to read a word (rather than just a byte).

Note that when i2cget reads the data, the byte order is reversed. The first byte is actually the least significant byte (LSB) and the second byte is the most significant byte (MSB). The string returned by i2cget is something like 0xb01d. The code uses substr() to pull out the two digits for each byte, and then hexdec() to convert a string representing a hexadecimal value to a decimal numeric value.

From a computer on the same network, access the URL for the page (where the IP address will vary):

The Java code below reads current temperature data from the TMP102 temperature sensor using the i2cget command line utility to read the TMP102's registers. The reading is taken every second and the result is saved to a MySQL database table. The table contains a column for the date and time and a column for the temperature in degrees Fahrenheit. The code below should work with Java 6, 7, or 8. You will need to have the complete JDK installed to compile the code.

Prerequisites

The i2cget command line utility is part of the I2C Tools package. You can install this package on the Raspberry Pi using the following apt-get command:apt-get install i2c-tools

To install MySQL, use the following command and follow the prompts:

apt-get install mysql-server

The following command installs the Java database (JDBC) driver that allows your code to access the database:

javac -cp /opt/mysql-connector-java-5.1.31/mysql-connector-java-5.1.31-bin.jar Tmp102.javaAdjust the -cp path for the MySQL-Connector .jar file to match your installation, if needed.The following command runs the program. The temperature readings are printed to the terminal, while the current timestamp and temperature are saved in the database. java -cp .:/opt/mysql-connector-java-5.1.31/mysql-connector-java-5.1.31-bin.jar Tmp102Again, adjust the -cp path to match the location of your MySQL-Connector .jar file.Stop the program by pressing Ctrl-C.

Saturday, July 5, 2014

If you want to connect to a MySQL DB server instance running on a Raspberry Pi, a couple bits of additional configuration are required.

On the Raspberry Pi, use nano or another text editor to edit the /etc/mysql/my.conf file. Under the [mysqld] section, edit the bind-address to match the RPi's IP address, if you wish to restrict access to one IP interface's address or set it to 0.0.0.0 to all access using the IP address for any available network interface.

To allow remote access via the MySQL root login, (re)run the mysql_secure_installation script and answer the relevant question about remote access for root appropriately. The following MySQL command (issued at the MySQL command line) has the same effect. Change the password as appropriate.

grant all privileges on *.* to 'root'@'%' identified by 'MyRootPassword' with grant option;

To install the RXTXComm serial library for Java, run the following apt-get command:

apt-get install librxtx-java

Last year, I posted a similar example that uses MySQL, but since JavaDB (also known as Derby) comes with the Java 8 installation, it is very convenient and easy to use.

Configuring JavaDB

While JavaDB comes with the Java 8 JDK, a small amount of configuration is needed. JavaDB can run in embedded or in network server mode. In this example, I am running it as a network server so that it can be accessed by more than one application running in different Java virtual machines.

Make sure that your JAVA_HOME environment variable is set. If you used the Adafruit tutorial above and have the JDK installed in /opt/jdk1.8.0, add the following line to your .bashrc file. If you have your JDK installed in a different location, adjust as needed.

export JAVA_HOME=/opt/jdk1.8.0

Then add the following lines to set DERBY_HOME and adjust your path to include the JavaDB executables.

export DERBY_HOME=$JAVA_HOME/db

export PATH=$PATH:/$JAVA_HOME/db/bin

Run source ~/.bashrc to load the settings from your edited .bashrc file.

Edit your java.policy file to allow access to port 1527. If you have the JDK installed in /opt/jdk1.8.0, your policy file should be /opt/jdk1.8.0/jre/lib/security/java.policy. Add the following line to this file before the closing bracket of the grant block -

permission java.net.SocketPermission "localhost:1527", "listen";

Now start the JavaDB server with the following command:

/opt/jdk1.8.0/db/bin/startNetworkServer &

It may take a moment to start, but the output should indicate that the security policy has been applied and that the server is now running on port 1527.

For this example, I used the ij client utility to connect to JavaDB, create the gpsdb database, and create the gps_readings table. I won't go into a lot of detail about ij, but you can find good documentation online. With the path to the JavaDB/Derby binaries included in your path, you can simple run the ij command to start the command line DB client.

To create the database, I used the following connect statement at the ij> prompt:

connect 'jdbc:derby://localhost:1527/gpsdb;create=true';

I ran the following SQL at the ij> prompt to create the gps_readings table:

When the program runs, the GPS data read from the Copernicus II is printed out in the terminal window and records are inserted into the gps_readings table. You can use ij to query the data in the database. Since JavaDB is running in network server mode, you can access it using ij while the Java Gps program is running.