0
0
Raspberry Piprogramming~20 mins

Controlling GPIO through web interface in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GPIO Web Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Flask GPIO control snippet?

Consider this Python Flask code snippet running on a Raspberry Pi to toggle an LED connected to GPIO pin 17. What will be printed when the user accesses the root URL?

Raspberry Pi
from flask import Flask
import RPi.GPIO as GPIO

app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

@app.route('/')
def toggle_led():
    GPIO.output(17, GPIO.HIGH)
    print('LED ON')
    return 'LED is now ON'

if __name__ == '__main__':
    app.run()
ARuntimeError due to GPIO setup
BLED ON printed to console and 'LED is now ON' shown in browser
CNothing printed and browser shows 'LED is now ON'
DLED ON printed to console but browser shows an error
Attempts:
2 left
💡 Hint

Think about what the print statement does and what the Flask route returns.

🧠 Conceptual
intermediate
1:30remaining
Which HTTP method is best to toggle GPIO state safely?

You want to create a web interface to toggle a GPIO pin on your Raspberry Pi. Which HTTP method should you use to change the GPIO state?

APOST
BGET
CHEAD
DOPTIONS
Attempts:
2 left
💡 Hint

Consider which HTTP methods are intended for actions that change server state.

🔧 Debug
advanced
2:30remaining
Why does this Flask GPIO code raise RuntimeError?

Look at this Flask app code snippet. It raises a RuntimeError: 'This module can only be run on a Raspberry Pi!'. Why?

Raspberry Pi
from flask import Flask
import RPi.GPIO as GPIO

app = Flask(__name__)

@app.route('/on')
def led_on():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(18, GPIO.OUT)
    GPIO.output(18, GPIO.HIGH)
    return 'LED ON'

if __name__ == '__main__':
    app.run()
ARPi.GPIO module is imported but code runs on a non-Raspberry Pi machine
BGPIO pin 18 is not supported on Raspberry Pi
CFlask app is missing GPIO.cleanup() call
DFlask app is missing debug=True in app.run()
Attempts:
2 left
💡 Hint

Think about the environment where the code runs and the RPi.GPIO module requirements.

📝 Syntax
advanced
2:30remaining
Which Flask route code correctly toggles GPIO pin 22?

Choose the correct Flask route code snippet that toggles GPIO pin 22 from LOW to HIGH or HIGH to LOW each time the route is accessed.

A
def toggle():
    state = GPIO.input(22)
    GPIO.output(22, state == 0)
    return f'Pin 22 set to {state == 0}'
B
def toggle():
    state = GPIO.input(22)
    GPIO.output(22, not state)
    return f'Pin 22 set to {not state}'
C
def toggle():
    state = GPIO.input(22)
    GPIO.output(22, 1 if state == 0 else 0)
    return f'Pin 22 set to {1 if state == 0 else 0}'
D
def toggle():
    state = GPIO.input(22)
    GPIO.output(22, state)
    return f'Pin 22 set to {state}'
Attempts:
2 left
💡 Hint

Remember GPIO.output expects 0 or 1, and toggling means switching between these states.

🚀 Application
expert
3:00remaining
How many GPIO pins are set HIGH after running this Flask app code?

Given this Flask app code controlling multiple GPIO pins, how many pins will be HIGH after the '/activate' route is accessed once?

Raspberry Pi
from flask import Flask
import RPi.GPIO as GPIO

app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
pins = [5, 6, 13, 19]
for pin in pins:
    GPIO.setup(pin, GPIO.OUT)
    GPIO.output(pin, GPIO.LOW)

@app.route('/activate')
def activate():
    for pin in pins:
        if pin % 2 == 1:
            GPIO.output(pin, GPIO.HIGH)
    return 'Activated odd pins'

if __name__ == '__main__':
    app.run()
A2
B4
C1
D3
Attempts:
2 left
💡 Hint

Check which pins in the list are odd numbers.