Skip to content

Software 0. Installation

David Albrecht edited this page Apr 14, 2023 · 27 revisions

This documentation is for software version 0.2.0 and earlier. Click here to see this page in the latest docs.

Installation

This project is only supported on a Pi 3B+ or Pi 4B.

Custom Raspbian Lite Image Installation

My custom Pi OS image is ready to flash onto your SD card and make the initial setup and installation quicker and easier. See further down for manual installation steps if you do not want to use the custom image. Otherwise, grab your microSD card and continue reading!

  1. Download the Raspberry Pi OS image. (Click here)

    Note: SSH is enabled with the default SSH credentials (pi // raspberry). Please ensure your Pi is not exposed to the internet before connecting it to a network.

  2. Flash your microSD card or USB drive with the image. I recommend using the Raspberry Pi Imager, but other tools such as Rufus, EtcherIO, etc, will work.

  3. Prepare your network connection.

    • Wi-Fi: remove/re-insert your micro SD/USB drive into your PC. A drive should be mounted, called Boot. Inside the Boot drive, there is a file called rpi_power_monitor-wpa-supplicant.txt. Open this file with a text editor.

      Your Wi-Fi network is hopefully using WPA2 security, so find the WPA2 section and remove all the # from the beginning of the lines for the network definition. For example, the section looks like this before editing:

      ## WPA/WPA2 secured
      #network={
      #  ssid="put SSID here"
      #  psk="put password here"
      #}
      

      And afterwards, it should look like this:

      # WPA/WPA2 secured
      network={
        ssid="Your SSID"
        psk="yourSecurePassword"
      }
      

      Note: If your SSID is hidden, add the text scan_ssid=1 on a new line below the line with psk=

      Save and close the file, then safely remove the microSD card from your PC.

    • Wired connection: Simply plug your network cable into the Raspberry Pi (DHCP assumed).

  4. Insert the card to your Pi. You'll need a way to find the IP address of your Pi, and if you're not sure how to do that without a monitor, connect a monitor and keyboard to your Pi now. Then, power on your Pi.

  5. Your Pi should boot and connect to your network.

    Note your Pi's IP address by entering the command ifconfig | grep -E "eth0|wlan0" -A 1.

    If you connected your Pi to a cable, you should see your address below the eth0 interface. If you connected it to Wi-Fi, you should see your address below the wlan0 interface. In either case, the IP address is listed immediately after inet. Example: inet 192.168.1.123

  6. Ideally, you should assign a static IP address to your Raspberry Pi so that you can always reach it at the same IP address. This is also a necessity if you setup port forwarding on your network's router to reach the power monitor dashboard from outside your home network. Here's a guide on how to set a static IP address: (Static IP Address )

  7. Now is a good time to connect to the Pi via SSH. If you're not sure what SSH is, see here. (You'll need SSH eventually because keeping a monitor and keyboard connected to a project like this is not reasonable. If you don't know how to use it, now is the time to learn!)

    The credentials for SSH access use the default Raspberry Pi credentials: Username: pi Password: raspberry

  8. A systemd service file is already created for you - it just needs to be activated. This is the file that controls the automatic power monitor start on boot, and will restart the power monitor if it fails for whatever reason. I recommend waiting to enable the systemd service file until after you've calibrated your unit and are ready for final install - that way, you avoid the all-too-common mistake of starting the software manually in calibration mode when it's already running in the background.

    To enable the systemd service file, run the following commands:

    sudo systemctl enable power-monitor
    sudo systemctl start power-monitor
    

Manual Installation Steps

A fresh installation of Raspbian Lite is highly recommended before continuing. I do not recommend using the full version of Raspbian, or any boards older than a Pi 3B+.

In summary, the following steps will:

  • Update and upgrade your Pi
  • Install Python 3 & pip
  • Install Git
  • Install Nginx (optional, but recommended for viewing raw data - more on this in the project Wiki)
  • Install InfluxDB and Grafana
  • Download the source code for this project
  • Install the Python dependencies for the source code

You should assign a static IP address to your Pi and issue the following commands over an SSH connection. There are countless guides on the internet to do this...


  1. Update and upgrade your system:

     sudo apt-get update && sudo apt-get upgrade
    
  2. Install Python 3, Git, Pip, and Nginx:

     sudo apt-get install python3 python3-pip git nginx
    
  3. Enable the SPI interface on the Pi

     sudo raspi-config
    

    Use the arrow keys and the Enter key to:

    • Select "Interfacing Options"
    • Select "P4 SPI"
    • Enable the SPI interface
    • Press Tab twice to move the selector to Finish
  4. Modify the permissions of the webroot so that Python can write to it. If you're not using the pi username, be sure to change it before executing the commands below:

     sudo chown -R pi /var/www/html/
     sudo chgrp -R www-data /var/www/html/
     sudo chmod -R 750 /var/www/html/
    
  5. Update the Nginx configuration to turn file indexing on, and remove the default Nginx index file:

     sudo nano /etc/nginx/sites-enabled/default
    

    Find the section that looks like:

     location / {
              # First attempt to serve request as file, then
              # as directory, then fall back to displaying a 404.
              try_files $uri $uri/ =404;
              }
    

    ... and add autoindex on; underneath the try_files line. The block should look like this:

     location / {
              # First attempt to serve request as file, then
              # as directory, then fall back to displaying a 404.
              try_files $uri $uri/ =404;
              autoindex on;
              }
    

    Close your text editor with Ctrl-x, then a "Y" to save the file. Then, remove the default index.html file:

     rm /var/www/html/index.nginx-debian.html
    
  6. Reboot your Pi:

     sudo reboot 0
    
  7. Install Grafana and InfluxDB:

     sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
     sudo rm -f /etc/apt/sources.list.d/grafana.list
     echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
     sudo apt update
     sudo apt install -y grafana
    
     wget -q https://repos.influxdata.com/influxdata-archive_compat.key
     cat influxdata-archive_compat.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null
     echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main' | sudo tee /etc/apt/sources.list.d/influxdata.list
     sudo rm -f /etc/apt/sources.list.d/influxdb.list
     sudo apt update
     sudo apt install influxdb
    
  8. Download the source code for this project:

     git clone --single-branch -b master https://github.com/David00/rpi-power-monitor.git rpi_power_monitor
    
  9. Navigate into the rpi_power_monitor directory and install the Python library dependencies:

    cd rpi_power_monitor
    pip3 install .
    
  10. Create the service file:

     sudo nano /etc/systemd/system/power-monitor.service
    

    Paste the following contents, and save and close the file with Ctrl-x, y, enter

     [Unit]
     Description=Raspberry Pi Power Monitor
     After=network.target
    
     [Service]
     Restart=always
     RestartSec=1
     StartLimitInterval=120
     StartLimitBurst=5
     User=pi
     ExecStart=/usr/bin/python3 /home/pi/rpi_power_monitor/rpi_power_monitor/power_monitor.py
    
     [Install]
     WantedBy=multi-user.target
    
  11. Enable the service file:

     sudo systemctl enable power-monitor.service
    
  12. (Optional) Start the power monitor software in the background:

     sudo systemctl start power-monitor.service
    

    You may not want to start the power monitor in the background yet if you still have to do calibration! You can always stop the power monitor service with:

     sudo systemctl stop power-monitor.service
    

15. GPU Firmware Update (fixes start4.elf is not compatible)

After applying the kernel update, if your Pi fails to boot and displays the message start4.elf is not compatible on the screen, you'll have to manually upgrade the Pi GPU firmware files on your microSD card. Reinsert your card back into your computer, and open up the boot partition on the card. The following two files need to be replaced with newer versions: start4.elf and fixup4.dat

I've zipped the files here (RPI 4 GPU Firmware.zip), but if you want to get them from the source, see my comment here.

Once you have the two files, copy/paste them into the root level directory of the boot partition on your microSD card. It should ask if you want to replace the existing files, so answer yes.

After that, you should be able to boot your Pi.