Skip to content

Miscellaneous scripts for my Turing Pi setup. Turn on and off all nodes with a command or power button and have a power LED.

License

Notifications You must be signed in to change notification settings

nicholaswilde/turing-pi-scripts

Repository files navigation

Turing Pi Scripts

Miscellaneous scripts for my Turing Pi setup. Turn on and off all nodes with a command or power button and have a power LED.

⚠️ WARNING:The register which controls the power to each board is backed by an EEPROM. Don't set all bits in there to 0, otherwise you won't be able to boot and correct it. A fix is to set register 0xF4 to 1 which would send the writes to the shadow SRAM, instead of the EEPROM. A fix for this is to connect an external raspberry pi to the I2C External pins and reset the registers. |

Script Function
turn-on-nodes.sh Turns all nodes on via the CMB
turn-off-nodes.sh Turns off all nodes via the CMB
turn-on-nodes.service systemd service file to run turn-on-nodes.sh at boot
listen-for-shutdown.py Listen for when the power button is held for 2 seconds and turn off the Turing Pi
listen-for-shutdown.service systemd service file to start listen-for-shutdown.py at boot

Notes

  • The power LED will blink a few times when holding the power button down for two seconds. All worker nodes will turn off first and a normal shutdown node will be ran on the master node.
  • The power LED will stay on until the master node shuts down completely.
  • Once the power LED turns off, the power button can then be pressed to turn on the master node which will then turn on the worker nodes.

Prerequisites

# turn-on-nodes.sh and turn-off-nodes.sh
$ sudo apt-get update
$ sudo apt-get install i2c-tools

# listen-for-shutdown.py
$ sudo apt-get install python3
$ sudo apt-get install python3-gpiozero

Installation

On your master node:

$ cd ~
$ mkdir git
$ cd git
$ git clone https://github.com/nicholaswilde/turing-pi-scripts.git
$ cd turing-pi-scripts
$ chmod +x turn-on-nodes.sh
$ chmod +x turn-off-nodes.sh
$ chmod +x listen-for-shutdown.py
$ nano nodes.cfg

Edit the I2C CMB registers of the worker nodes in nodes.cfg.

# Node #1 (Master)
#0x02
# Node #2 (Worker 1)
0x04
# Node #3 (Worker 2)
0x08
# Node #4 (Worker 3)
0x10
# Node #5 (Worker 4)
0x80
# Node #6 (Worker 5)
0x40
# Node #7 (Worker 6)
0x20

Edit the listen-for-shutdown.py file and change the constants to your liking. Uncomment PIN_NO_LED if you have a power LED.

...
# Power button GPIO pin number (GPIO, not actual)
PIN_NO_BUTTON = 18
# Power LED pin number (GPIO, not actual)
PIN_NO_LED = 17
# Button hold time (s)
HOLD_TIME = 2
# Sleep time between power LED blinks (s)
SLEEP_TIME = 0.5
# Path to turn-off-nodes.sh
SCRIPT_PATH='/home/pirate/git/turing-pi-scripts/turn-off-nodes.sh'
...

Turn on the power LED connected to GPIO 17 at boot when the master node is on.

#/boot/config.txt
...
# Set GPIO17 to be an output set to 1
gpio=17=op,dh
...

Optional

You can also copy the scripts to the bin folder to easily run them from anywhere

$ sudo cp turn-on-nodes.sh /usr/bin/turn-on-nodes
$ sudo cp turn-off-nodes.sh /usr/bin/turn-off-nodes
$ sudo cp listen-for-shutdown.py /usr/bin/listen-for-shutdown
# Make the turing-pi-scripts
$ sudo mkdir /etc/turing-pi-scripts
# Copy the config file to the /etc/turing-pi-scripts/ directory
$ sudo cp nodes.cfg /etc/turing-pi-scripts/

Enable at boot

Edit turn-on-nodes.service file with the installation location of the turn-on-nodes.sh script.

...
ExecStart=/usr/bin/sudo /bin/bash -lc '/home/pirate/git/turing-pi-scripts/turn-on-nodes.sh'
...

Edit listen-for-shutdown.service file with the installation location of the listen-for-shutdown.py script.

...
ExecStart=/usr/bin/python3 /home/pirate/git/turing-pi-scripts/listen-for-shutdown.py
ExecStop=pkill -f /home/pirate/git/turing-pi-scripts/listen-for-shutdown.py
WorkingDirectory=/home/pirate/git/turing-pi-scripts/
...
# Copy the file
sudo cp turn-on-nodes.service /etc/systemd/system/turn-on-nodes.service
sudo cp listen-for-shutdown.service /etc/systemd/system/listen-for-shutdown.service

# Change the permission of the files
$ sudo chmod 664 /etc/systemd/system/turn-on-nodes.service
$ sudo chmod 664 /etc/systemd/system/listen-for-shutdown.service

# Enable it at start
$ sudo systemctl daemon-reload
$ sudo systemctl enable /etc/systemd/system/turn-on-nodes.service
$ sudo systemctl enable /etc/systemd/system/listen-for-shutdown.service

Test

On the master node:

# Initial Conditions: All nodes are on and in the ~/git/turing-pi-scripts/ directory.
$ sudo ./turn-off-nodes.sh
# All worker nodes should be off (activity lights should stop blinking).
# Turn on the worker nodes again.
$ sudo ./turn-on-nodes.sh
# Turn off the worker nodes again.
$ sudo ./turn-off-nodes.sh
# Shutdown the master node.
$ sudo shutdown now
# Master node should shut down after a while.
# Press the power button.
# The power LED should come on.
# The master node should power on.
# The worker nodes should also power on (activity lights start blinking).
# Hold the power button for two seconds.
# The power LED should blink a few times.
# Worker nodes should power down.
# Master node should power down after awhile.
# The power LED should turn off.
# Press the power button to turn on all the nodes.

Usage

# Turn on all worker nodes
$ sudo ./turn-on-nodes.sh
# or
$ sudo turn-on-nodes

# Turn off all worker nodes
$ sudo ./turn-off-nodes.sh
# or
$ sudo turn-off-nodes

# Start listen-for-shutdown.py
$ sudo ./listen-for-shutdown.py
# or
$ sudo listen-for-shutdown
# Hold the power button for two seconds and wait for the power LED to blink

Wiring Diagram


Image created with Circuit Diagram

Todo

  • Verify that the node register values are valid.
  • Create install and uninstall scripts and/or deb package.

Credit

listen-for-shutdown is based off of lihak's rpi-power-button

Author

The repository was created in 2020 by Nicholas Wilde.

About

Miscellaneous scripts for my Turing Pi setup. Turn on and off all nodes with a command or power button and have a power LED.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published