Detecting through python if a pushbutton is pressed

Sir i am given a task to control leds using pushbutton via esp8266mod. But i am stuck at “how to detect through python if a pushbutton is pressed”. Through google searches i have found that there is a module for pushbutton, there it can be created and controlled but there is no module or library for hardware pushbutton. I have tried the command if mybolt.digitalRead(‘0’)==‘HIGH’ , but it doesnt work.
Please help if you have any solution regarding this.

hello @sutaraisha123
you just want to check the condition of the push switch in BOLT through right?

To detect if a pushbutton is pressed using an ESP8266 module and Python, you can use a python library called " machine " which provides access to the low-level hardware of ESP8266.

You can use a function called " machine.Pin() " to create a pin object for the pin connected to the pushbutton

The function called " value() " can be used to read the state of the pin

Here is an example [ on how to use the "
machine " library to detect if a pushbutton connected to pin 0 is pressed]


import machine

button_pin = machine.Pin(0, machine.Pin.IN)

while True:
    if button_pin.value() == 0:
        print("Button pressed")
    else:
        print("Button not pressed")

It’s important to note that, depending on the physical push button, and the ESP8266 board you’re using, you may need to add a pull-up or pull-down resistor to the push button pin to ensure that it reads a stable value when the button is not pressed.

Hope this helps :smile:

You need to first import machine library . The machine module contains specific functions related to the hardware on a particular board. Most functions in this module allow to achieve direct and unrestricted access to and control of hardware blocks on a system (like CPU, timers, buses, etc.).
and then you can use machine.pin() function

Hi, @sutaraisha123

To detect if a push button is pressed using the ESP8266 module and Python, you can utilize the digitalRead() function provided by the Bolt IoT Python library. However, you need to make sure that you have correctly set up the hardware connections between the push button and the ESP8266 module.

Here’s an example of how you can detect a push button press using the Bolt IoT Python library:

from boltiot import Bolt

api_key = ‘YOUR_API_KEY’
device_id = ‘YOUR_DEVICE_ID’

mybolt = Bolt(api_key, device_id)

response = mybolt.digitalRead(‘0’)
button_state = response[‘value’]

if button_state == ‘HIGH’:
print(“Button is pressed”)
else:
print(“Button is not pressed”)

In this code snippet:

  • Replace 'YOUR_API_KEY' with your actual Bolt Cloud API key.
  • Replace 'YOUR_DEVICE_ID' with the device ID of your Bolt device.

Ensure that you have properly connected the push button to GPIO pin 0 (or the desired pin) on the ESP8266 module. Additionally, make sure that you have configured the Bolt Cloud correctly and associated the correct device ID with your Bolt account.

Note: Ensure that you have installed the boltiot library using the command pip install boltiot before running the code.

If you are still facing issues with detecting the push button press, please provide more details about your hardware setup, code, and any error messages you are encountering.