0
0
Raspberry Piprogramming~20 mins

REST API for IoT device in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
IoT REST API 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 REST API endpoint code?

Consider this simple Flask REST API code running on a Raspberry Pi controlling an LED. What will be the output when you send a GET request to /led/status?

Raspberry Pi
from flask import Flask, jsonify
app = Flask(__name__)

led_state = False

@app.route('/led/status')
def led_status():
    return jsonify({'led': 'on' if led_state else 'off'})

if __name__ == '__main__':
    app.run(host='0.0.0.0')
A{"led": "on"}
B{"led": "off"}
C500 Internal Server Error
D404 Not Found
Attempts:
2 left
💡 Hint

Look at the initial value of led_state.

🧠 Conceptual
intermediate
1:30remaining
Which HTTP method is best to toggle an IoT device state?

You want to create a REST API to toggle an LED on a Raspberry Pi. Which HTTP method should you use to change the LED state?

ADELETE
BGET
CPUT
DPOST
Attempts:
2 left
💡 Hint

Think about which method is used to create or change data on the server.

🔧 Debug
advanced
2:30remaining
What error does this Raspberry Pi REST API code raise?

Examine this Flask code snippet for controlling a sensor. What error will it raise when running?

Raspberry Pi
from flask import Flask, request, jsonify
app = Flask(__name__)

sensor_value = 0

@app.route('/sensor', methods=['POST'])
def update_sensor():
    data = request.json
    return jsonify({'sensor': sensor_value})
    sensor_value = data['value']
    return jsonify({'sensor': sensor_value})

if __name__ == '__main__':
    app.run()
AUnboundLocalError: local variable 'sensor_value' referenced before assignment
BNo error, returns updated sensor value
CTypeError: 'NoneType' object is not subscriptable
DKeyError: 'value'
Attempts:
2 left
💡 Hint

Look at how sensor_value is assigned inside the function.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a Flask route to turn on an LED?

Choose the correct Flask route definition to turn on an LED when a POST request is sent to /led/on.

A
@app.route('/led/on', methods=['POST'])
def led_on():
    turn_on_led()
    return 'LED is on'
B
@app.route('/led/on')
def led_on():
    turn_on_led()
    return 'LED is on'
C
@app.route('/led/on', method=['POST'])
def led_on():
    turn_on_led()
    return 'LED is on'
D
@app.route('/led/on', methods='POST')
def led_on():
    turn_on_led()
    return 'LED is on'
Attempts:
2 left
💡 Hint

Check the correct keyword and data type for HTTP methods in Flask routes.

🚀 Application
expert
1:30remaining
What is the number of items in the JSON response from this Raspberry Pi REST API code?

This Flask API returns sensor readings as JSON. How many key-value pairs are in the JSON response?

Raspberry Pi
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/sensors')
def sensors():
    data = {
        'temperature': 22.5,
        'humidity': 45,
        'pressure': 1013
    }
    return jsonify(data)

if __name__ == '__main__':
    app.run()
A2
B1
C3
D4
Attempts:
2 left
💡 Hint

Count the keys in the data dictionary.