Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes
Introduction: Use Python to create an API to monitor your Raspberry Pi hardware, and build a dashboard with Appsmith.
Build a Raspberry Pi Monitoring Dashboard in Under 30 MinutesThis article has 12633 words and an estimated reading time of: 16 minutes
https://linux.cn/article-15984-1.htmlAuthor: Keyur Paralkar Translator: ChatGPT

Use Python to create an API to monitor your Raspberry Pi hardware, and build a dashboard with Appsmith.

If you want to know how your Raspberry Pi is performing, you might need a Raspberry Pi dashboard. In this article, I will demonstrate how to quickly build an on-demand monitoring dashboard to view your Raspberry Pi’s CPU performance, memory, and disk usage in real-time, and add more views and actions as needed.

If you have already used Appsmith, you can directly import sample applicationπŸ”— github.com and get started.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Appsmith

Appsmith is an open-source low-codeπŸ”— www.redhat.com application building tool that helps developers easily and quickly build internal applications, such as dashboards and management panels. It is a great choice for dashboards and reduces the time and complexity required by traditional coding methods.

In the dashboard for this example, I display the following statistics:

β—ˆ CPU

β—ˆ Usage Percentage
β—ˆ Frequency or Clock Speed
β—ˆ Count
β—ˆ Temperature
β—ˆ Memory

β—ˆ Usage Percentage
β—ˆ Available Memory Percentage
β—ˆ Total Memory
β—ˆ Free Memory
β—ˆ Disk

β—ˆ Disk Usage Percentage
β—ˆ Absolute Disk Space Used
β—ˆ Available Disk Space
β—ˆ Total Disk Space

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Create an Endpoint

You need a way to get this data from the Raspberry Pi and pass it to Appsmith. psutilπŸ”— psutil.readthedocs.io is a Python library for monitoring and analyzing, while Flask-RESTfulπŸ”— flask-restful.readthedocs.io is a Flask extension for creating REST APIsπŸ”— www.redhat.com.

Appsmith calls the REST API every few seconds to automatically refresh the data, responding with a JSON object containing all the required statistics, as shown below:

{ "cpu_count": 4,
"cpu_freq": [
600.0,
600.0,
1200.0 ],
"cpu_mem_avail": 463953920,
"cpu_mem_free": 115789824,
"cpu_mem_total": 971063296,
"cpu_mem_used": 436252672,
"cpu_percent": 1.8,
"disk_usage_free": 24678121472,
"disk_usage_percent": 17.7,
"disk_usage_total": 31307206656,
"disk_usage_used": 5292728320,
"sensor_temperatures": 52.616 }

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

1. Set Up the REST API

If your Raspberry Pi does not have Python installed, open a terminal on the Raspberry Pi and run this installation command:

$ sudo apt install python3

Now set up a Python virtual environmentπŸ”— opensource.com for your development:

$ python -m venv PiData

Next, activate the environment. You must do this after restarting the Raspberry Pi.

$ source PiData/bin/activate
$ cd PiData

To install Flask, Flask-RESTful, and other dependencies needed later, create a file named requirements.txt in your Python virtual environment and add the following content:

flask
flask-restful
gunicorn

Save the file, then use pip to install them all at once. You must do this after restarting the Raspberry Pi.

(PyData)$ python -m pip install -r requirements.txt

Next, create a file named pi_stats.py to hold the logic for retrieving system statistics from the Raspberry Pi using psutil. Paste the following code into the pi_stats.py file:

from flask import Flask
from flask_restful import Resource, Api
import psutil
app = Flask(__name__)

api = Api(app)
class PiData(Resource):
    def get(self):
        return "RPI Stat dashboard"

api.add_resource(PiData, '/get-stats')

if __name__ == '__main__':
    app.run(debug=True)

This code does the following:

β—ˆ Uses app = Flask(name) to define an application object for the nested API.
β—ˆ Uses Flask-RESTful’s API methodπŸ”— flask-restful.readthedocs.io to define the API object.
β—ˆ Defines PiData as a specific Resource classπŸ”— flask-restful.readthedocs.io in Flask-RESTful to expose each supported HTTP method.
β—ˆ Uses api.add_resource(PiData, '/get-stats') to attach the resource PiData to the API object api.
β—ˆ Returns PiData as a response whenever you access the URL /get-stats.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

2. Read Statistics Using psutil

To get statistics from your Raspberry Pi, you can use the built-in functions provided by psutil:

β—ˆ cpu_percentage, cpu_count, cpu_freq, and sensors_temperatures functions are used to get the CPU usage percentage, count, clock speed, and temperature, respectively. The sensors_temperatures reports the temperature of all devices connected to the Raspberry Pi. To get only the CPU temperature, use the key cpu-thermal.
β—ˆ The virtual_memory function returns statistics for total memory, available memory, used memory, and free memory (in bytes).
β—ˆ The disk_usage function returns statistics for total disk space, used space, and available space (in bytes).

Here is an example of combining all functions into a Python dictionary:

system_info_data = {
'cpu_percent': psutil.cpu_percent(1),
'cpu_count': psutil.cpu_count(),
'cpu_freq': psutil.cpu_freq(),
'cpu_mem_total': memory.total,
'cpu_mem_avail': memory.available,
'cpu_mem_used': memory.used,
'cpu_mem_free': memory.free,
'disk_usage_total': disk.total,
'disk_usage_used': disk.used,
'disk_usage_free': disk.free,
'disk_usage_percent': disk.percent,
'sensor_temperatures': psutil.sensors_temperatures()['cpu-thermal'][0].current,
}

The next section will use this dictionary.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

3. Get Data from Flask-RESTful API

To see the data from the Raspberry Pi in the API response, update the pi_stats.py file to include the dictionary system_info_data in the PiData class:

from flask import Flask
from flask_restful import Resource, Api
import psutil
app = Flask(__name__)
api = Api(app)

class PiData(Resource):
    def get(self):
        memory = psutil.virtual_memory()
        disk = psutil.disk_usage('/')
        system_info_data = {
            'cpu_percent': psutil.cpu_percent(1),
            'cpu_count': psutil.cpu_count(),
            'cpu_freq': psutil.cpu_freq(),
            'cpu_mem_total': memory.total,
            'cpu_mem_avail': memory.available,
            'cpu_mem_used': memory.used,
            'cpu_mem_free': memory.free,
            'disk_usage_total': disk.total,
            'disk_usage_used': disk.used,
            'disk_usage_free': disk.free,
            'disk_usage_percent': disk.percent,
            'sensor_temperatures': psutil.sensors_temperatures()['cpu-thermal'][0].current, }

    return system_info_data

api.add_resource(PiData, '/get-stats')

if __name__ == '__main__':
    app.run(debug=True)

Your script is ready, now run PiData.py:

$ python PyData.py
 * Serving Flask app "PiData" (lazy loading)
 * Environment: production
 WARNING: This is a development server. Do not run this in a production environment.
 
 * Debug mode: on
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!

You have a working API.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

4. Expose the API to the Internet

You can interact with the API on your local network. However, to access it over the internet, you need to open a port in your firewall and forward incoming traffic to the port provided by Flask. However, as your test output suggests, running the Flask app in Flask is only suitable for development, not for production. To safely expose the API to the internet, you can use the gunicorn production server installed during the setup.

Now you can start the Flask API. You need to do this every time you restart the Raspberry Pi.

$ gunicorn -w 4 'PyData:app'
Serving on http://0.0.0.0:8000

To access your Raspberry Pi from the outside world, open a port in your network firewall and direct traffic to your Raspberry Pi’s IP address on port 8000.

First, get the internal IP address of the Raspberry Pi:

$ ip addr show | grep inet

The internal IP address typically starts with 10, 192, or 172.

Next, you must configure the firewall. Usually, the router you get from your Internet Service Provider (ISP) has a built-in firewall. Typically, you can access your home router through a web browser. The router’s address is sometimes printed on the bottom, starting with 192.168 or 10. However, every device is different, so I can’t tell you which options to click to adjust the settings. For a complete description of how to configure the firewall, read Seth Kenlon’s article “Open ports and route traffic through your firewallπŸ”— opensource.com“.

Alternatively, you can use localtunnelπŸ”— theboroer.github.io to use dynamic port forwarding services.

Once your traffic reaches the Raspberry Pi, you can query your API:

$ curl https://example.com/get-stats
{
   "cpu_count": 4,
   "cpu_freq": [
      600.0,
      600.0,
      1200.0 ],
   "cpu_mem_avail": 386273280,
   ...

If you have made it this far, the hardest part is over.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

5. Repeat Steps

If you restart the Raspberry Pi, you must follow these steps:

β—ˆ Use source to reactivate the Python environment
β—ˆ Use pip to refresh the application’s dependencies
β—ˆ Use gunicorn to start the Flask application

Your firewall settings are persistent, but if you used localtunnel, you must start a new tunnel after restarting.

If you wish, you can automate these tasks, but that’s a topic for another tutorial. The final part of this tutorial is building a user interface on Appsmith, binding your Raspberry Pi data to the user interface using drag-and-drop widgets and some JavaScript. Trust me, it will be easy from now on!

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Building a Dashboard on Appsmith

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Hardware Monitoring Dashboard

To create a dashboard like this, you need to connect the public API endpoint to Appsmith, build a user interface using Appsmith’s widget library, and bind the API’s response to the widgets. If you have already used Appsmith, you can directly import sample applicationπŸ”— github.com and get started.

If you haven’t, please registerπŸ”— appsmith.com for a free Appsmith account. Alternatively, you can choose to self-host AppsmithπŸ”— docs.appsmith.com.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Connect the API as a Data Source in Appsmith

Log into your Appsmith account.

β—ˆ Find and click the β€œQuery or JS(QUERIES/JS)” button next to the left navigation bar.
β—ˆ Click the β€œCreate a blank API(Create a blank API)” button.
β—ˆ Name your project β€œPiData” at the top of the page.
β—ˆ Get the URL for your API. If you are using localtunnel, it’s a localtunnel.me address, always adding /get-stats at the end to get the statistics. Paste it into the first blank field on the page and click the β€œRUN” button.

Make sure to see a successful response in the β€œResponse(Response)” panel.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Appsmith Interface

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Building User Interface

The Appsmith interface is very intuitive, but if you feel lost, I recommend checking out the Build your first app on AppsmithπŸ”— docs.appsmith.com tutorial.

For the title, drag and drop the β€œText(Text)”, β€œImage(Image)”, and β€œDivider(Divider)” widgets onto the canvas. Arrange them as follows:

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Setting Project Title

The β€œText(Text)” widget contains the actual title of your page. Type something cooler than β€œRaspberry Pi Stats”.

The β€œImage(Image)” widget is used to showcase the unique logo of the dashboard. You can use any logo you like.

Use the β€œSwitch(Switch)” widget to toggle real-time data mode. Configure it in the β€œProperty(Property)” panel to get data from the API you built.

For the main section, create a CPU statistics area using the following widgets from the left widget library:

β—ˆ Progress Bar(Progress Bar)
β—ˆ Stat Box(Stat Box)
β—ˆ Chart(Chart)

For the memory and disk statistics sections, repeat the same steps. The disk statistics section does not require a chart, but you can use it if you find a purpose for it.

Your final widget layout should look similar to the following:

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Appsmith Property Settings

The final step is to bind the API’s data to your widgets.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Binding Data to Widgets

Return to the canvas and find your widgets in the three category sections. First, set up the CPU statistics.

To bind data to the β€œProgress Bar(Progress Bar)” widget:

β—ˆ Click the β€œProgress Bar(Progress Bar)” widget to open the right β€œProperty(Property)” panel.
β—ˆ Find the β€œProgress(Progress)” property.
β—ˆ Click the β€œJS” button to activate JavaScript.
β—ˆ Paste {{PiData.data.cpu_percent ?? 0}} in the β€œProgress(Progress)” field. This code references the data stream from your API named PiData. The key cpu_percent contains the data that Appsmith uses to display the CPU utilization percentage.
β—ˆ Add a β€œText(Text)” widget as a label below the β€œProgress Bar(Progress Bar)” widget.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Binding Data in the Configuration Screen

In the CPU section, there are three β€œStat Box(Stat Box)” widgets. The steps to bind data to each widget are exactly the same as the steps to bind the β€œProgress Bar(Progress Bar)” widget, except you need to bind different data properties from the .data operator. Follow the same steps, but with the following exceptions:

β—ˆ Use {{${PiData.data.cpu_freq[0]} ?? 0 }} to display the clock speed.
β—ˆ Use {{${PiData.data.cpu_count} ?? 0 }} to display the CPU count.
β—ˆ Use {{${(PiData.data.sensor_temperatures).toPrecision(3)} ?? 0 }} to display the CPU temperature data.

If everything goes smoothly, you will get a beautiful dashboard as shown below:

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Raspberry Pi Dashboard

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

CPU Utilization Trend Chart

You can use the β€œChart(Chart)” widget to display CPU utilization as a trend line and have it automatically update over time.

First, click on the widget, find the β€œChart Type(Chart Type)” property on the right, and change it to β€œLine Chart(LINE CHART)”. To display the trend line, you need to store cpu_percent in an array of data points. Your API currently returns it as a single time data point, so you can use Appsmith’s storeValue function (a native implementation of Appsmith’s built-in setItem method) to obtain an array.

Click the β€œ+” button next to β€œQuery or JS(QUERIES/JS)” and name it β€œutils”.

Paste the following JavaScript code into the β€œCode(Code)” field:

export default {
  getLiveData: () => {
  //When switch is on:
    if (Switch1.isSwitchedOn) {
      setInterval(() => {
        let utilData = appsmith.store.cpu_util_data;

        PiData.run()
          storeValue("cpu_util_data", [...utilData, {
            x: PiData.data.cpu_percent,
            y: PiData.data.cpu_percent
          }]);           
        }, 1500, 'timerId')
      } else {
    clearInterval('timerId');
  }
},
initialOnPageLoad: () => {
  storeValue("cpu_util_data", []);
  }
}

To initialize the Store, you create a JavaScript function in the initialOnPageLoad object and put the storeValue function in it.

You use storeValue("cpu_util_data", []); to store the value in cpu_util_data using the storeValue function. This function runs when the page loads.

So far, every time the page refreshes, the code will store a data point in cpu_util_data in the Store. To store an array, you use the index variables x and y, both storing the values from the cpu_percent data property.

You also want to automatically store this data by setting a fixed time interval between stored values. When executing the setIntervalπŸ”— docs.appsmith.com function:

β—ˆ Get the values stored in cpu_util_data.
β—ˆ Call the API PiData.
β—ˆ Update cpu_util_data using the latest cpu_percent data returned.
β—ˆ Store the value of cpu_util_data in the key utilData.
β—ˆ Repeat steps 1 to 4 only when the function is set to run automatically. You use the Switch widget to set it to run automatically, which explains why there is a parent function called getLiveData.

In the β€œSettings(Settings)” tab, find all parent functions in the object and set initialOnPageLoad to β€œYes” in the β€œRun on Page Load(RUN ON PAGE LOAD)” option.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Setting Functions to Run on Page Load

Now refresh the page to confirm.

Return to the canvas. Click the β€œChart(Chart)” widget and find the β€œChart Data(Chart Data)” property. Paste {{ appsmith.store.disk_util_data }} to bind it. This way, if you run the object utils multiple times, you will get chart data. To run this automatically:

β—ˆ Find and click the β€œLive Data Switch(Live Data Switch)” widget in the dashboard title.
β—ˆ Find the onChange event.
β—ˆ Bind it to {{ utils.getLiveData() }}. The JavaScript object is utils, and getLiveData is the function that activates when you toggle the switch, fetching real-time data from your Raspberry Pi. However, there are other real-time data, so the same switch applies to them as well. Keep reading for more details.

Binding data to the memory and disk sections of the widgets is similar to what you did in the CPU statistics section.

For the memory section, bind as follows:

β—ˆ The binding in the progress bar is: {{( PiData.data.cpu_mem_avail/1000000000).toPrecision(2) * 100 ?? 0 }}.
β—ˆ The bindings for the three stat box widgets are: {{ ${PiData.data.cpu_mem_used/1000000000).toPrecision(2)} ?? 0 }} GB, {{ ${PiData.data.cpu_mem_free/1000000000).toPrecision(2)} ?? 0}} GB, and {{ ${PiData.data.cpu_mem_total/1000000000).toPrecision(2)} ?? 0 }} GB.

For the disk section, the bindings for the progress bar and stat box widgets are:

β—ˆ The binding for the progress bar is: {{ PiData.data.disk_usage_percent ?? 0 }}.
β—ˆ The bindings for the three stat box widgets are: {{ ${PiData.data.disk_usage_used/1000000000).toPrecision(2)} ?? 0 }} GB, {{ ${PiData.data.disk_usage_free/1000000000).toPrecision(2)} ?? 0 }} GB, and {{ ${PiData.data.disk_usage_total/1000000000).toPrecision(2)} ?? 0 }} GB.

The chart here needs to update the utils object you created for CPU statistics, using storeValue with the key named disk_util_data, nested under getLiveData, with logic similar to that of cpu_util_data. For the disk utilization chart, the logic for storing disk_util_data is the same as that for the CPU utilization trend chart.

export default {
  getLiveData: () => {
  //When switch is on:
    if (Switch1.isSwitchedOn) {
       const cpuUtilData = appsmith.store.cpu_util_data;
       const diskUtilData = appsmith.store.disk_util_data;                    
       PiData.run();
       storeValue("cpu_util_data", [...cpuUtilData, { x: PiData.data.cpu_percent,y: PiData.data.cpu_percent }]);
       storeValue("disk_util_data", [...diskUtilData, { x: PiData.data.disk_usage_percent,y: PiData.data.disk_usage_percent }]);
    }, 1500, 'timerId')
  } else {
    clearInterval('timerId');
  }
},
  initialOnPageLoad: () => {
    storeValue("cpu_util_data", []);
    storeValue("disk_util_data", []);
  }
}

Overall, it looks beautiful, minimal, and very useful.

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes

Enjoy using it!

As you become more familiar with psutils, JavaScript, and Appsmith, I believe you will find it easy to infinitely tweak your dashboard to implement very cool features, such as:

β—ˆ View trends for the previous week, month, quarter, year, or any custom range allowed by your Raspberry Pi data
β—ˆ Build alert mechanisms for threshold violations of any statistics
β—ˆ Monitor other devices connected to your Raspberry Pi
β—ˆ Extend psutils to another computer with Python installed
β—ˆ Monitor your home or office network with other libraries
β—ˆ Monitor your garden
β—ˆ Track your own lifestyle habits

Have fun with your next exciting project!

(Cover Image: MJ/9754eb1f-1722-4897-9c35-3f20c285c332)

via: https://opensource.com/article/23/3/build-raspberry-pi-dashboard-appsmith

Author: Keyur Paralkar Topic: lkxed Translator: ChatGPT Proofreader: wxy

This article is originally compiled by LCTT and honorably presented by Linux China

Build a Raspberry Pi Monitoring Dashboard in Under 30 Minutes
Welcome to reprint according to the CC-BY-SA agreement,
If you need to reprint, please leave a message under the article “Reprint: Public Account Name“,
We will add you to the whitelist and authorize “modifications when reprinting articles“.

Leave a Comment

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