Jump to content

Found use for an otherwise useless 2nd gen Kindle Fire - A simple PC monitor

steelo

Hey guys,

 

In case anybody is interested - Here is a simple Python/java script that runs on a server and streams usage information to an outdated Kindle (or pretty much any device with a web browser). It's pretty basic, but having a small tablet which shows my CPU, memory and disk usage in real time is satisfying. Feel free to improve/add on if you have any use for this!

 

image.png.3111aa2dd2d541fe97dfbc024bb8178c.png

 

 

 

Python/java code (if you want to review), files and instructions below:

 

Open python compiler and insert the following:

 

from flask import Flask, jsonify, render_template
import psutil

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/system_info')
def system_info():
    cpu_usage = psutil.cpu_percent(interval=1)
    memory_info = psutil.virtual_memory()
    disk_info = psutil.disk_usage('/')
    net_info = psutil.net_io_counters()
    
    data = {
        'cpu_usage': cpu_usage,
        'memory_percent': memory_info.percent,
        'disk_percent': disk_info.percent,
        'network_sent': net_info.bytes_sent / (1024 * 1024),  # MB
        'network_recv': net_info.bytes_recv / (1024 * 1024),  # MB
    }
    
    return jsonify(data)

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

 

 

Create a file called 'index.html' in notepad and insert the following text:

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>System Monitor</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
    <style>
        body {
            background-color: #000; /* Black background */
            color: #fff; /* White text */
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
        }
        .container {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
            padding: 20px;
        }
        .chart-container {
            width: 30%;
            height: 300px;
            margin-bottom: 20px;
            text-align: center;
        }
        canvas {
            width: 100% !important;
            height: 100% !important;
        }
        .label {
            color: #fff;
            margin-top: 10px;
            font-size: 18px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="chart-container">
            <canvas id="cpuChart"></canvas>
            <div class="label">CPU Usage</div>
        </div>
        <div class="chart-container">
            <canvas id="memoryChart"></canvas>
            <div class="label">Memory Usage</div>
        </div>
        <div class="chart-container">
            <canvas id="diskChart"></canvas>
            <div class="label">Disk Usage</div>
        </div>
    </div>

    <script>
        function createSkinnyPieChart(ctx, data, label) {
            return new Chart(ctx, {
                type: 'doughnut',
                data: {
                    labels: [label, 'Remaining'],
                    datasets: [{
                        data: data,
                        backgroundColor: ['#ff8c00', '#444'], // Orange and gray
                        borderWidth: 0
                    }]
                },
                options: {
                    cutoutPercentage: 80, // Makes the pie chart skinny
                    rotation: -Math.PI / 2,
                    legend: { display: false },
                    responsive: true,
                    maintainAspectRatio: false,
                    tooltips: { enabled: false },
                    hover: { mode: null },
                    plugins: {
                        beforeDraw: function(chart) {
                            var width = chart.chart.width,
                                height = chart.chart.height,
                                ctx = chart.chart.ctx;

                            ctx.restore();
                            var fontSize = (height / 114).toFixed(2);
                            ctx.font = fontSize + "em sans-serif";
                            ctx.textBaseline = "middle";

                            var percentage = chart.config.data.datasets[0].data[0];
                            var text = percentage.toFixed(1) + "%",
                                textX = Math.round((width - ctx.measureText(text).width) / 2),
                                textY = height / 2;

                            ctx.fillStyle = '#fff'; // White text
                            ctx.fillText(text, textX, textY);
                            ctx.save();
                        }
                    }
                }
            });
        }

        var cpuChart = createSkinnyPieChart(
            document.getElementById('cpuChart').getContext('2d'),
            [0, 100],
            'CPU Usage'
        );

        var memoryChart = createSkinnyPieChart(
            document.getElementById('memoryChart').getContext('2d'),
            [0, 100],
            'Memory Usage'
        );

        var diskChart = createSkinnyPieChart(
            document.getElementById('diskChart').getContext('2d'),
            [0, 100],
            'Disk Usage'
        );

        function fetchData() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/system_info', true);
            xhr.onload = function() {
                if (xhr.status === 200) {
                    var data = JSON.parse(xhr.responseText);

                    // Update charts with real-time data
                    updatePieChart(cpuChart, data.cpu_usage);
                    updatePieChart(memoryChart, data.memory_percent);
                    updatePieChart(diskChart, data.disk_percent);
                }
            };
            xhr.send();
        }

        function updatePieChart(chart, value) {
            chart.data.datasets[0].data = [value, 100 - value];
            chart.update();
        }

        setInterval(fetchData, 1000); // Fetch data every second
        fetchData(); // Initial fetch
    </script>
</body>
</html>

 

Create a folder called 'templates' and move the index.html into this folder. It is important that the templates folder is in the same location as the .py file (python script)

Run the python script using a compiler (I use Thonny) 

The bottom shell section should tell you the ip address it is streaming to

Open up the ip address in a web browser on your tablet or smart phone

 

system_monitor.py index.html

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×