Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Cdaprod/3f086ca1fb5a9f9befca3b5b069e8414 to your computer and use it in GitHub Desktop.
Save Cdaprod/3f086ca1fb5a9f9befca3b5b069e8414 to your computer and use it in GitHub Desktop.
This setup should allow you to detect touch events on your round display and trigger an API GET request when a touch is detected.

To implement a feature where tapping the round display triggers an API GET request using MicroPython on the ESP32-C3, you'll need to follow these steps:

  1. Set up the display and touch functionality.
  2. Configure Wi-Fi for network access.
  3. Handle the touch event.
  4. Make an HTTP GET request when the display is tapped.

Here's a detailed guide to help you achieve this:

1. Setting Up the Display and Touch Functionality

First, ensure that your round display is properly connected and recognized by the ESP32-C3. For this example, we assume the display uses the ILI9341 controller and has touch capability.

2. Configuring Wi-Fi

To enable Wi-Fi, you'll need to connect to your network.

3. Handling the Touch Event

We'll use a simple method to detect touch. The actual implementation might vary based on the specific touch controller used with your display.

4. Making an HTTP GET Request

We'll use the urequests library to make the HTTP GET request.

Here's the complete code that combines all the above steps:

from machine import Pin, SPI, I2C
import network
import urequests
import time
import ili9341  # Adjust this to your display library
from xpt2046 import Touch  # Adjust this to your touch library

# Configure Wi-Fi
def connect_wifi(ssid, password):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)
    while not wlan.isconnected():
        pass
    print('Network config:', wlan.ifconfig())

# Initialize the display
spi = SPI(1, baudrate=40000000, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
display = ili9341.ILI9341(spi, cs=Pin(5), dc=Pin(21), rst=Pin(22))

# Initialize the touch controller (assuming XPT2046)
touch = Touch(cs=Pin(14), int_pin=Pin(27))  # Adjust the pins according to your setup

# Connect to Wi-Fi
ssid = 'your-ssid'
password = 'your-password'
connect_wifi(ssid, password)

# Function to handle touch and make a GET request
def handle_touch():
    if touch.is_pressed():
        print("Touch detected!")
        display.fill(0xFFFF)  # Clear the display or give feedback
        response = urequests.get('http://httpbin.org/get')
        print(response.text)
        response.close()
        display.fill(0x0000)  # Clear the display again or give feedback

# Main loop
while True:
    handle_touch()
    time.sleep(0.1)  # Adjust the delay as necessary

Explanation:

  1. Wi-Fi Configuration:

    • The connect_wifi function handles connecting to the Wi-Fi network.
  2. Display Initialization:

    • SPI communication is set up for the ILI9341 display.
  3. Touch Controller Initialization:

    • The Touch class is used to manage the touch input.
  4. Touch Handling:

    • The handle_touch function checks if the display is touched and, if so, makes an HTTP GET request to http://httpbin.org/get.
  5. Main Loop:

    • Continuously checks for touch input and triggers the GET request if a touch is detected.

Notes:

  • Ensure you have the appropriate libraries (ili9341 for the display and xpt2046 for the touch controller) installed and properly configured.
  • Adjust the pin numbers according to your specific hardware setup.
  • The delay in the main loop can be adjusted to suit the responsiveness you need.

This setup should allow you to detect touch events on your round display and trigger an API GET request when a touch is detected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment