DIY Sensor Panel: Dynamic Data Display From Text Files

by Pedro Alvarez 55 views

Hey guys! Let's dive into creating a simple sensor panel that pulls data from text files and displays it dynamically. This is perfect for a basic monitoring setup, and it's not meant to replace any full-fledged software, just a cool proof of concept for an AOOSTAR-X screen alternative. Think of it as a neat way to visualize your system's vitals!

Image

JSON Configuration: The Blueprint

Our sensor panel relies on a JSON configuration file to define how everything looks and works. This file acts like a blueprint, telling the panel which background image to use and where to display sensor readings. Here's a breakdown of what the JSON file covers:

  • Background Image Filename: Specifies the image to use as the backdrop for your sensor readings. This could be anything from a sleek system diagram to a cool graphic.
  • Sensor Fields: Defines the individual sensor readouts we want to display. For each sensor, we need to specify:
    • Location: The x and y coordinates on the background image where the sensor value should appear. Think of it like placing labels on a map.
    • Font Size: How big the text should be, so it's easily readable but doesn't clutter the screen.
    • We aim to use a subset of the official AOOSTAR-X JSON configuration file for compatibility and ease of use. This allows us to leverage existing configurations and adapt them for our needs.

Sensor Values from Text Files: The Data Source

Now, where does all this sensor data come from? In our simple setup, we're using text files as the data source. This keeps things straightforward and easy to manage. Here's the lowdown on how these text files work:

Text File Format

  • File Extension: We're using .txt files for simplicity.
  • Key-Value Pairs: Each line in the file represents a sensor reading in the format key: value. For example, cpu_temperature: 65.
  • Line-Based: Each sensor reading gets its own line in the file. This makes parsing and updating the data a breeze.
  • Whitespace Handling: Any extra spaces or tabs around the key and value are automatically trimmed. No need to worry about messy formatting!
  • Comments: You can add comments to your text files by starting a line with #. These comments are ignored by the sensor panel, making it easy to document your setup.
  • Special Keys for Units: We have a clever way to handle units of measurement. If a key ends with #unit, its value is treated as the unit for the corresponding sensor. For example, net_download_speed#unit: M/S specifies that the unit for net_download_speed is Megabytes per second. This is super useful for dynamic units that might change depending on the sensor value.

Automatic Updates

One of the coolest features is that the sensor panel automatically reads and updates its display whenever the text files change. This means your sensor readings are always up-to-date!

To ensure data integrity, it's crucial that the sensor value provider (the program writing the data to the text file) uses atomic file updates. The best practice here is to write the new values to a temporary file on the same filesystem and then use a move or rename operation to replace the old file. This prevents partial or corrupted data from being read by the sensor panel.

Multiple Sensor Files

We support using one or multiple sensor text files. You can specify either a single file path or a directory path.

  • If you specify a single file, the sensor panel will monitor that file for changes.
  • If you specify a directory, the panel will read and monitor all files within that directory that match the sensor file naming pattern. This is great for organizing your sensor data into separate files for different components (CPU, GPU, etc.).
  • Subdirectories are ignored, keeping things simple and preventing accidental inclusion of unrelated files.

Example Text File

Here's an example of what a sensor text file might look like:

cpu_temperature: 65
cpu_percent: 98
memory_usage: 77
memory_Temperature: 48
net_ip_address: 146.56.182.244
gpu_core: 98
gpu_temperature: 78
net_upload_speed: 100
net_upload_speed#unit: K/S
net_download_speed: 120
net_download_speed#unit: M/S
motherboard_temperature: 38
storage_ssd[0]['temperature']: 31
storage_ssd[0]['used']: 17
storage_ssd[1]['temperature']: 32
storage_ssd[1]['used']: 27
storage_ssd[2]['temperature']: 33
storage_ssd[2]['used']: 37
storage_ssd[3]['temperature']: 34
storage_ssd[3]['used']: 47
storage_ssd[4]['temperature']: 35
storage_ssd[4]['used']: 57
storage_hdd[0]['temperature']: 36
storage_hdd[0]['used']: 17
storage_hdd[1]['temperature']: 37
storage_hdd[1]['used']: 27
storage_hdd[2]['temperature']: 38
storage_hdd[2]['used']: 37
storage_hdd[3]['temperature']: 39
storage_hdd[3]['used']: 47
storage_hdd[4]['temperature']: 40
storage_hdd[4]['used']: 57
storage_hdd[5]['temperature']: 10
storage_hdd[5]['used']: 67

Display Rendering: Bringing it to Life

Now that we have our configuration and data source sorted, let's talk about how we actually display the sensor values on the screen. For this initial version, we're focusing on simple sensor value fields. No fancy graphics just yet, but we can always add those later!

Supported Fields

For each sensor value, we support the following fields in the JSON configuration:

  • label: The text label to display next to the sensor value (e.g.,