pimoroni / phew

MIT License
204 stars 44 forks source link

Change http status code based on gpio input #32

Closed andywarburton closed 1 year ago

andywarburton commented 1 year ago

Apologies if I'm being a bit dense but I didn't see anything in the documentation about that.

I'm working on a small project to "upgrade" my electric bike with smart RP2040 powered lighting. My intention is to have a Pico W attatched to the main front light and another attatched to the back which would host a rear light and "indicators")

I want to avoid running wires from the back of the bike to the front so I was thinking to use Phew! on the "front" Pico with a couple of buttons and the back Pico to read the status of the Phew! web server via a http request. What I'm struggling to understand is how I can listen for input on the buttons and then change the response on the webserver. I was thinking of just using http status codes to indicate the different "states".

psedocode:

if button_a == pressed

     route_a_status_code = 200

else:

    route_a_status_code = 404  

Does this make any sense at all or am I being a fruitloop?

andywarburton commented 1 year ago

I'm an idiot. I figured this out :D

andywarburton commented 1 year ago

Suppose I should share my solution for anyone that every discovers this:

button_left = Pin(15, Pin.IN)
button_right = Pin(14, Pin.IN)
button_front = Pin(13, Pin.IN)

def root_handler(request):

    # rando = str(random.randint(200, 210))

    if button_left.value() == 1:
        http_status = 201
    elif button_right.value() == 1:
        http_status = 202      
    elif button_front.value() == 1:
        http_status = 203      
    else:
        http_status = 200

    return str(http_status), str(http_status)