Showing posts with label GPIO. Show all posts
Showing posts with label GPIO. Show all posts

Thursday, June 22, 2023

Rugged Metal Buttons with LED Rings on Raspberry Pi

How to interface rugged metal pushbuttons with LED rings to a headless Raspberry Pi — using the built-in triggerhappy daemon to invoke scripts on button press and provide LED feedback, with no display or keyboard needed.

Connection diagram — pushbuttons to Raspberry Pi GPIO

Connection diagram — two pushbuttons wired to Raspberry Pi GPIO pins.

How it works

Instead of userspace I/O libraries, this approach uses the triggerhappy daemon (built into Raspbian) to map standard key codes from /dev/input/eventX to scripts via /etc/triggerhappy/triggers.d/actions.conf.

Overview of the steps:

  1. Connect pushbuttons to Raspberry Pi GPIOs as shown in the connection diagram
  2. Prepare a Raspberry Pi OS Lite SD card using Raspberry Pi Imager
  3. SSH into the Raspberry Pi
  4. Add dtoverlay config lines to /boot/config
  5. Create push-buttons.conf under /etc/triggerhappy/triggers.d/
  6. Modify /lib/systemd/system/triggerhappy.service to run as the pi user
  7. Create action scripts configured in push-buttons.conf
  8. Reboot and press a button to see the configured action invoked

Prepare the SD card

  1. Download the Raspberry Pi Imager
  2. Open the app → CHOOSE OS → Raspberry Pi OS (Other) → Raspberry Pi OS Lite (32-bit)
  3. Insert the SD card and click CHOOSE STORAGE
  4. Press Ctrl + Shift + X to open advanced options
  5. Set hostname, enable SSH, username/password, and local time settings as shown below, then click SAVE
  6. Click WRITE to create the bootable SD card
Raspberry Pi Imager advanced options

Raspberry Pi Imager — advanced options for headless setup.

Setup and demo

  1. Connect two pushbuttons to Raspberry Pi GPIO pins as shown in the connection diagram
  2. Insert the SD card, connect the Pi to your local DHCP network, and power on
  3. SSH in: ssh pi@my-raspi-001 (use the password from the Imager advanced options)
  4. Run the following commands:
sudo apt-get install -y git esptool
git clone https://github.com/hackboxguy/pi-pushbtn-demo.git
cd pi-pushbtn-demo
sudo ./setup.sh
sudo reboot
Terminal showing setup commands

Running the setup commands on the Raspberry Pi.

Wait ~40 seconds for the Pi to reboot. Press one pushbutton — its LED ring blinks at ~2 Hz. Press the other button — its LED starts blinking and the first one stops.

Extending the demo

Instead of just blinking LEDs, the buttons can invoke real tasks. For example, to flash different firmware binaries to an ESP8266 (e.g. Wemos D1 Mini) on button press, replace the action scripts:

cp /home/pi/pi-pushbtn-demo/flash-esp-1hz.sh /home/pi/pi-pushbtn-demo/key-pressed-down.sh
cp /home/pi/pi-pushbtn-demo/flash-esp-4hz.sh /home/pi/pi-pushbtn-demo/key-pressed-up.sh

After this change, pressing a button blinks the LED ring for ~30 seconds (indicating the Wemos D1 Mini is being flashed), then stops when flashing completes. See the 1 Hz flash script and 4 Hz flash script for details.

Hardware setup — pushbuttons connected to Raspberry Pi

Complete setup — rugged metal pushbuttons connected to the Raspberry Pi.

SOURCE CODE

github.com/hackboxguy/pi-pushbtn-demo — scripts and setup instructions

Sunday, October 02, 2022

Measuring PREEMPT_RT Latency with an Oscilloscope

A ready-to-use bootable SD card image for BeagleBone Black/Green with a fully preemptible Linux kernel (PREEMPT_RT) — includes a gpio-test program that mirrors an input signal to a GPIO output pin for measuring scheduler latency with an oscilloscope.

Oscilloscope showing input and output waveforms with scheduler latency

Oscilloscope view — yellow is the input signal, blue is the GPIO output. The phase difference indicates scheduler latency.

Thanks to Maxime Chevallier of Bootlin for providing the gpio-test demo utility during PREEMPT_RT training.

Setup diagram

Setup diagram — signal generator, BeagleBone, and oscilloscope

Setup diagram — signal generator feeds the BeagleBone, oscilloscope measures input vs output.

Items needed

Instructions

  1. Prepare the setup as shown in the diagram (set signal generator to 100 Hz, 50% duty cycle)
  2. Download sdcard-beaglebone-preemptrt-demo.img.xz (~25 MB)
  3. Write the image to a micro SD card using Balena Etcher
  4. Insert the SD card into the BeagleBone and power on
  5. After boot, the oscilloscope shows two square waves with a phase difference — yellow (input) and blue (output from BeagleBone)
  6. The phase difference indicates the latency introduced by the scheduler while running the preemptrt-gpiotest program

On startup, /etc/init.d/S99PreemptrtGpioTester invokes:

chrt -r 99 /usr/sbin/preemptrt-gpiotest gpiochip0 28 gpiochip0 17 h p

This starts the gpio-test utility at real-time priority 99.

Measuring latency under load

The gpio-test program mirrors a GPIO input to an output — the phase difference between the two signals shows how fast the scheduler delivers context to your application. Jitter on the output signal (measured in oscilloscope persist mode) indicates the best and worst case scheduler latency under different load conditions (network traffic, interrupts, etc.).

Oscilloscope persist mode showing best and worst case latency

Persist mode — showing best and worst case scheduler latency under load.

For further details on PREEMPT_RT, see Bootlin's PREEMPT_RT training material.

Building from source

Detailed build instructions are available in the git repository. Cross-compile instructions for modifying and running a customized gpio-test utility are also available.

SOURCE CODE

github.com/hackboxguy/preemptrt-gpiotest — GPIO latency test program

github.com/hackboxguy/br-wrapper — Buildroot config and build instructions

sdcard-beaglebone-preemptrt-demo.img.xz — pre-built SD card image (~25 MB)

Monday, January 04, 2016

Control Home Devices via XMPP Chat with Raspberry Pi

Control your home devices from anywhere using a Raspberry Pi running the RBox Linux distribution. The RBox chat-bot connects to a public XMPP/Jabber server, so you can toggle GPIO pins from your phone's chat app — no port forwarding, no dynamic DNS, no paid cloud services.

How it works

Setup diagram — Raspberry Pi at home communicating with smartphone via XMPP

Setup diagram — Raspberry Pi at home and your phone both connect to a public Jabber server. Chat messages carry GPIO commands.

Both the Raspberry Pi (running RBox) and your Android phone (running Xabber or any XMPP client) log in to a public Jabber server with their own credentials. Once the two accounts have authorized each other, they can exchange chat messages through the server.

Advantages

  • No router configuration or port forwarding needed — all incoming firewall ports can stay blocked
  • No dependency on paid cloud services — hundreds of free Jabber servers are available
  • Works from any location as long as your phone has internet access (Wi-Fi or mobile data)

Controlling GPIO

To toggle a GPIO pin, send a chat message from your XMPP client:

Gpio 2 1    # set GPIO2 high
Gpio 2 0    # set GPIO2 low

For full setup instructions (SD card preparation, XMPP account creation, first boot), see the RBox installation guide.

SOURCE CODE

github.com/hackboxguy/brbox — RBox sources and build instructions

Sunday, January 03, 2016

RBox XMPP Command Reference for IoT Control

Complete command reference for RBox. Send Help to your RBox device and it will reply with the full list of available commands. For background on RBox, see the introductory post.

RBox Help command output on Xabber

Help command output shown in the Xabber Android app.

Commands are not case-sensitive — they are converted to lowercase internally before processing.

System commands

Version — returns the RBox version in the format MJ.Mn.BUILD (major, minor, 5-digit SVN revision). Run this before and after Sysupdate to confirm the upgrade.

Sysupdate — downloads and installs the latest RBox release from GitHub onto the non-active boot partition. A reboot is required for changes to take effect. Tip: send Sysupdate;reboot to update and reboot in one step.

Reboot — gracefully shuts down all services and restarts the device.

Poweroff — gracefully shuts down and halts the system. Always use this before cutting power — a hard power-off leaves the XMPP session stale (the server shows the device as online but it is unresponsive).

Uptime — returns the device uptime in hours.

Network commands

Publicip — returns the public IP address assigned to the router. Useful when dynamic DNS is unavailable or not updating correctly.

Localip — returns the local LAN IP address of the RBox device. Useful for SSH access on the same network.

Hostname commands

Hostname [name] — with an argument, sets a new persistent hostname (takes effect after reboot). Without an argument, prints the current hostname. Useful when SSH-ing into RBox on the local network.

Resethostname — resets the hostname to the default format brbox-1A2B3C4D5E6F (where the last 12 characters are the MAC address). Requires a reboot.

GPIO commands

Gpio <address> [value] — reads or writes a GPIO pin. Examples:

Gpio 2 1    # set GPIO2 high
Gpio 2 0    # set GPIO2 low
Gpio 2      # read current state of GPIO2

GPIO pin values are not retained across reboots — they revert to defaults.

SOURCE CODE

github.com/hackboxguy/brbox — RBox sources and build instructions