knutejohnson.com/pi

Programs for the Raspberry Pi

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 8 or later except the IPConfig program which requires Java 11.

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

How To Display DHT11 Data On a Web Page

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.

dht11.pl Running in browser
  1. Install apache, the apache documentation and enable CGI:
    sudo apt update
    sudo apt install apache2 apache2-doc
    sudo a2enmod cgid
                    
  2. Copy this Perl script, dht11.pl, to your /usr/lib/cgi-bin directory.
  3. sudo cp dht11.pl /usr/lib/cgi-bin
                    
  4. Set the ownership and permissions on the dht11.pl file:
    sudo chown www-data:www-data dht11.pl
    sudo chmod 750 dht11.pl
                    
  5. Point your browser to:
    localhost/cgi-bin/dht11.pl
                    

How to Automatically Update a Photo on a Web Page from your Pi Camera

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.

  1. Enable the camera interface with Raspberry->Preferences-> RaspberryPi Configuration->Interfaces Tab or with raspi-config.
  2. Install your camera and test it to confirm that it is working. A simple test is to run this command:
    raspistill -f
                    
    Be advised you won't see any output if you have the vc4-kms-v3d overlay enabled on a 3B+.
  3. Install apache and the apache documentation.
    sudo apt update
    sudo apt install apache2 apache2-doc
                    
  4. Create a systemd service file called camera.service and save it to the /etc/systemd/system directory. This file is owned by root so you need to sudo.
    [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
                    
  5. Run the command:
    sudo systemctl daemon-reload
                    
  6. Start the service running:
    sudo systemctl start camera
                    
  7. Create the following as index.html in /var/www/html. This directory and file are owned by root so you need to sudo.
    <!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>
                    
  8. Point a browser on your Pi to http://localhost
  9. To have the camera service start when the computer is started:
    sudo systemctl enable camera
                    

IPConfig

IPConfig IPConfig is a Java GUI program to set the IP parameters of a RaspberryPi running Buster. This program requires Java 11 and must be run as root. I don't think this program will run on a Pi 1 or 0 because of no Java 11 being available for either. If you've already hacked up your /etc/dhcpcd.conf file you can download an untouched one below.

sudo java -jar IPConfig.jar

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.