Monday, June 08, 2015

JAVA DHT11 sensor with Raspberry Pi

Hello there,

DHT11 sensor
This is the review on Lesson 22 DHT11 Sensor of the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". I have to admit that Java Native Interface is hard stuff. Even though with time and being stubborn enough you can do anything. I think the main issue with JNI is the lack of documentation. I may do a JNI tutorial later on. However for this exercise I created a JNI script which makes the process of building and installing the native library much easier.

I found a couple of good sources which mixed together made a nice simple cocktail. From the GreenHouse project I found a good example about how to use JNI with a small C code library. Their C code did not work with my sensor. I think the sensor they used might be slightly different because the initialization timings before the readings are a bit different from the ones that I had from the Sunfounder sensors kit C code 20_DTH11. So I just had to refactor the GreenHouse dhtreader.c code with the sunfounder's 20_DHT11. All what was left then was a little bit of polishing.

Sample readings
I researched about JNI and how to compile and install the library in the Raspberry Pi. I first found out that I had to use the Java arm openJdk 7 instead of the Oracle Jdk 7. Looks like the oracle one have no JNI capabilities. After that was sorted out and few hours of banging my head with the desk I came up with the script jniDHT11SensorReaderBuilder.sh which will compile and install the native library into the default location of the "java.library.path" property. There are many locations for this property, in this case I choosed /usr/lib. If you want to give it a try just check out the project into your Raspberry Pi with the command:

    git clone https://github.com/marcandreuf/sunfounder-sensors-raspi-4j.git

Then navigate to the project source folder "src/main/java" and if you have the Java OpenJdk 7 in the default location (check the script for more details) you should be able to run the script to install the native library with:

   sudo sh jniDHT11SensorReaderBuilder.sh


After few seconds the library should be installed and you all ready to run the Java class to test a single reading. Just connect the DHT11 sensor to GPIO01 and run the command:

   sudo java org.mandfer.dht11.DHT11SensorReader

You should get a simple reading of the temperature and humidity. If it is all fine and you want to check a better example which read values every 1.5 seconds try to run the exercise 20 from the project root folder with:

   sh runPi.sh org.mandfer.sunfunpi4j.Ex20_DHT11_Native

That is all for now, 
Thanks for reading. If you have any issue, leave me a comment below.



For full details about how to set up your Raspberry Pi and how to run these exercises please check out my old post Raspberri Pi with Pi4j and Junit testing mockups. Or leave me a comment if you have any issue or question.

Posted by Marc Andreu.

No comments: