Handler for Momentary Button

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
samtkc
 
Posts: 30
Joined: Wed Oct 04, 2017 11:28 am

Handler for Momentary Button

Post by samtkc »

Can someone provide example code for pushing a button value from a Momentary Button (IO Block) to a Pi Pico using CircuitPython? Basically, I want to set up a handler for the feed from a Momentary Button. I can do this in C using onMessage, but I don't know how to do this in CircuitPython.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Handler for Momentary Button

Post by mikeysklar »

This is from the Adafruit Pico guide CircuitPython section.

Code: Select all

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Button example for Pico. Prints button pressed state to serial console.

REQUIRED HARDWARE:
* Button switch on pin GP13.
"""
import time
import board
import digitalio

button = digitalio.DigitalInOut(board.GP13)
button.switch_to_input(pull=digitalio.Pull.DOWN)

while True:
    print(button.value)
    time.sleep(0.5)

User avatar
samtkc
 
Posts: 30
Joined: Wed Oct 04, 2017 11:28 am

Re: Handler for Momentary Button

Post by samtkc »

Thanks for the example, though I guess I was not clear. I am looking for a way to push a value from the dashboard to the Pi Pico. The Momentary Button I referred to is a dashboard block. Also, I am fine with reading a value from the dashboard on the Pi Pico in CircuitPython. What I am looking for is a way to do this with a handler i.e. I do not want to be constantly checking the value of the dashboard Momentary Button. And similar to how this would be done in C using "onMessage".

User avatar
samtkc
 
Posts: 30
Joined: Wed Oct 04, 2017 11:28 am

Re: Handler for Momentary Button

Post by samtkc »

Is this simply not possible???

User avatar
jwcooper
 
Posts: 1004
Joined: Tue May 01, 2012 9:08 pm

Re: Handler for Momentary Button

Post by jwcooper »

Here is a CircuitPython example that does an on_message for a feed sent from a dashboard. This example is using mqtt, which is the recommended approach for something like this:

https://github.com/adafruit/Adafruit_Ci ... ack.py#L79

User avatar
samtkc
 
Posts: 30
Joined: Wed Oct 04, 2017 11:28 am

Re: Handler for Momentary Button

Post by samtkc »

Thanks!

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”