Pneumatic Water Level Sensor – Software

This post covers the firmware, calibration and server side of the pneumatic water level sensor project. The hardware design (MPX2050DP, INA333, MCP3426, PCB) is described in the companion post: Pneumatic Water Level Sensor – Hardware. The full discussion thread with source files is on mikrocontroller.net.

The code was developed entirely with AI assistance (Claude) and reviewed by me. It runs on MicroPython on the Raspberry Pi Pico W.

Unterstützen

Wir sind unabhängig schalten keine Werbung und benutzen keine Affiliation Links. Wenn Du uns unterstützen möchtest, ist dies hier möglich: ♥

1. Firmware Overview

The software is split into two independent projects: the Pico W firmware and the Raspberry Pi server.

The Pico W runs MicroPython and consists of seven files:

  • epd_driver.py is the unmodified low-level driver for the Waveshare 2.9″ e-Paper display.
  • config.py holds all hardware constants and calibration coefficients (CAL_A, CAL_B) as a single source of truth. 
  • sensors.py encapsulates the MCP9808 temperature sensor and MCP3426 ADC behind clean driver classes. 
  • display_ui.py renders all four screen layouts — main view, 30-day graph, admin menu and boot splash — in native portrait mode (128×296 px). 
  • button.py implements debounced multi-tap detection (1 tap = instant measurement, 2 taps = graph, 3 taps = admin). storage.py manages three JSON files on the Pico flash: calibration parameters, 60-day daily aggregates for the graph, and a transmit queue for measurements not yet delivered to the Pi. 
  • wifi.py handles Wi-Fi connection, NTP time sync (Europe/Berlin) and HTTP communication with the Pi server;
  • .env credentials are loaded from .env file. 
  • main.py ties everything together: hardware initialisation, the hourly measurement loop, the display state machine and periodic polling for remote trigger requests from the Pi.

The Raspberry Pi runs a Flask/Gunicorn server (app.py) on port 5000, installed as a systemd service via install.sh. 

  • data_store.py persists every measurement in a plain CSV file — no database required — making the data directly portable and exportable.
  • The web front-end (index.html, style.css, chart_app.js) provides a live dashboard with current water level and temperature, a historical chart with selectable time range, and a button to trigger an instant measurement remotely.

2. Installation

2.1 Pico W Setup

  1. Copy all files from the pico folder to the Pico W using mpremote:
mpremote connect auto fs cp FILENAME.py :
  1. Create a .env file with your credentials and copy it to the Pico:
WIFI_SSID=YourNetwork
WIFI_PASS=YourPassword
PI_HOST=192.168.X.XX
PI_PORT=5000

2.2 Raspberry Pi Server Setup

  1. Copy all files from pi folder to the Raspberry Pi.
  2. Run install.sh to set up the Flask server and SQLite database.

3. ADC Configuration

The MCP3426 is configured as follows:

ADC_CFG = 0x88   # Channel 1, One-Shot, 16-bit, PGA ×1

Each measurement is averaged 32 times (integer summation, single float conversion at the end). This reduces noise by a factor of √32 ≈ 5.7. ADC resolution: 16-bit / 62.5 µV per LSB.

4. Calibration

4.1 Procedure

The calibration takes 5 measurement points at known reference pressures and computes the coefficients a and b via least-squares linear regression:

Pressure [kPa] = a · ADC [mV] + b

Reference pressures are applied using a precision mmHg manometer. Conversion: 1 mmHg = 1.35951 cm H₂O.

Calibration points used: 0 / 100 / 160 / 220 / 300 mmHg

4.2 Results

a = 0.037497 kPa/mV
b = −0.343484 kPa
Max error = 2.45 cm
Effective sensitivity: 26.9 mV/kPa (99.2% of theoretical)

The achieved maximum error of 2.45 cm over a 470 cm range exceeds the design target of 5 cm and corresponds to a relative accuracy of better than 0.5%.

4.3 Temperature Compensation

The MPX2050DP has on-chip temperature compensation (±1% VFSS over 0–85°C). At a 20°C temperature swing the residual error is under 0.4 cm – no software compensation is required. The MCP9808 temperature sensor on the board is available for logging and future use.

5. Display and User Interface

The Waveshare 2.9″ e-Paper display (128×296 px, SPI) shows the current water level, timestamp and last measurement result. Button interactions:

  • 1× press: Trigger immediate measurement
  • 2× press: Show 30-day graph (planned)
  • 3× press: Admin menu (planned)

6. Wi-Fi and Server

Once per hour the Pico W connects to Wi-Fi and transmits the measurement to a Raspberry Pi running a lightweight Flask server with SQLite storage. The server provides:

  • Real-time display of the current water level
  • 30-day chart
  • Annual archive
  • CSV export

Calibration data and measurements are stored on the Pico flash (JSON), so the device survives a power cycle without losing its configuration.

7. Outlook

  • Complete display menu with 30-day graph and admin mode
  • Persistent storage for all historical measurements on Pico flash
  • Web interface improvements: full year archive view, alerting
  • Housing build (report to follow)

Related Posts

Unterstützen

Wir sind unabhängig schalten keine Werbung und benutzen keine Affiliation Links. Wenn Du uns unterstützen möchtest, ist dies hier möglich: ♥

Download

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert