The Raspberry Pi (3) power on and shutdown are issues. The original thought was to just turn power on and boot up the Raspberry Pi along with everything else on the TrainThing layout and when done turn off the power. This works for the Arduino (2) which is running the Base Station DCC++ software which is designed as a standalone embedded system. The Raspberry Pi on the other hand is running a general purpose OS that writes data to memory/data storage and if that process is interrupted, the data file may get corrupted and the system may not reboot.

A search resulted in three topics:

  • A graceful way to shutdown the Pi
  • A way to restart/ boot the Pi
  • A way to tell when the Pi is running or not

Without a screen or keyboard, (headless operation) we need a way to shutdown or put to sleep the Raspberry Pi (3). Just shutting off the power can corrupt files or the SD card. If we can get the OS's attention python can then issue a shutdown command. Of course we could add a shutdown command to the DCC++ commands sent from the Laptop. That works only if the WiFi connection is working. The better option is to set up an interrupt so that when a GPIO pin is brought to ground the shutdown command is executed. This requires a Raspberry Pi GPIO pin, Which we do not have as currently designed. 

Deep in the OS, when the system is shutdown or asleep, bring GPIO pin 3 to ground will wake up the Pi. By setting up GPIO #3 to put the Pi to sleep when running the same pin could also be used to wake up the Pi when asleep. However, GPIO pin 3 is also one of the two I2C interface pins used to control most of the HATs used by TrainThing. So GPIO Pin 3 is not available. Another pin must be made available to shutdown the raspberry Pi

Another option to wake-up the Pi is to ground the GLOBAL_EN pin on the Raspberry Pi board to reboot a shutdown Pi.

If the UART in the Pi is enabled GPIO pin 14 will be high when the Pi is running and ground when asleep/shutdown. This can drive a LED to show when the Raspberry Pi is running/asleep.

To make all of this work two GPIO pins are needed: 1 as a run LED (pin 14) and 1 to shutdown the Pi. By moving the 4 traffic monitoring LEDs from Raspberry Pi GPIO pins on the 16-channel PWM HAT to 4 output pins on the IO Pi HAT, four GPIO pins are now available including GPIO pin 14.