Skip to content

Instantly share code, notes, and snippets.

@CharlesGodwin
Last active December 1, 2023 13:37
Show Gist options
  • Save CharlesGodwin/adda3532c070f6f6c735927a5d6e8555 to your computer and use it in GitHub Desktop.
Save CharlesGodwin/adda3532c070f6f6c735927a5d6e8555 to your computer and use it in GitHub Desktop.
How to set up the WaveShare Raspberry Pi PoE HAT Type (B) with display
#!/bin/bash
# 2022-01-01 Charles Godwin
# Derived from https://www.waveshare.com/wiki/PoE_HAT_(B)
# enable i2c interface if necessary
# Read the following for details
# https://gist.github.com/CharlesGodwin/adda3532c070f6f6c735927a5d6e8555#file-waveshare-poe-hat-md
#
IS_I2C=`sudo raspi-config nonint get_i2c`
[ $IS_I2C -ne 0 ]&&sudo raspi-config nonint do_i2c 0
# install required packages
sudo apt update
sudo apt install -y python3-pip python3-pil libatlas-base-dev
sudo pip3 install RPi.GPIO smbus numpy
# cleanout artifacts if necesary
[ -d ~/PoE_HAT_B_code ] && rm -rf ~/PoE_HAT_B_code
[ -f ~/PoE_HAT_B_code.7z ] && rm -rf ~/PoE_HAT_B_code.7z
# install Waveshare libraries and examples
wget https://www.waveshare.com/w/upload/8/8b/PoE_HAT_B_code.zip
unzip -o PoE_HAT_B_code.zip -d .
# move required parts to ~/.poe-hat directory
[ -d ~/.poe-hat ] && rm -rf ~/.poe-hat
mkdir -p ~/.poe-hat
mv PoE_HAT_B_code/python/examples/main.py ~/.poe-hat/
mv PoE_HAT_B_code/python/lib/waveshare_POE_HAT_B ~/.poe-hat/
mv PoE_HAT_B_code/python/readme_EN.txt ~/.poe-hat/PoE_HAT_B_readme.txt
# cleanup
rm PoE_HAT_B_code.zip
rm -rf PoE_HAT_B_code
rm -f $0
#
# define and enable service
#
PYTHON=`which python3 |head -n1`
cat <<EOF >poe-hat.service
[Unit]
Description=Waveshare Poe Hat B
After=network.target
[Service]
Environment=systemd=true
ExecStart=${PYTHON} ${HOME}/.poe-hat/main.py
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
EOF
sudo mv -f poe-hat.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable poe-hat.service --now
sudo systemctl status poe-hat.service --no-pager

I have reviewed this git project and think it's a superior solution. Try it!

This script has been tested on Raspberry Pi OS, Buster (Legacy) and Bullseye only. This installation is simpler that the Waveshare provided instructions.

The supplied program will control the fan but for it to work you must slide the FAN switch on the HAT to the P0 position.

To use the script you can download the zip file and unzip it or, copy and paste the RAW version setup-poe-hat.sh and paste it into an editor. I reccomend you use nano setup-poe-hat.sh in a Pi terminal or ssh session.

Then run bash setup-poe-hat.sh

When finished it will delete itself and any files needed only for installation. This script can be rerun. It will do a new install of the Waveshare software.

When complete a systemd service named poe-hat.service will run and start on every boot.

to remove run these commands

sudo systemctl disable poe-hat.service
sudo systemctl stop poe-hat.service
rm -rf ~/.poe-hat
@klamann
Copy link

klamann commented Oct 24, 2023

well, if you've been using @CharlesGodwin's script before, you can stop this service and deactivate autostart with sudo systemctl disable poe-hat.service --now. But depending on your distribution and other things you have modified on your system, this might not do the trick. If you're struggling with this, you can ask the community on https://raspberrypi.stackexchange.com/

@qatar2030
Copy link

Now after re-install OS lite 64 on RP4 both of @CharlesGodwin's script and your script NOT working any more any help please?

@CharlesGodwin
Copy link
Author

CharlesGodwin commented Oct 27, 2023

"NOT working" is not very informative. How is it not working? error in the install? screen not lighting up? fan not working? send whatever you can. If my script doesn't work, go to "https://www.waveshare.com/wiki/PoE_HAT_(B)" and follow their instructions. If that doesn't work, contact them.

@jhud1030
Copy link

well, if you've been using @CharlesGodwin's script before, you can stop this service and deactivate autostart with sudo systemctl disable poe-hat.service --now. But depending on your distribution and other things you have modified on your system, this might not do the trick. If you're struggling with this, you can ask the community on https://raspberrypi.stackexchange.com/

Thanks @klamann! I got this up and running, Works great! My CPU temp is up a few degrees since changing over but I also decided to use a USB thumb drive as a boot device so I'm not sure which has caused the increase in temp. When the fan is on, is it running at 100%?

@jackra1n
Copy link

jackra1n commented Nov 21, 2023

Hey guys!
I was also looking for a better alternative to the PoE HAT script. Unfortunately I did not find any alternatives that fully satisfy my needs.
So after seeing what @klamann created I got inspired and made my own version written in Rut 😃
Because it's written in Rust it has very low resource usage (I'm seeing less than 4MB memory and barely any cpu)
If you want try it out and let me know what you think!
https://github.com/jackra1n/RustBerry-PoE-Monitor
IMG_3890

@klamann
Copy link

klamann commented Dec 1, 2023

@jackra1n this looks really good! makes my implementation pretty much obsolete. Well, at least my idea for the UI lives on 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment