Category Archives: Utility

a simple circuit to implement a run indicator and shutdown button

Raspberry PI running indicator and shutdown button

One of the things I want for my IoT devices is a smart power button. When off or in low power I would like to be able to press the button and have the device power on. If the device is on I want to press the power button and have the device shutdown safely and either power off or indicate that power can be shut off.

The PI has built in support for the seed of my solution in the form of device tree overlays  (dto) that can activate kernel support for a shut down signal and a powered down signal. I will be using these two features to implement a simple shutdown button and running indicator led. Not quite the feature set I am looking for, but this may be useful to pull out for some circumstances. Besides, if I can control a led at shut down I could send a signal to put the PI in low power or have it’s UPS shutdown.

There are many sources on the web that detail these two dto’s . The two I found most helpful were:

shutdown signal:
https://www.raspberrypi.org/forums/viewtopic.php?t=217442
 
toggling gpio at shutdown:
https://www.raspberrypi.org/forums/viewtopic.php?t=223157

It should be noted that specifying the gpio pin to use is different in these two overlays. My implementation looks like this (in /boot/config.txt):

dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up
dtoverlay=gpio-poweroff,gpiopin=19,active_low=1

With this configuration I am monitoring pin 17 for a low signal to initiate system shut down. When the system has halted, pin 19 will go low. Pin 19 will go from low to high on startup.

When pin 19 signals shut down there are two low to high transitions that look like this:

the has shut down signal toggles low to high once before staying low
The has shut down signal toggles low to high once before staying low

So — using this line to control a running led would have the led on while running. When the system shuts down the led will blink once and then go out indicating it is safe to power off. Keeping the led lit adds 10 ma to the measured current draw of the PI. If this is a concern the logic could be reversed and the led could be made to illuminate at system shutdown. I’m going to be using the led as a running indicator, on while running.

These simple circuits are all that we need to implement our running indicator and shutdown switch.

a simple circuit to implement a run indicator and shutdown button
For a dollars worth of parts we can add some helpful features.

In case it is not clear — those upside diamonds are grounds at the bottom of the schematic. I like the other style ground symbols better.

D1 is a generic led I had on my desk. R1 is 500 ohms because I have so many of them. R3 and C1 are for switch debounce. You can leave these out if you want. I added them because my switch was glitchy when activated:

Without r3/c1:

glitchy shutdown signal
With out R3/C1 my shutdown signals usually look like this

With r3/c1

glitch free shutdown signal
With r3/c1 installed the shutdown signal is glitch free

And there you have it. With a simple file edit and circuit we now have a shutdown switch and a running indicator for our PI. We no longer have to log in and issue a shutdown command and have to assume it is safe to power off our PI. This is a good first step, but it would be handy if we could get this to be a smart power switch. We will be looking at this in an up coming post.

These links are relevant for what was done here:

These links are relevant for what was done here:
 
https://www.onsemi.com/pub/Collateral/2N3903-D.PDF
 
https://www.thegeekpub.com/246471/debouncing-a-switch-in-hardware-or-software/
 
https://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/
 
https://www.electronics-tutorials.ws/transistor/tran_4.html
 
Logic captures by the awesome Saleae logic analyzer

If you find this work helpful perhaps you would consider supporting m2ag.labs open source efforts by buying us coffee. Any amount would be appreciated. Please use this link to do so:

Small Donate Button

Thanks for stopping by.

Remote SQLite access

The problem:

I want to use SQLite as a file system format for my configuration setting and log management on my IoT devices I tend to use small embedded Linux units that run headless. Accessing SQLite you have to be local to the db file so this makes it hard to do db maintenance on my devices. The choices I have for access are:

  • Use a network file share to make the db available.
  • Install PHP and use use phpLiteAdmin
  • Remote login with ssh and use the SQLite shell
  • Roll my own web service to do the job.

What would be handy is an API to the database that any app could consume. That way I can automate management apps to tend to my devices SQLite storage. This can allow remote configuration, log caching , etc that can center on the SQLite file as a database. Since I am using Python for device side hardware interfacing chores it seemed natural to do something with it. There are several Python web frame works that could help here and I chose to use Flask. Client side will be a HTML 5 app served by the API.

The repo for the app is at https://github.com/m2ag-labs/sqlite-remote – MIT license. This tool is intended for development use and light duty maintenance. The most common thing I do is trouble shoot messaging issues and pruning tables. For more intensive usage I’m thinking phpLiteAdmin would be a better choice, but you would have to install PHP and a webserver.