CVD control: development notes

Flask + Chart.js web interface for a CVD furnace, running on a Raspberry Pi with a CN0554 board. Written November 2026. Everything was tested in simulation mode; hardware behaviour reflects what you reported.

1. Overview

A browser page with two frames. The left frame, CVD Control, holds the growth-trial name, one set-value box plus live reading per controller, and a recipe table. The right frame plots all readings. The Python server talks to the board, runs recipes, and keeps two logs.

cvd_simple/
  app.py                  server: board I/O, 1 Hz loop, recipe, logging, web routes
  static/index.html       the page (plain JavaScript, no framework)
  static/chart.umd.min.js Chart.js, kept local so the Pi needs no internet
  CVD_notes.html          this file
  logs/buffer.log(.1)     created at run time: rolling log, 2 x 50 MB
  runs/USER_TRIAL.log     created at run time: one file per growth trial
python3 app.py --sim       # test the page without hardware
sudo python3 app.py        # real board (DAC writes need root), then http://PI-ADDRESS:5000

2. Hardware and units

The CN0554 carries an AD7124-8 ADC (used differentially) and an LTC2688 DAC. The Linux kernel exposes both as IIO devices. Each channel has three numbers:

ADC:  volts = (raw + offset) * scale / 1000 * IN_GAIN     (scale is in mV per code)
DAC:  code  = volts / OUT_GAIN / (scale/1000) - offset     (clamped to 0..65535)
ItemValue you sawMeaning
ADC scale 0.0001492.5 V / 224Unipolar, 2.5 V reference, gain 1. Bipolar at gain 1 would print 0.000298. Check the offset file: 0 is unipolar, -8388608 is bipolar.
DAC scale 0.4577, offset -3276830 V / 65536The channel is on the ±15 V span. 0 V is code 32768, 5 V is about code 43691.
Danger on a ±15 V span: code 0 is -15 V. A DAC write that ignores offset puts negative volts on a controller's setpoint input. The server therefore always subtracts the offset, and you should keep controllers disconnected when testing any change to the write code.

The ADC chip reads at most 2.5 V between its pins. Whether 5 V at the board terminal fits depends on the CN0554 input stage. Measure it (see troubleshooting) and put the ratio in IN_GAIN, or build a calibration table.

3. How the code works

app.py

Log columns: time, trial, recipe, NAME_pv ..., NAME_sp ... (pv = reading, sp = last setpoint, empty until you set one).

index.html

Builds the page once from /api/data, then refreshes the numbers and charts every second. Charts use a plain category axis with clock-time labels, so no date adapter is needed. Each controller type gets one chart (mass flow, pressure, temperature).

4. How it got here

StepWhat changedWhy
1First version: Python server, HTML page, Manual / Recipe / Monitor tabs, extendable controller list in devices.json.Your first spec. The third mode was not defined, so Monitor (read-only) was a guess.
2Ported to FastAPI, same endpoints.You asked. Added input validation and an API test page.
3Live charts, first uPlot then Chart.js.You wanted real-time plots. Chart.js chosen at your request.
4Two-frame layout, trial name + user, trial log file, 100 MB rolling buffer, outputs not driven until first set.Your layout and logging spec.
5Rewrite as a small Flask app (cvd_simple/), one Python file and one HTML file.The previous code was too large to maintain yourself. Dropped rate limits, event lines, setpoint overlays, multi-board support.
6Hardware fixes: AD7124-8 and LTC2688 names, differential channel names, offset in the DAC write, --list, clearer errors.My first guesses (AD7124-4, AD5686R, single-ended names) were wrong for your board.
7Suggested edits: calibration functions, password for changes, zero outputs on start and emergency stop, plain-sysfs alternative to pylibiio, systemd service.Your later requests. These were given as snippets, so your file may not contain all of them (checklist below).

The older FastAPI version may still be in your cvd/ folder. It is larger and has extra features (rate limits, event log lines, dashed setpoint lines); keep it only as a reference.

Is my app.py up to date?

grep -n "offset" app.py          # DAC write must subtract the offset
grep -n "def to_volts" app.py    # calibration functions
grep -n "need_password" app.py   # password protection
grep -n "zero_outputs" app.py    # zero on start and on emergency stop

No output for a line means that change is not in your copy.

5. Troubleshooting

SymptomLikely causeWhat to do
Browser shows 404 Not Foundindex.html is not in a static/ folder next to app.py.Use the folder layout in section 1. Start the server from inside that folder.
TypeError: unsupported operand type(s) for |: 'dict' and 'dict'Python older than 3.9.Use {**a, **b} instead of a | b, or upgrade Python (Raspberry Pi OS Bookworm has 3.11).
Page loads but cannot be reached from another computerDifferent network, firewall, or server bound to localhost.Check hostname -I on the Pi, both machines on the same LAN, host="0.0.0.0" at the bottom of app.py.
ADC shows "-" or a red channel not found messageA differential channel is not called voltage0. It is voltage0-voltage1. Or the device-tree overlay is not loaded.List the channels (section 6). Copy the exact names into ai. If the AD7124 shows no channels, check the CN0554 overlay line in config.txt.
Reading flatlines near 2.5 V or looks half the real valueThe input range at the chip is 2.5 V. A 5 V signal needs a divider.Apply known voltages (0.5 V, 1.0 V), compare with a multimeter, set IN_GAIN to applied/shown, or use a calibration table. Check the board manual before connecting 5 V or more.
Zero flow reads slightly high or noisyUnipolar ADC clips small negative noise to 0.Add a zero point to the "read" calibration table.
DAC does nothing(a) Writing needs root. (b) Channel is powered down. (c) No raw file because the channel is in toggle mode.(a) Run with sudo. (b) cat out_voltage0_powerdown; write 0 if it is 1. (c) Look for raw0/raw1/toggle_en in the channel list. That is set in the overlay.
DAC output is -10 to -15 V when you ask for 0 to 5 VThe write ignores offset on a ±15 V span.Use the formula in section 2. Test with controllers disconnected.
Output voltage is a constant factor offThe board has an output stage the code does not know about.Measure 2.5 V and put the ratio in OUT_GAIN.
Kernel files work but Python does not connectPackage installed for a different Python (typical under sudo), or libiio and its Python bindings differ in version.Run python3 -c "import iio; print(iio.__file__)" as the user that runs the server. Or use plain sysfs reads and writes in read_volts/write_volts, which need no library.
find_channel(name) returns a channel missing from the channel listThe default is an input search. Or two wrapper objects for the same channel compare as different.Use find_channel(name, True) for the DAC. Compare ch.id and ch.output, not the objects.
Password prompt appears, then "Password required"Wrong or cancelled password.Any user name works. Closing the browser forgets the password. Emergency stop never asks.
Red "Log error" lineDisk full or no write permission in logs/ or runs/.Free space, or fix ownership (the service runs as root, so files are root-owned).
"Address already in use" when startingAn old copy (tmux or the service) still holds port 5000.sudo systemctl stop cvd or stop the other copy.
Service does not start after rebootWrong paths in the service file, packages missing for root, or zeroing the outputs failed.journalctl -u cvd -e shows the reason.

6. Reference

Web routes

RouteDoesPassword
GET /api/dataReadings, setpoints, history, run and recipe status, errorsno
POST /api/setSet one controller (name, value). Refused while a recipe runs.yes
POST /api/run/start, /api/run/stopStart or stop the trial logyes
POST /api/recipe/start, /api/recipe/stopStart a recipe (steps, ramp) or stop and holdyes
POST /api/estopStop the recipe and put outputs at their safe or zero valueno, on purpose

Settings you will edit

NameMeaning
ADC_NAME, DAC_NAMEKernel device names (ad7124-8, ltc2688)
DEVICES fieldsname (no quote characters), kind (mfc, pressure, temperature), ao DAC channel, ai ADC channel, unit, max full scale in units, vmax volts at full scale, safe emergency-stop value
OUT_GAIN, IN_GAINCorrection for board output or input stages
BUFFER_LIMIT, HISTORYBytes per buffer file (50 MB), plot points kept (600)
CVD_PASSWORD (environment)Password for changes, when the password edit is applied

Useful commands

grep . /sys/bus/iio/devices/*/name                        # which iio:deviceN is which chip
ls /sys/bus/iio/devices/iio:device0/                      # channel files (in_voltage0-voltage1_raw ...)
cat /sys/bus/iio/devices/iio:device0/in_voltage0-voltage1_{raw,scale,offset}
cat /sys/bus/iio/devices/iio:device1/out_voltage0_{raw,scale,offset,powerdown}
sudo systemctl restart cvd                                # after editing app.py
journalctl -u cvd -f                                      # live server messages
tail -n 5 logs/buffer.log                                 # latest logged rows

Starting at boot

A systemd service (/etc/systemd/system/cvd.service) runs python3 -u app.py as root from the cvd_simple folder, with the password in /etc/cvd.env (mode 600). Enable with sudo systemctl enable --now cvd. Every start zeroes the DAC outputs, so do not restart it during a growth run. A trial log is not resumed after a reboot; press Start again.

7. Safety notes and possible future work