

Execute shell script on GPIO change

  • Hey everyone,

    I'm essentially looking to toggle the Omega Onion 2S+'s WiFi on/off using a button connected to GPIO19. Something similar was asked in this post:

    Back story is the onion is hooked up to a Ublox Sara R410m which will provide internet connectivity most of the time but I just want to press a button so users can turn on and access Soft AP for device configuration changes. We do not however want that Soft AP/WiFi on all the time, hints needing to toggle it on and off from a button.

    The suggestion in the post above seems like the correct solution but looking in rc.button I only see failsafe, power, reset, and rfkill. I'm new to openwrt and am still learning the ins and outs. I'm not sure how I could add handling to hotplug for a button connected to GPIO19. Is this possible? If so could anyone provide a nudge in the right direction?

  • @Travis-Elliott google hotplug2.rules you'll find details on how you can get hotplug to respond to a wider range of events, including buttons.

  • Thanks for the feedback @crispyoz but I just wrote a python script to monitor the button input on interval and then used uci calls through subprocess to turn wifi on and off. This gives me more capability and I suppose it's not that big of a deal to run a Python script on boot, I'm also flashing an LED through the Python script depending on whether wifi is on or not so it all worked out.

  • administrators

    @Travis-Elliott Good to hear that you resolved it. If you can spare the space that Python takes up, that's not a bad solution and I would stick with it.

    A smaller alternative would be a C application that does the same thing. Might be a good idea to make it generic so that command line arguments specify the GPIO to watch and the script/binary to run when the GPIO goes up or down.
    Here's a sample C implementation in case you want to pursue this route: https://github.com/OnionIoT/gpioIrq

    One more alternative but it's work intensive: build a custom firmware that defines a GPIO button input that's polled by the kernel. Button presses will then trigger scripts in the /etc/rc.button directory.
    For example, this DTS file defines GPIO46 as an active-high button input. Button presses will trigger execution of a script at /etc/rc.button/wps:

  • @Lazar-Demin
    Thank you for the input. I'll look into a C implementation. That's a bit more up my ally anyway. I don't generally write in Python but I have quite a bit of experience in C and C++.

    I've got the Ublox module registered following this guide:
    Is there a way to automatically trigger a script when the status of the connection changes? I believe it is possible to monitor network status. Maybe using uci? The network connection seems to be pretty stable but if it goes offline I'd like to indicate that through the RGB LED we have on board.

  • administrators

    @Travis-Elliott let me know how the C implementation works out. A generic solution could be useful for a lot of users!

    For monitoring network status and taking action based on changes, I recommend looking into the openwrt hotplug system: https://openwrt.org/docs/guide-user/base-system/hotplug

    Specifically the iface and net parts.

Log in to reply
