0
0
Raspberry Piprogramming~20 mins

Serving sensor data as JSON API in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sensor 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 API endpoint?
Consider this Python Flask code running on a Raspberry Pi that reads a temperature sensor and serves the data as JSON. What will be the output when a client sends a GET request to /temperature?
Raspberry Pi
from flask import Flask, jsonify
app = Flask(__name__)

def read_temperature():
    return 23.5

@app.route('/temperature')
def temperature():
    temp = read_temperature()
    return jsonify({'temperature_celsius': temp})

if __name__ == '__main__':
    app.run()
A{"temp": 23.5}
B{"temperature_celsius": 23.5}
C23.5
DInternal Server Error
Attempts:
2 left
💡 Hint
Look at the dictionary key used in jsonify.
🧠 Conceptual
intermediate
1:00remaining
Which HTTP method should be used to request sensor data from a JSON API?
You have a Raspberry Pi serving sensor data as a JSON API. Which HTTP method is the correct and standard way to request this data?
AGET
BDELETE
CPUT
DPOST
Attempts:
2 left
💡 Hint
Think about which method is used to retrieve data without changing it.
🔧 Debug
advanced
2:00remaining
What error does this Raspberry Pi Flask code raise?
This code tries to serve sensor data as JSON but has a bug. What error will it raise when running?
Raspberry Pi
from flask import Flask, jsonify
app = Flask(__name__)

def read_humidity():
    return 55

@app.route('/humidity')
def humidity():
    value = read_humidity()
    return jsonify({'humidity': value})

if __name__ == '__main__':
    app.run()
ASyntaxError
BTypeError
CKeyError
DNo error, returns JSON
Attempts:
2 left
💡 Hint
Check the syntax of the argument passed to jsonify.
🚀 Application
advanced
2:30remaining
How to add a timestamp to sensor JSON response?
You want to serve sensor data with a timestamp in the JSON response on your Raspberry Pi Flask API. Which code snippet correctly adds the current time in ISO format under key 'timestamp'?
Raspberry Pi
from flask import Flask, jsonify
from datetime import datetime
app = Flask(__name__)

def read_light():
    return 300

@app.route('/light')
def light():
    value = read_light()
    # Add timestamp here
    return jsonify({})
Areturn jsonify({'light': value, 'timestamp': str(datetime.now)})
Breturn jsonify({'light': value, 'timestamp': datetime.isoformat()})
Creturn jsonify({'light': value, 'timestamp': datetime.now})
Dreturn jsonify({'light': value, 'timestamp': datetime.now().isoformat()})
Attempts:
2 left
💡 Hint
Use datetime.now() to get current time, then convert to ISO string.
Predict Output
expert
3:00remaining
What is the output of this asynchronous sensor data API code?
This Raspberry Pi code uses async Flask to serve sensor data. What is the JSON output when calling /sensor?
Raspberry Pi
import asyncio
from flask import Flask, jsonify
app = Flask(__name__)

async def read_sensor():
    await asyncio.sleep(0.1)
    return {'temperature': 22.1, 'humidity': 48}

@app.route('/sensor')
async def sensor():
    data = await read_sensor()
    return jsonify(data)

if __name__ == '__main__':
    app.run(debug=False)
A{"temperature": "22.1", "humidity": "48"}
BRuntimeError
C{"temperature": 22.1, "humidity": 48}
DEmpty JSON {}
Attempts:
2 left
💡 Hint
Check the awaited function and returned dictionary keys and values.