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)
| Item | Value you saw | Meaning |
|---|---|---|
| ADC scale 0.000149 | 2.5 V / 224 | Unipolar, 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 -32768 | 30 V / 65536 | The channel is on the ±15 V span. 0 V is code 32768, 5 V is about code 43691. |
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
- Settings block at the top:
ADC_NAME,DAC_NAME,OUT_GAIN,IN_GAINand theDEVICESlist. One line per controller. To add one, add a line. - Board access:
read_volts(d)andwrite_volts(d, volts)are the only functions that touch hardware.--simreplaces them with a fake that echoes written volts. Changing the backend (pylibiio, pyadi-iio, or plain sysfs files) means changing only these two. - apply(d, value): clamps the value to 0..max, converts units to volts, writes it. Used by the page's Update button and by recipes.
- Loop (
loop(), 1 Hz, own thread): advances the recipe if one is running, reads every controller, appends to the plot history (last 600 points) and writes one log row. Errors go to a red line on the page and never stop the loop. - Recipe: steps of time plus values per device. Between steps it ramps linearly or jumps (checkbox). It runs on the server, so closing the browser does not stop it. While it runs, manual set values are refused. Stopping a recipe leaves outputs where they are.
- Logging: every second a CSV row goes to
logs/buffer.log. At 50 MB it is renamedbuffer.log.1(replacing the old one) and a new file starts, so at most 100 MB is ever kept. After Start, the same rows also go toruns/USER_TRIAL.log. Names are cleaned (spaces and symbols become dashes). Reusing a name appends to the existing file.
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
| Step | What changed | Why |
|---|---|---|
| 1 | First 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. |
| 2 | Ported to FastAPI, same endpoints. | You asked. Added input validation and an API test page. |
| 3 | Live charts, first uPlot then Chart.js. | You wanted real-time plots. Chart.js chosen at your request. |
| 4 | Two-frame layout, trial name + user, trial log file, 100 MB rolling buffer, outputs not driven until first set. | Your layout and logging spec. |
| 5 | Rewrite 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. |
| 6 | Hardware 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. |
| 7 | Suggested 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
| Symptom | Likely cause | What to do |
|---|---|---|
| Browser shows 404 Not Found | index.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 computer | Different 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 message | A 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 value | The 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 noisy | Unipolar 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 V | The write ignores offset on a ±15 V span. | Use the formula in section 2. Test with controllers disconnected. |
| Output voltage is a constant factor off | The 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 connect | Package 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 list | The 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" line | Disk 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 starting | An old copy (tmux or the service) still holds port 5000. | sudo systemctl stop cvd or stop the other copy. |
| Service does not start after reboot | Wrong 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
| Route | Does | Password |
|---|---|---|
GET /api/data | Readings, setpoints, history, run and recipe status, errors | no |
POST /api/set | Set one controller (name, value). Refused while a recipe runs. | yes |
POST /api/run/start, /api/run/stop | Start or stop the trial log | yes |
POST /api/recipe/start, /api/recipe/stop | Start a recipe (steps, ramp) or stop and hold | yes |
POST /api/estop | Stop the recipe and put outputs at their safe or zero value | no, on purpose |
Settings you will edit
| Name | Meaning |
|---|---|
ADC_NAME, DAC_NAME | Kernel device names (ad7124-8, ltc2688) |
DEVICES fields | name (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_GAIN | Correction for board output or input stages |
BUFFER_LIMIT, HISTORY | Bytes 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
- Safety limit in the DAC write: clamp volts to 0..
vmaxso no bug or bad calibration can leave the controller's range. - Set the DAC span to 0-5 V in the device-tree overlay: then negative output is impossible in hardware and the step size improves to about 0.076 mV. Needs the CN0554 overlay documentation and a reboot.
- Calibration tables: separate "set" and "read" tables per device (CSV), with straight-line interpolation. The read table must rise with value.
- Network exposure: the password travels unencrypted over HTTP. Keep the server on the lab network or use an SSH tunnel, never open port 5000 to the internet.
- Ideas not built: setpoint lines on the plots, "go safe when the recipe ends", rate limits for furnace ramps, resuming a trial after reboot, event lines in the logs, a second board.
- Before real growths: press Emergency stop once with controllers connected and confirm each one goes where you expect, compare readings against the controllers' own displays at two points, and delete test files from
runs/.