0
0
Raspberry Piprogramming~10 mins

REST API for IoT device in Raspberry Pi

Choose your learning style9 modes available
Introduction

A REST API lets your IoT device talk to other devices or apps over the internet easily. It helps you control or get data from your device remotely.

You want to check sensor data from your Raspberry Pi using a phone app.
You want to turn on/off a light connected to your Raspberry Pi from anywhere.
You want to collect temperature readings from multiple devices in one place.
You want to update device settings without physically touching the device.
You want to build a web dashboard to monitor your IoT devices.
Syntax
Raspberry Pi
from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/status', methods=['GET'])
def get_status():
    return jsonify({'status': 'Device is running'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Use Flask, a simple Python web framework, to create REST APIs on Raspberry Pi.

Define routes with @app.route to handle different API endpoints.

Examples
This example returns a fixed temperature value as JSON when you visit /temperature.
Raspberry Pi
from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/temperature', methods=['GET'])
def temperature():
    temp = 25  # example sensor value
    return jsonify({'temperature': temp})
This example accepts a POST request to turn an LED on or off by sending JSON with a 'state' key.
Raspberry Pi
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/led', methods=['POST'])
def control_led():
    data = request.json
    state = data.get('state')
    # Here you would add code to turn LED on/off
    return jsonify({'led_state': state})
Sample Program

This program creates two API endpoints: one to get sensor data and one to control an LED. It runs on the Raspberry Pi and listens on all network interfaces.

Raspberry Pi
from flask import Flask, jsonify, request

app = Flask(__name__)

# Simulated sensor data
sensor_data = {'temperature': 22, 'humidity': 60}

@app.route('/sensor', methods=['GET'])
def get_sensor_data():
    return jsonify(sensor_data)

@app.route('/led', methods=['POST'])
def set_led():
    data = request.json
    state = data.get('state')
    if state not in ['on', 'off']:
        return jsonify({'error': 'Invalid state'}), 400
    # Simulate setting LED state
    return jsonify({'led_state': state})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
OutputSuccess
Important Notes

Make sure your Raspberry Pi and client device are on the same network or properly configured for internet access.

Use tools like Postman or curl to test your REST API endpoints.

For real devices, replace simulated data with actual sensor readings and hardware control code.

Summary

A REST API helps your IoT device communicate over the network easily.

Flask is a simple way to create REST APIs on Raspberry Pi using Python.

You can get sensor data or control hardware remotely using API endpoints.