Skip to content

Instantly share code, notes, and snippets.

@CharlesGodwin
Last active December 1, 2023 13:37
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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
@qatar2030
Copy link

ill try it

@qatar2030
Copy link

would somebody helping me to let it work please?
do I have to flow supplier on this https://www.waveshare.com/wiki/PoE_HAT_(B) and then flow this step?
I get some error.
I don't know from where I should start and I'm new in this field.

@CharlesGodwin
Copy link
Author

when you say "flow" do you mean "follow"? If you do, then you should just follow my script setup-poe-hat.sh and ignore the other instrcutions.

If you get an error try to send it to me here.

@qatar2030
Copy link

.poe-hat/main.py

how i can access it please

@CharlesGodwin
Copy link
Author

You need an editor. I use the comand line editor named nano.
I do not have the skill to instruct you as a beginner. I recommend you search for online training for Using a Raspberry Pi/

@qatar2030
Copy link

can you show me yours (line 20) please?

@CharlesGodwin
Copy link
Author

CharlesGodwin commented Feb 17, 2023

Here is the entire program on my machine

     1  import time
     2  import sys
     3  import os
     4
     5  libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
     6  if os.path.exists(libdir):
     7      sys.path.append(libdir)
     8
     9  import logging
    10
    11  from waveshare_POE_HAT_B import POE_HAT_B
    12
    13
    14  logging.basicConfig(level=logging.INFO)
    15
    16  POE = POE_HAT_B.POE_HAT_B()
    17
    18  try:
    19      while(1):
    20          POE.POE_HAT_Display(43)
    21          time.sleep(1)
    22
    23  except KeyboardInterrupt:
    24      print("ctrl + c:")
    25      POE.FAN_OFF()

@qatar2030
Copy link

Untitled
I did it Charles thank you man

@CharlesGodwin
Copy link
Author

good for you

@JavDMBrett
Copy link

Guys anyone know why I only see the Ethernet IP Address port displayed and not the WLAN0 IP Address, or how to change it.
We have an image that automatically gives the eth port an ip address does not matter where you are school, home etc...192.168.137.2.
I do not want to see this display but just the wlan0 ip address. I ´have the latest Bullseye OS. on a Raspberry Pi. these OS is setup to be used in our school. I have PoE HAT (B). on top...

@CharlesGodwin
Copy link
Author

If you unplug the ethernet cable and wait a minute or less, it will show the wan0 address. The code grabs the IP address of the device that connects to a google DNS server (8.8.8.8). I have no idea how to change that, python socket software is not my speciality.

@JavDMBrett
Copy link

JavDMBrett commented Apr 7, 2023 via email

@CharlesGodwin
Copy link
Author

I don't know what to say. I just built a new pi image and install the latest version of Waveshare following the instructions in this gist (they were changed recently) When I start-up with no ethernet connected it shows the wlan0 IP, when I plug in an ethernet cable it soon switches to eth0 value. when I remove the cable, it then shows the wlan0 again.

@JavDMBrett
Copy link

JavDMBrett commented Apr 7, 2023 via email

@mpietruschka
Copy link

mpietruschka commented Apr 11, 2023

Hey,

I tried another approach.

Personally I don't like having a lot dependencies around - like python. Especially for such a simple task like setting a string or turning the fan on and off. For that reason I wrote a very simple c++ which program you are able to call in your shell.

It's possible to view up to 4 rows. The text size is adapted automatically.

It comes with a very simple bash script that you can customize in any way you like. It simply calls that c++ program and doesn't need to be recompiled.

In case you are running docker, you might want to try the alpine based image.

Here are some example calls..

foo@bar:~$ sudo ./waveshare-poe-hat-b-cli fan on
foo@bar:~$ sudo ./waveshare-poe-hat-b-cli fan off
foo@bar:~$ sudo ./waveshare-poe-hat-b-cli oled "row1"
foo@bar:~$ sudo ./waveshare-poe-hat-b-cli oled "row1" "row2"
foo@bar:~$ sudo ./waveshare-poe-hat-b-cli oled "row1" "row2" "row3"
foo@bar:~$ sudo ./waveshare-poe-hat-b-cli oled "row1" "row2" "row3" "row4"

https://github.com/pietmacom/waveshare-poe-hat-b-cli
https://hub.docker.com/r/pietmacom/waveshare-poe-hat-b-cli

Hope that helps!

Marti

@CharlesGodwin
Copy link
Author

CharlesGodwin commented Apr 11, 2023

Wow! I like that. It has possibilities.
@mpietruschka Thanks

UPDATE: Deleted unnecessary text.

@klamann
Copy link

klamann commented Oct 15, 2023

Hey everyone, I built a thing, might be of interest for some of you: https://github.com/klamann/raspi-poe-mon

I wasn't happy with the information the example library was giving us (oh, the fan is on? thanks, I didn't notice 😆), so I decided to make better use of the limited screen space ;)

@CharlesGodwin
Copy link
Author

Looks great. I'll try it out.

@qatar2030
Copy link

Hey everyone, I built a thing, might be of interest for some of you: https://github.com/klamann/raspi-poe-mon

I wasn't happy with the information the example library was giving us (oh, the fan is on? thanks, I didn't notice 😆), so I decided to make better use of the limited screen space ;)

i can't install it i get error(-bash: pip: command not found) and e: unable to locate package libtiff5)

@klamann
Copy link

klamann commented Oct 20, 2023

i can't install it i get error(-bash: pip: command not found) and e: unable to locate package libtiff5)

Please follow the install instructions in the readme. The part that starts with sudo apt install is probably relevant for you :)

@qatar2030
Copy link

i can't install it i get error(-bash: pip: command not found) and e: unable to locate package libtiff5)

Please follow the install instructions in the readme. The part that starts with sudo apt install is probably relevant for you :)

Screenshot 2023-10-22 081043
Screenshot 2023-10-22 081115

No it's NOT going to work

@CharlesGodwin
Copy link
Author

I tried and found there is a libtiff6 that may work. But I'm handicapped by the manufacture changing software and OS changing things.

You can try changing the install to libtiff6 or use this git project instead. It's better software.

@klamann
Copy link

klamann commented Oct 23, 2023

@qatar2030 I haven't tested the setup with older versions of raspi OS. Maybe try pip3 instead of pip? And you can use apt-cache search libtiff to find whatever version of libtiff is available for your distribution, but the software might also work without libtiff

@jhud1030
Copy link

@klamann I got it up and running but it is switching between what you have created and what I originally had on the screen. I am assuming I need to remove that from a file somewhere but I don't remember how I originally set this up. Help would be appreciated.

@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