0
0
Raspberry Piprogramming~10 mins

Serving sensor data as JSON API in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Serving sensor data as JSON API
Start Raspberry Pi
Initialize Sensor
Read Sensor Data
Format Data as JSON
Start Web Server
Wait for API Request
Send JSON Response
Repeat Reading & Serving
The Raspberry Pi reads sensor data, formats it as JSON, and serves it via a web API on request.
Execution Sample
Raspberry Pi
import json
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/sensor')
def sensor():
    data = {'temperature': 22.5, 'humidity': 60}
    return jsonify(data)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
This code creates a simple web API that returns sensor data as JSON when '/sensor' is requested.
Execution Table
StepActionSensor Data ReadJSON DataAPI Response Sent
1Start Raspberry Pi and initialize sensorNoneNoneNo
2Read sensor data{temperature: 22.5, humidity: 60}NoneNo
3Format data as JSON{temperature: 22.5, humidity: 60}{"temperature": 22.5, "humidity": 60}No
4Wait for API request at '/sensor'{temperature: 22.5, humidity: 60}{"temperature": 22.5, "humidity": 60}No
5Receive API request{temperature: 22.5, humidity: 60}{"temperature": 22.5, "humidity": 60}No
6Send JSON response{temperature: 22.5, humidity: 60}{"temperature": 22.5, "humidity": 60}Yes
7Repeat reading and serving on next requestNext sensor readNext JSON dataNo
💡 Server runs continuously, serving JSON data on each API request.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6Final
sensor_dataNone{temperature: 22.5, humidity: 60}{temperature: 22.5, humidity: 60}{temperature: 22.5, humidity: 60}Updated on each read
json_dataNoneNone{"temperature": 22.5, "humidity": 60}{"temperature": 22.5, "humidity": 60}Updated on each read
Key Moments - 3 Insights
Why do we format sensor data as JSON before sending?
JSON is a standard format that web clients understand easily. The execution_table row 3 shows data being converted to JSON before sending.
Does the server read sensor data on every API request?
Yes, the server reads fresh sensor data before sending a response, as shown in execution_table rows 2 and 7.
What happens if no API request is made?
The server waits idle, as shown in execution_table row 4, until it receives a request to respond.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the JSON data at Step 3?
A{"temperature": 22.5, "humidity": 60}
B{temperature: 22.5, humidity: 60}
CNone
DEmpty string
💡 Hint
Check the JSON Data column at Step 3 in the execution_table.
At which step does the API response get sent?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look at the API Response Sent column in the execution_table.
If sensor data changes, which variable in variable_tracker updates?
Ajson_data only
Bsensor_data only
CBoth sensor_data and json_data
DNeither variable
💡 Hint
See variable_tracker rows for sensor_data and json_data changes.
Concept Snapshot
Serving sensor data as JSON API:
- Initialize sensor and web server
- Read sensor data
- Convert data to JSON format
- Wait for API request
- Send JSON response
- Repeat for each request
Full Transcript
This example shows how a Raspberry Pi reads sensor data and serves it as a JSON API. First, the sensor is initialized. Then the Pi reads data like temperature and humidity. This data is converted into JSON format, which is a simple text format that computers and web browsers understand. A web server runs on the Pi, waiting for requests. When a request comes to the '/sensor' URL, the server sends the JSON data as a response. This process repeats for every new request, always sending fresh sensor data.