knutejohnson.com/pi

Java Programs for the Raspberry Pi

Here are some HOW-TO instructions and Java programs I have written for the Raspberry Pi. They include a thermometer program to view remotely sensed temperatures and display them on a local touch screen, an intercom program and a program to detect motion and capture images with a Raspberry Pi camera.

Javadocs for the complete library are located here and the complete library may be downloaded here. Extract the library file in your ~/bin directory and you can use the included build scripts.

All of these programs require Java 8 but that is already installed on your Raspbian running Pi.

Please direct all questions, comments, suggestions or requests to: pi at knutejohnson dot com.

HOW-TO Use a DHTXX Temperature/Humidity Sensor With the dht11 dtoverlay

DHT Wiring Diagram
  1. Turn off your pi and disconnect the power!
  2. Connect the + lead of your DHT sensor to the 3.3V pin, connect the - lead to a GND pin and the signal/out/data pin to GPIO 4 (you can use others but to keep this simple use GPIO 4 for now, see /boot/overlays/README). If you are using the sensor on the little PC board you don't need a pull up resistor. If you are using the bare sensor you need a 5.1K ohm resistor between the + and the signal wire. A 4.7K to a 10K will probably work just fine. This overlay works with the DHT11, DHT22 and AM2302 sensors and probably some other similar ones.
  3. If you want to use a 5 volt supply on a bare sensor so you can put the sensor on a longer wire then you need to use two resistors one about twice the value of the other is connected between the - lead and the signal/out/data lead and the other is connected between the signal/out/data lead and the + lead. I tested this with a 10K and 5.6K resistor pair and a 3.3K and 1.5K pair. I didn't try it down a long wire though.
  4. Power up your PI
  5. Edit the /boot/config.txt file and add this line: dtoverlay=dht11
  6. Reboot your Pi
  7. The temperature will be found in the file: /sys/bus/iio/devices/iio:device0/in_temp_input
    and the humidity will be in the file: /sys/bus/iio/devices/iio:device0/in_humidityrelative_input

There is one caveat with this technique. When you read the temperature file you will occasionally get an error/exception. I think this is because the overlay software is writing to the file when you are trying to read it but that is only a guess on my part. So you should wrap your reading code in a loop to make sure you successfully read the data. I normally read the temperature first and so if that is successful I rarely get an error reading the humidity but I assume it is possible.

The data you read from the file(s) is a 5 character string that needs to be converted to a decimal value. Convert both strings to a floating point type and divide both the temperature and humidity by 1000.0 to get the actual values. Temperature is in degrees Celsius. If you prefer Kelvin you will have to make the appropriate adjustment.

Even after accounting for the occasional error reading the files I have seen some bad data show up but only every few days of reading the files every few minutes.

Here are three programs (pick your favorite language) to read the data files. I'm not a python guy so if there is a better way to do this with python, send me a note and I'll change it out.

Thermometer

Thermometer Program - TempPanel Thermometer - SelectPanel Thermometer is a Java program to run on a Pi with a touch screen attached (ie UCTRONICS 3.5 inch touch screen) to display the temperature collected from up to six remote Pis running the TemperatureSender program.

The TemperatureSender?? programs read the temperature from a DHTxx or DS18B20 device attached to a Pi and sends the temperature via multicast packet to the Thermometer program.

Since the TemperatureSender?? programs use the dht11 or 1 wire device tree overlay to collect the temperature data from the sensor you need to enable it in /boot/config.txt with the line:

dtoverlay=dht11

or

dtoverlay=w1-gpio

Run the TemperatureSender?? program every minute using the following entry in your crontab:

java -jar TemperatureSenderDHT11.jar "Project Room" 0

or

java -jar TemperatureSenderDS18B20.jar 6774267 "Project Room" 0

I have tested the Thermometer program on a 1B+, a 3B and a 3B+. The program runs fine on all models however I did have some problems with using WiFi on the 1B+. With an ethernet connection to the local network it ran flawlessly on the 1B+. The TemperatureSender?? programs will run on any Pi, I'm currently testing with two 1Bs, an A+ and a B+ all connected to the network with WiFi.

Intercom

Intercom Program The Intercom program is a simple voice over IP intercom that will send voice data to another copy of the Intercom program.

MotionDetection

MotionDetection Program The MotionDetection program continuously takes images with the Raspberry Pi camera and displays them on the program window. If motion is detected a green dot indicator is displayed in the upper right corner of the screen. The images where motion is detected may be captured to storage or emailed. Also, as in the photo, a composite image may be displayed consisting of the previous image and a set of blue boxes showing where motion was detected in the image.