Skip to content

All_in_One 2.0: one firmware for every machine, on our MQTT broker - #2

Open
tanmoyAtb wants to merge 12 commits into
mainfrom
feat/aio-iot-mqtt
Open

tanmoyAtb wants to merge 12 commits into
mainfrom
feat/aio-iot-mqtt

Conversation

@tanmoyAtb

Copy link
Copy Markdown
Collaborator

Why

All MQTT machines went offline on Nov 12: the firmware checks AWS IoT's certificate against its build date, and AWS rotated it. Machines are moving to our own broker (iot-mqtt.headless.ltd, repo HeadlessTech/iot-mqtt) and get reflashed. This PR is that firmware, plus the memory problem, a simpler production setup, and merging the two-group machine in so one firmware covers every machine.

Don't flash machines in the field from this before the board test passes (checklist below), and before production VMS has moved to the broker.

What changed (one commit each)

  1. Build: the six libraries are copied into All_in_One/src (tools/vendor-libraries.sh), so the Arduino IDE and arduino-cli build exactly the same code with nothing to install but ESP32 board package 2.0.17. flash.bat (Windows: build, flash, show log) and tools/build.sh.
  2. config.h: the only file production edits (ID, type, size, GSM/Wi-Fi + site Wi-Fi, modules, server). config_check.h refuses missing/misspelled/conflicting settings with a message naming the setting. flash.bat shows the settings and asks before flashing.
  3. TLS randomness fix: SSLClient seeded TLS from analogRead(A5) = GPIO 33 = the column-6 motor pin: it switched that pin to analog before every connection (the real cause of the "GPIO 33 forced to INPUT" workaround) and gave almost no entropy. Patched to use the ESP32 hardware RNG.
  4. Our broker: broker.h (address + the machines' shared login, password at the top), our two CAs as trust anchors, AWS client certificate and private key removed. Certificates are valid from 2020, so no clock is needed (the Clock.h work on fix/ssl-and-timer isn't needed). Firmware version 2.0.0 sent in the "online" status; MQTT failure reasons logged.
  5. Display memory, about 110 KB more free heap: one framebuffer holding only the rows that are drawn (logo + one image), drawn in place and only when the picture changes. Before: two full buffers, about 170 KB of a ~300 KB heap.
  6. Two-group 160 merged in (MACHINE_TYPE TYPE_TWO_GROUP_160). Standard machines' loop is the previous loop() unchanged (checked identical); the two-group loop is All_in_One_Weird's (checked identical).
  7. Fixes: 405 ("not enough points") shows "limit crossed" instead of "something went wrong"; two-group shows "try again" after 25 s instead of rebooting.
  8. All_in_One_Weird deprecated: building it stops with a pointer to All_in_One.
  9. Docs: README.md (production flashing), AGENTS.md (how it works, server contract, memory, pins, gotchas).

Builds (tools/build-all.sh: 20/20 pass)

Configuration (real machine) Flash Static RAM
standard 160, GSM, card (JJMBRM160) 763 KB 29.4 KB
two-group 160, GSM, card + bKash (BBKD01) 890 KB 29.5 KB
standard 20, all modules (BAIO20) 910 KB 29.5 KB
standard 60, GSM (bracit-brm60-01) 847 KB 29.4 KB
standard 40, cash only (BBCM40) 742 KB 29.3 KB
standard 120, staging (CAAB120) 911 KB 29.5 KB
standard 160, Wi-Fi (UCEP02) 1,212 KB (92%) 49.0 KB

Before this PR (standard 20, all modules): 924 KB flash, 31.8 KB static RAM. Plus 13 config.h mistakes rejected with the right message.

Not verified yet: board test against staging

Set SERVER SERVER_STAGING and a machine ID that exists in the staging DB, flash, keep the serial log:

  • boot banner shows the settings; free heap after display about 110 KB higher than before
  • TLS to iot-mqtt.headless.ltd (BearSSL with our CAs) and MQTT login; "online" appears on vms-new
  • sync answered with the stock count
  • card dispense, motor runs, confirmation; unknown card; 405 screen
  • cash (if wired): pulses per ten-taka note
  • two-group (if wired): both buttons, both groups, BELT two-motor columns, GPIO 34/35 limit switches
  • recovery after network loss and a broker restart
  • motor timeout: what happens physically (hardware team)

Notes

  • SSL.h was the only file with Windows line endings; it now has Unix ones like the rest.
  • broker.h holds the shared machine password on purpose (every machine carries it in flash anyway).
  • Rdm6300 and SSLClient are GPL-3 and TinyGSM is LGPL, as before; they're now in the repo rather than installed.
  • After this merges and the board test passes: tag fix/ssl-and-timer as archive/clock-ntp and delete the branch.

🤖 Generated with Claude Code

tanmoyAtb and others added 12 commits November 27, 2026 00:08
The six libraries (ArduinoJson 7.4.3, MultiButton 1.3.0, PubSubClient 2.8,
Rdm6300 2.0.0, SSLClient 1.6.11, TinyGSM 0.12.0) are copied into
All_in_One/src/ by tools/vendor-libraries.sh. The Arduino IDE and arduino-cli
now build exactly the same code with nothing to install but the ESP32 board
package; Rdm6300 is not in the Arduino library registry, so version pinning
alone could not do this. The only change to the copies: includes of a
library's own files are made relative to the including file.

flash.bat (Windows, production) and tools/build.sh build with ESP32 board
package 2.0.17 (installed if missing), flash and show the serial log.

Checked: with no libraries installed, the sketch builds to the same size as
before (924,381 bytes flash, 31,760 bytes static RAM).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Production now edits only All_in_One/config.h: machine ID, machine type,
size, GSM or Wi-Fi (with this site's Wi-Fi name and password), installed
modules and production or staging server. config_check.h stops the build
with a message naming the setting when one is missing, misspelled, has a
value that isn't allowed, or doesn't work with the others (e.g. a module
switch typed as HAS_RFIDX, a machine ID with a space, Wi-Fi without a
network name, two-group with cash). It also requires ESP32 board package
2.0.17. The committed config.h has MACHINE_ID "CHANGE_ME", which can't be
built, so nothing is flashed with another machine's settings.

The boot log prints the configuration; the Wi-Fi password is no longer
printed. flash.bat shows config.h's settings and asks before flashing.

tools/build-all.sh builds real machine configurations and checks that 13
kinds of config.h mistake are rejected. TYPE_TWO_GROUP_160 is refused until
the two-group machine is merged in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…or pin

SSLClient seeds BearSSL's random generator (its only seed) with 16
analogRead()s of the pin given to its constructor. The firmware passed A5,
which on the ESP32 Dev Module is GPIO 33: the column-6 motor pin. So every
TLS connection switched that motor pin to an analog input (the real reason
for the "GPIO 33 is forced back to INPUT" workaround in Dispense.h), and the
seed was readings of a pin held LOW, i.e. nearly constant: weak TLS keys.

The library copy is patched (by tools/vendor-libraries.sh, so the patch
survives a library upgrade or fails loudly) to use esp_fill_random(). The
GPIO 33 OUTPUT safeguard stays, with the real reason in its comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- broker.h: broker address and the machines' shared login ("vms"); the
  password is at the top of the file, not in config.h.
- IoT_Root_CA.h (from iot-mqtt/scripts/bearssl_trust_anchors.py): our main
  and backup CA, valid 2020-2050, replacing Amazon Root CA 1. The broker
  certificate is valid 2020-2045, so BearSSL's check against the build date
  passes for any build from 2020 on; no clock needed.
- The AWS device certificate and private key (the same in every build) are
  removed; client ID = machine ID, which the broker requires for a
  machine's own topics.
- A failed connection logs PubSubClient's state (4 = wrong password).
- version.h: FIRMWARE_VERSION 2.0.0. The "online" status now carries it with
  the machine type and build time ({"message":"online","fw":"2.0.0 STD
  Nov 27 2026 10:15:00"}); the server ignores unknown fields. Also printed
  at boot, with two heap diagnostics (from fix/ssl-and-timer).
- SSLClient no longer gets GPIO 33 as its "analog pin" (unused since the
  previous commit).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The display kept a front and a back buffer of 336x240, allocated as 480
separate blocks: about 170 KB of the ESP32's ~300 KB heap, which left Wi-Fi
machines close to the "free heap < 55 KB -> restart" guard before TLS.

Only the logo (220x47) and one 168x124 message image are ever drawn; the
rest of the screen is black. Now:
- one framebuffer, drawn in place (an image takes about 1 ms to draw, so
  there is no visible flicker)
- only the rows the logo and the image cover have memory, as one block;
  every other row shows one shared black row (the video interrupt reads each
  line through a row pointer). About 60 KB in total.
- the logo is drawn once at boot; showImage() draws only when the picture
  changes, so the idle screen is no longer redrawn on every loop pass
- each image's size is checked at build time (all must be 168x124)

The boot log prints the free heap before and after the display starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MACHINE_TYPE TYPE_TWO_GROUP_160 in config.h now builds what
All_in_One_Weird did, from the same code as every other machine:
- buttons: top = BELT group, bottom = PANTY group (staging builds: long
  press top = reconnect, bottom = sync); then the customer taps the card
  within 15 s, and the request carries the group as "size"
- motors: columns 1-2 two motors and two limit switches each, columns 3-6
  one motor each, limit switches on GPIO 34/35 (board pull-ups needed)
- per-group stock from the sync reply's "stock" object
- screens "push button or scan QR" (idle) and "hold card"

Standard machines are unchanged: their loop is the previous loop() under
the name loopStandard (checked to be identical text), and the two-group
loop is All_in_One_Weird's loop (identical text, apart from the
sendDispenseRequest name). Code that only one type uses is built only for
that type. Builds: two-group 890 KB flash, standard 160 763 KB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ting

- The server answers 405 when a card doesn't have enough points; the
  firmware had no case for it and showed "something went wrong / try
  again". It now shows the "limit crossed" screen (no image exists for
  "not enough points"; reusing one costs no memory), or "something went
  wrong" on machines without a card reader.
- Two-group machine: when a card request was still unanswered after the
  15 s group window, it rebooted 15 s after the request. It now does what
  standard machines do: shows "try again" after 25 s and keeps running; a
  late reply still dispenses and can't dispense twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n_One

The two-group 160 machine is now All_in_One with MACHINE_TYPE
TYPE_TWO_GROUP_160. All_in_One_Weird.ino starts with an #error so it can't
be flashed by mistake; DEPRECATED.md explains the config.h settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README.md: what each folder is, one-time setup, editing config.h,
flash.bat, what a good log looks like, what each build error and
connection failure means, the Arduino IDE route, testing on staging.

AGENTS.md: how All_in_One works (boot, loop, dispense with dispense-number
history, bKash, two-group, recovery), the MQTT contract with VMS, memory
budget and display design, TLS (BearSSL, certificate dates vs build date,
our SSLClient patch), pins per machine type, gotchas, and the open
questions for the hardware team.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tests/host/dispense_history_test.cpp runs Global.h on the computer with
stand-ins for FreeRTOS mutexes, Preferences (NVS that survives a simulated
reboot) and Serial: the last 100 numbers are kept, older ones forgotten,
the history survives a reboot, a corrupted or wrong-size NVS record starts
empty, and no path leaves a mutex taken (22 checks). tools/test-host.sh
builds and runs it.

GitHub Actions (firmware.yml) runs the host tests and tools/build-all.sh
(every machine type + 13 config.h mistakes) on every PR and push to main,
with ESP32 board package 2.0.17 cached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
GCC on the CI runner warns that the two-group variables in Global.h are
unused in the host test (they're used by the sketch); clang didn't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- build-all.sh: stop if cd fails; check arduino-cli's result directly
  instead of through $? (SC2164, SC2181); the expected message is matched
  from a here-string, not a pipe.
- vendor-libraries.sh: the library names are a bash array; the license file
  is found with find instead of ls | grep (SC2010, SC2086). A rerun produces
  a byte-identical All_in_One/src/.
- CI runs ShellCheck on every *.sh before the host tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant