Bird
0
0

Given this Flask route code snippet controlling GPIO pin 17:

medium📝 Predict Output Q13 of 15
Raspberry Pi - Web Server and API
Given this Flask route code snippet controlling GPIO pin 17:
from flask import Flask, request
import RPi.GPIO as GPIO

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

@app.route('/pin17/')
def pin17(state):
    if state == 'on':
        GPIO.output(17, GPIO.HIGH)
        return 'Pin 17 is ON'
    elif state == 'off':
        GPIO.output(17, GPIO.LOW)
        return 'Pin 17 is OFF'
    else:
        return 'Invalid state'

What will be the output if you visit /pin17/on in the browser?
AInvalid state
BPin 17 is ON
CPin 17 is OFF
D404 Not Found
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the URL parameter

    The URL /pin17/on passes 'on' as the state argument to the function.
  2. Step 2: Check the condition for 'on'

    The code checks if state == 'on', then sets GPIO pin 17 HIGH and returns 'Pin 17 is ON'.
  3. Final Answer:

    Pin 17 is ON -> Option B
  4. Quick Check:

    URL /pin17/on = Pin 17 ON [OK]
Quick Trick: Match URL state to if-condition for output [OK]
Common Mistakes:
MISTAKES
  • Confusing 'on' with 'off' states
  • Expecting an error for valid URL
  • Ignoring the return string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes