Dashboard for IoT Devices

Step 1: Set Up Your Environment

  1. Install Flask: Make sure you have Python and pip installed. Then, run:bashCopy codepip install Flask
  2. Create Your Project Structure:arduinoCopy code/iot_dashboard ├── app.py ├── static │ └── style.css └── templates └── index.html

Step 2: Create app.py

This file will handle the backend logic.

pythonCopy codefrom flask import Flask, render_template, jsonify, request
import random

app = Flask(__name__)

# Simulate IoT device data
devices = {
'device1': {'temperature': 20, 'humidity': 30},
'device2': {'temperature': 22, 'humidity': 40}
} @app.route('/') def index():
return render_template('index.html')
@app.route('/api/devices', methods=['GET']) def get_devices():
return jsonify(devices)
@app.route('/api/update', methods=['POST']) def update_device():
data = request.json
device_id = data.get('id')
if device_id in devices:
    devices[device_id]['temperature'] = data.get('temperature', devices[device_id]['temperature'])
    devices[device_id]['humidity'] = data.get('humidity', devices[device_id]['humidity'])
    return jsonify(success=True)
return jsonify(success=False), 404
if __name__ == '__main__':
app.run(debug=True)

Step 3: Create index.html

This file will be the frontend for your dashboard.

htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;IoT Dashboard&lt;/title&gt;
&lt;link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"&gt;
&lt;script src="https://code.jquery.com/jquery-3.6.0.min.js"&gt;&lt;/script&gt;
</head> <body>
&lt;h1&gt;IoT Device Dashboard&lt;/h1&gt;
&lt;div id="devices"&gt;&lt;/div&gt;
&lt;script&gt;
    function fetchDevices() {
        $.getJSON('/api/devices', function(data) {
            $('#devices').empty();
            $.each(data, function(id, device) {
                $('#devices').append(`&lt;div class="device"&gt;
                    &lt;h3&gt;${id}&lt;/h3&gt;
                    &lt;p&gt;Temperature: ${device.temperature}°C&lt;/p&gt;
                    &lt;p&gt;Humidity: ${device.humidity}%&lt;/p&gt;
                    &lt;button onclick="updateDevice('${id}')"&gt;Update&lt;/button&gt;
                &lt;/div&gt;`);
            });
        });
    }
    function updateDevice(id) {
        const temperature = prompt("Enter new temperature:");
        const humidity = prompt("Enter new humidity:");
        if (temperature !== null &amp;&amp; humidity !== null) {
            $.ajax({
                url: '/api/update',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ id: id, temperature: temperature, humidity: humidity }),
                success: function() {
                    fetchDevices();
                }
            });
        }
    }
    $(document).ready(function() {
        fetchDevices();
    });
&lt;/script&gt;
</body> </html>

Step 4: Create style.css

Add some basic styles.

cssCopy codebody {
font-family: Arial, sans-serif;
} .device {
border: 1px solid #ccc;
padding: 10px;
margin: 10px;
}

Step 5: Run Your Application

  1. Navigate to your project directory:bashCopy codecd iot_dashboard
  2. Run your Flask app:bashCopy codepython app.py
  3. Open your web browser and go to http://127.0.0.1:5000.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *