GUIGPIO - A Java GUI GPIO Interface Program
Here are some HOW-TO instructions and Java and C 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 Java programs 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 the Java programs will compile and run under Java 11 or later.
Please direct all questions, comments, suggestions or requests to: pi at knutejohnson dot com.
Now that you have your DHT11/22 temperature/humidity sensor working it would be great to display that information on a web page and have it update automatically.
sudo apt update
sudo apt install apache2 apache2-doc
sudo a2enmod cgid
sudo cp dht11.pl /usr/lib/cgi-bin
sudo chown www-data:www-data dht11.pl
sudo chmod 750 dht11.pl
localhost/cgi-bin/dht11.pl
This article will explain how to take photos with your Pi camera and have them appear in a web page served up from the same Pi. This example updates the image every second. You can change both the rate at which the camera takes photos and the rate at which the HTML document requests a new image.
raspistill -f
Be advised you won't see any output if you have the vc4-kms-v3d
overlay enabled on a 3B+.
sudo apt update
sudo apt install apache2 apache2-doc
[Unit]
Description=Run raspistill for web page
After=network.target
[Service]
Type=simple
User=root
ExecStart=raspistill -n -tl 1000 -o /var/www/html/photo.jpg -t 0 -w 800 -h 600
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start camera
<!DOCTYPE html>
<html lang="en_US">
<head>
<meta charset="utf-8">
<title></title>
<style>
body { font-family: sans-serif; }
h1 { text-align: center; }
img#photo {
display: block;
margin-left: auto;
margin-right: auto;
width: 800px;
height: 600px;
}
</style>
<script>
setInterval(function() {
var myImage = document.getElementById("photo");
myImage.src = "photo.jpg?r=" + Math.random();
},1000);
</script>
</head>
<body>
<h1>Reloading Photo</h1>
<img id="photo" src="photo.jpg" alt="Reloading Photo">
</body>
</html>
sudo systemctl enable camera
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 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.
The Intercom program is a simple voice over IP intercom that will send
voice data to another copy of the Intercom 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.