26 min read · Automation

An inspection station that uses four device families at once usually breaks at the seams, not in the devices. Jetson answers half a second late, and the PLC does not know whether to wait or decide. A sensor node drops out, and the web page keeps showing the old value as if nothing happened. The factory network to the web goes down for two hours, and the data that fell in the gap is either lost for good or recorded twice when the link returns.
This article does not compare device specs. It designs the data contract between the layers, then writes a simulator in TypeScript that Node 24 runs directly. We inject one fault at a time and look at what each layer actually does when its neighbor disappears. A manager can read the tables and explanations to see who is responsible for what. An engineer can run the code and follow along.
The real problem: who owns the decision when a part goes missing
Consider three hypothetical situations. None of them is a BT San customer project.
- A defect inspection station on a conveyor: Jetson analyzes images when the PLC tells it to. One day the model loads slowly, and the result arrives after the next part has already come in. If the PLC applies the old result to the new part, a bad part slips through as good.
- A storage room with temperature nodes: a sensor board hangs, and the value on the web page stays frozen at 41 degrees all night with no alarm, because nobody checks how old that value is.
- A production line that sends data to a web report: the network switch up to the office fails for half a day, and the report has a gap. When the network returns, the system sends the backlog, but some of it is sent twice, so the production count in the report is too high.
All three are solved by the same principle: every data line needs a sequence number, a timestamp and a heartbeat. Every layer must know what it will do when its neighbor goes quiet. Safety-related work must sit in the layer whose response is the most deterministic.
Facts from the documents this design rests on
| Topic | What the document says | Effect on the design |
|---|---|---|
| MQTT keep alive | The MQTT 3.1.1 spec says the server must disconnect a client that sends nothing within 1.5 times the Keep Alive period, as if the network had failed. A value of 0 turns the mechanism off. | Silence must be treated as a fault, not assumed to mean everything is fine. The example uses the same principle as a heartbeat on every line. |
| MQTT Will Message | The spec says the will message must be published when the connection is closed without a prior DISCONNECT. | Let a node announce in advance whom to notify if it disappears. The receiving side does not have to wait and guess. |
| MQTT QoS 1 | QoS 1 is at least once: the message surely arrives but may be duplicated, and the DUP flag is set on a resend. QoS 2 is exactly once. | If you choose at-least-once delivery, the receiver must remove duplicates itself. The example does it with an id. |
| MQTT Clean Session | With Clean Session set to 0, the server keeps the QoS 1 and 2 messages of the subscription for a client that comes back. | This is one way to get store-and-forward on the broker side, but an edge-side buffer is still needed when the broker cannot be reached. |
| OPC UA | The OPC Foundation's introduction page describes a platform-independent, service-oriented architecture with encryption, message signing and sequencing against replay, plus an object-based data model that clients can browse. | Suited to lines that need a data model and built-in security, such as a gateway to a higher-level system. |
| Modbus | Modbus data lives in four tables. Coils and holding registers are read/write, while discrete inputs and input registers are read-only. Communication is request-response, and the client always starts it. | Holding registers can serve as the PLC-to-Jetson contract. But a slave does not send on its own when it is not asked, so a heartbeat is needed to show that the other side is still writing. |
| IEC 61131-3 | The overview says the standard defines the ladder, FBD, SFC and structured text languages, and lets you set up several tasks per resource that run periodically or on events. | Sequence logic and interlocks live in the PLC program that runs in scan cycles. They are not moved into web code. |
These documents describe the general mechanisms. They do not give the timeout that suits your station. All the numbers in the example (400 ms, 300 ms, 600 ms) are assumed values that must be measured and set on the real machine.
Who owns what: hard real-time, soft real-time and reporting
This table is the conclusion of the design. Every row has a single owner. When the owner disappears, no other layer quietly takes over its duty.
| Layer | Owns this kind of work | Duty in the example | If this layer is lost |
|---|---|---|---|
| PLC | Hard real-time | Sequencing, interlock, e-stop, driving the conveyor and the sorter, deciding PASS/REJECT | This is the layer that must not be lost. If it is lost, it must go to a safe state through hardwired circuits. |
| Jetson | Soft real-time | Analyzes images when the PLC asks and answers within the deadline | The PLC times out and REJECTs that part. If the heartbeat is lost, it REJECTs immediately without waiting. |
| MCU node | Soft real-time | Reads auxiliary sensors on a cycle and sends samples with a seq | The gateway sees that the data is not fresh. The PLC goes to DEGRADED (slower conveyor) but does not stop the factory. |
| Gateway | Buffering intermediary | Aggregates data, assigns seq, keeps an outbox, sends to the web and waits for an ack | The PLC sees the gateway heartbeat disappear and treats the auxiliary data as not fresh. Production continues. |
| Web app | Reporting | Shows status, history and alerts, and stores data without duplicates | Production is not affected. The edge keeps the data and sends it later when the link returns. |
A simple rule of thumb: the more a task is tied to safety and to exact timing, the closer it must be to the PLC and the farther from the network and general-purpose operating systems. The web is a reader. It never commands the conveyor.
Overview of the example
The code is in the folder examples/hybrid-plc-jetson-mcu-web-architecture/. It uses only Node 24 built-ins, with no npm install. You run it directly with node file.ts. Node strips the types without type checking and supports only syntax that can be erased, so there is no enum or parameter property, and imports must include the .ts extension, as the Node documentation says.
The simulator uses a virtual clock that advances in 10 ms steps, so it gives the same result every time and does not wait in real time. The downside is that it does not measure the real latency of the network or of Jetson.
| File | Role |
|---|---|
| contracts.ts | The PLC-to-Jetson register map, the MCU sample format, the gateway-to-web event format, and the timeouts |
| plc.ts | The PLC: interlock before everything else, the deadline for waiting for the inspection result, and the DEGRADED and SAFE_STOP states |
| devices.ts | The simulated Jetson (adds delay, can hang) and the simulated MCU node |
| edge.ts | The gateway (outbox, ack, resend, limited buffer) and the web side that removes duplicates by id |
| sim.ts | The simulator and the set of faults |
| demo.ts / sim.test.ts | A script that runs 7 scenarios, and a node:test suite with 6 cases |
Step 1: the data contract of each line
The first line is PLC to Jetson through 16-bit registers, as Modbus holding registers. The PLC writes the sequence number of the inspection request. Jetson writes the sequence number it is replying to, together with the result. Both sides write a heartbeat that keeps increasing. The reason for the sequence number is that a returned result must say which part it belongs to, not implicitly belong to the latest trigger.
// แผนที่รีจิสเตอร์ PLC<->Jetson (ตัวเลข 16 บิต ค่าสมมติ)
export type Regs = {
plc_trigger_seq: number; // PLC เขียน: เลขลำดับของการสั่งตรวจ เพิ่มทีละ 1
plc_hb: number; // PLC เขียน: heartbeat
jt_result_seq: number; // Jetson เขียน: สะท้อนเลขลำดับที่ตอบ
jt_verdict: number; // Jetson เขียน: 0=ยังไม่มี 1=ผ่าน 2=ไม่ผ่าน
jt_hb: number; // Jetson เขียน: heartbeat
gw_hb: number; // gateway เขียน: heartbeat
gw_node_ok: number; // gateway เขียน: 1 = ข้อมูลจากโหนด MCU ยังสด
};
export const newRegs = (): Regs => ({
plc_trigger_seq: 0, plc_hb: 0, jt_result_seq: 0, jt_verdict: 0, jt_hb: 0, gw_hb: 0, gw_node_ok: 1,
});The second line is MCU to gateway, as small JSON with node, the node's own seq, ts and the measured value. The third line is gateway to web. Every event has an id of the form edge1:sequence, which stays the same when the event is resent. The web uses this id to remove duplicates. This matches what the MQTT spec says about QoS 1 possibly resending, so the receiving side must take responsibility itself.
- seq must increase by one per sender, so the receiving side can see gaps (the example tests that the seq the web stores has no gaps after a replay)
- ts is used to tell the age of data, not to order it, because each device's clock runs differently
- heartbeat must be a counter that the receiver checks for change, not a constant that the sender could leave written when its program hangs
- A 16-bit number wraps around. The example uses & 0xffff when incrementing, so the comparison must use equality, not greater-than.
Step 2: the PLC makes the final decision and owns the interlock
In the example, the PLC scan does three things in order. It always checks the e-stop first, and if the e-stop is active it reads nothing from Jetson, the MCU or the web. Then it looks at the Jetson and gateway heartbeats, and only then decides on the part that is waiting for a result. The SAFE_STOP state can be left only by an operator reset. Simply releasing the e-stop is not enough.
trigger(now: number, partId: number) {
if (this.state === "SAFE_STOP") return; // สายพานหยุดอยู่ ไม่มีชิ้นงานให้ตรวจ
const seq = (this.regs.plc_trigger_seq + 1) & 0xffff; // 16 บิต วนกลับได้
if (!this.jetsonAlive(now)) return this.decide(now, { partId, seq, outcome: "REJECT", reason: "vision_offline" });
this.regs.jt_verdict = 0;
this.regs.plc_trigger_seq = seq;
this.pending = { seq, partId, deadline: now + T.visionTimeoutMs };
}
scan(now: number) {
this.regs.plc_hb = (this.regs.plc_hb + 1) & 0xffff;
if (this.estop) { // 1) interlock ก่อนเสมอ
this.state = "SAFE_STOP"; this.conveyorPct = 0; this.pending = null; return;
}
if (this.state === "SAFE_STOP") return; // ออกได้ด้วย reset() เท่านั้น
if (this.regs.jt_hb !== this.seen.jt) { this.seen.jt = this.regs.jt_hb; this.seen.jtAt = now; }
if (this.regs.gw_hb !== this.seen.gw) { this.seen.gw = this.regs.gw_hb; this.seen.gwAt = now; }
const gwFresh = now - this.seen.gwAt <= T.hbStaleMs;
const nodesOk = gwFresh && this.regs.gw_node_ok === 1;
this.state = nodesOk ? "RUN" : "DEGRADED"; // ข้อมูลเสริมหาย: ช้าลง ไม่หยุดโรงงาน
this.conveyorPct = nodesOk ? 100 : 50;
const p = this.pending;
if (!p) return;
if (this.regs.jt_result_seq === p.seq && this.regs.jt_verdict !== 0) {
const ok = this.regs.jt_verdict === 1;
this.decide(now, { partId: p.partId, seq: p.seq, outcome: ok ? "PASS" : "REJECT", reason: ok ? "vision_ok" : "vision_defect" });
} else if (now >= p.deadline) {
this.decide(now, { partId: p.partId, seq: p.seq, outcome: "REJECT", reason: "vision_timeout" });
}
}- A Jetson result is used only when jt_result_seq matches the seq being waited for. A late result for an old part is therefore never applied to a new part.
- Timeout of 400 ms with no result matching the seq: decide REJECT with reason vision_timeout, which is this station's safe state. A part we are unsure about goes to a person for inspection.
- Jetson heartbeat stopped for more than 300 ms: the next part is REJECTed immediately (vision_offline), without spending time on the deadline for every part.
- Auxiliary data not fresh (gateway or MCU node lost): go to DEGRADED with the conveyor at 50%, and return to RUN when the data comes back. The whole line does not stop.
The e-stop in the example is just a variable in the code, to show the order of priority. A real one must be a hardwired circuit through relays or a safety PLC, designed and verified to the machine's safety standards. It must not be a bit that ordinary software can control.
Step 3: a Jetson and an MCU node that can fail
The simulated Jetson receives a trigger and replies after a 120 ms delay. It can be set to be slow or to hang. It has a work queue, because a real Jetson may still be working on the old part when a new one arrives. A late old result therefore really does exist, and the PLC has to discard it.
tick(now: number) {
if (this.frozen) return;
if (now % 100 === 0) this.regs.jt_hb = (this.regs.jt_hb + 1) & 0xffff;
const s = this.regs.plc_trigger_seq;
if (s !== this.lastSeen) {
this.lastSeen = s;
const part = this.partOfSeq.get(s) ?? 0;
this.jobs.push({ seq: s, at: now + this.latency(now), verdict: part % 5 === 0 ? 2 : 1 });
}
while (this.jobs.length && now >= this.jobs[0].at) {
const job = this.jobs.shift()!;
this.regs.jt_result_seq = job.seq;
this.regs.jt_verdict = job.verdict;
if (job.seq !== this.regs.plc_trigger_seq) this.plc.noteLate(); // PLC ไปชิ้นถัดไปแล้ว
}
}The MCU node sends a sample every 200 ms with its own seq. If it is down, it stays silent. It does not resend the last value. Silence is the signal, so the gateway decides from the age of the latest sample.
Step 4: the gateway stores then forwards: outbox, ack and duplicate removal
The gateway assigns a seq and stores the event in the outbox before sending. It deletes the event only when it gets an ack. This makes delivery at-least-once. If the network goes down or an ack is lost, it resends later. The web side stores by id, so sending any number of times gives the same result as sending once (idempotent). This matches the difference the MQTT spec describes between QoS 1 and QoS 2. Here we chose to pay for it with duplicate removal at the destination instead of a four-step protocol.
export class Web {
seen = new Map<string, EdgeEvent>();
received = 0;
duplicates = 0;
lastRx = 0;
receive(e: EdgeEvent, now: number) {
this.received++;
if (this.seen.has(e.id)) this.duplicates++;
else this.seen.set(e.id, e);
this.lastRx = now;
}
status(now: number) { return now - this.lastRx > T.webStaleMs ? "edge_stale" : "live"; }
} private enqueue(type: EdgeEvent["type"], now: number, data: Record<string, unknown>) {
this.seq++;
this.outbox.push({ id: `edge1:${this.seq}`, seq: this.seq, ts: now, type, data });
// simplified: เต็มแล้วทิ้ง sample เก่าสุด ไม่ทิ้ง decision; production ควรเขียนลงดิสก์ (SQLite/WAL)
while (this.outbox.length > this.cap) {
const i = this.outbox.findIndex((e) => e.type === "sample");
if (i < 0) break;
this.outbox.splice(i, 1);
this.dropped++;
}
this.maxDepth = Math.max(this.maxDepth, this.outbox.length);
}
tick(now: number) {
if (now % 10 === 0) this.regs.gw_hb = (this.regs.gw_hb + 1) & 0xffff;
this.regs.gw_node_ok = now - this.lastSample <= T.nodeStaleMs ? 1 : 0;
if (now < this.nextTry) return;
for (let pass = 0; pass < 2; pass++) {
while (this.outbox.length) {
const r = this.link.send(this.outbox[0], now);
if (!r.delivered || !r.acked) { this.nextTry = now + 200; return; } // ส่งไม่ถึงหรือไม่ได้ ack: ลองใหม่ทีหลัง
this.outbox.shift();
}
// outbox ว่างแล้วค่อยบอกเว็บว่า sample ขาดไปเท่าไร (ไม่ให้ gap สะสมระหว่างล่ม)
if (this.dropped === this.reportedDrops) return;
this.enqueue("gap", now, { dropped: this.dropped - this.reportedDrops });
this.reportedDrops = this.dropped;
}
}- Send one item at a time in order, and stop immediately when the first item is not acked, so the web sees the original order
- Retry every 200 ms, not on every cycle, so we do not hammer a network that is already in bad shape
- When the buffer is full, drop the oldest sample first, and never drop a PLC decision
- After everything is sent, send a gap event telling the web how many samples are missing, so the chart shows the gap plainly instead of drawing a smooth line
A real run: seven scenarios
demo.ts runs the simulator for 6 seconds per scenario. A part arrives every 500 ms, and parts 5 and 10 are set to be defective. Each fault is injected in the stated time window. The simulated network in the simulator is set up like this:
const gw = new Gateway(regs, {
send(e) {
if (inWin(now, f.linkDown)) return { delivered: false, acked: false };
web.receive(e, now);
return { delivered: true, acked: !inWin(now, f.ackLoss) };
},
}, f.bufferCap ?? 500);== A ปกติ
ชิ้นงาน 12: PASS 9, REJECT 2 {"vision_ok":9,"vision_defect":2}
สถานะ PLC ที่เคยเป็น: RUN; ผลช้าที่ถูกทิ้ง 0
เว็บ: รับ 42 ครั้ง เก็บ 42 รายการ ซ้ำ 0; decision 11/11; เรียงลำดับ true
gateway: บัฟเฟอร์สูงสุด 1, เหลือค้าง 0, ทิ้ง sample 0; เว็บเคยเห็น edge_stale false
== B Jetson ตอบช้า 800 ms ช่วง 1000-2000
ชิ้นงาน 12: PASS 7, REJECT 4 {"vision_ok":7,"vision_timeout":2,"vision_defect":2}
สถานะ PLC ที่เคยเป็น: RUN; ผลช้าที่ถูกทิ้ง 2
เว็บ: รับ 42 ครั้ง เก็บ 42 รายการ ซ้ำ 0; decision 11/11; เรียงลำดับ true
gateway: บัฟเฟอร์สูงสุด 2, เหลือค้าง 0, ทิ้ง sample 0; เว็บเคยเห็น edge_stale false
== C Jetson ค้างทั้งตัว ช่วง 2000-3200
ชิ้นงาน 12: PASS 7, REJECT 4 {"vision_ok":7,"vision_timeout":1,"vision_offline":2,"vision_defect":1}
สถานะ PLC ที่เคยเป็น: RUN; ผลช้าที่ถูกทิ้ง 0
เว็บ: รับ 42 ครั้ง เก็บ 42 รายการ ซ้ำ 0; decision 11/11; เรียงลำดับ true
gateway: บัฟเฟอร์สูงสุด 2, เหลือค้าง 0, ทิ้ง sample 0; เว็บเคยเห็น edge_stale false
== D โหนด MCU หาย ช่วง 1500-3500
ชิ้นงาน 12: PASS 9, REJECT 2 {"vision_ok":9,"vision_defect":2}
สถานะ PLC ที่เคยเป็น: RUN, DEGRADED; ผลช้าที่ถูกทิ้ง 0
เว็บ: รับ 32 ครั้ง เก็บ 32 รายการ ซ้ำ 0; decision 11/11; เรียงลำดับ true
gateway: บัฟเฟอร์สูงสุด 1, เหลือค้าง 0, ทิ้ง sample 0; เว็บเคยเห็น edge_stale false
== E เครือข่ายเว็บล่ม 1500-3000 (ack หายก่อนหน้า 900-1500)
ชิ้นงาน 12: PASS 9, REJECT 2 {"vision_ok":9,"vision_defect":2}
สถานะ PLC ที่เคยเป็น: RUN; ผลช้าที่ถูกทิ้ง 0
เว็บ: รับ 45 ครั้ง เก็บ 42 รายการ ซ้ำ 3; decision 11/11; เรียงลำดับ true
gateway: บัฟเฟอร์สูงสุด 15, เหลือค้าง 0, ทิ้ง sample 0; เว็บเคยเห็น edge_stale false
== F e-stop 2500-3000 ขณะเครือข่ายเว็บล่ม 2000-4000
ชิ้นงาน 11: PASS 8, REJECT 1 {"vision_ok":8,"vision_defect":1}
สถานะ PLC ที่เคยเป็น: RUN, SAFE_STOP; ผลช้าที่ถูกทิ้ง 0
เว็บ: รับ 40 ครั้ง เก็บ 40 รายการ ซ้ำ 0; decision 9/9; เรียงลำดับ true
gateway: บัฟเฟอร์สูงสุด 13, เหลือค้าง 0, ทิ้ง sample 0; เว็บเคยเห็น edge_stale true
e-stop: สายพานสูงสุดระหว่างกด 0% สถานะท้ายสุด RUN
== G เครือข่ายล่มนาน 500-5500 บัฟเฟอร์จำกัด 25 รายการ
ชิ้นงาน 12: PASS 9, REJECT 2 {"vision_ok":9,"vision_defect":2}
สถานะ PLC ที่เคยเป็น: RUN; ผลช้าที่ถูกทิ้ง 0
เว็บ: รับ 32 ครั้ง เก็บ 32 รายการ ซ้ำ 0; decision 11/11; เรียงลำดับ true
gateway: บัฟเฟอร์สูงสุด 25, เหลือค้าง 0, ทิ้ง sample 11; เว็บเคยเห็น edge_stale trueReading the results scenario by scenario (the part count is one higher than the decision count because the last part is still waiting for a result when the simulation ends):
- A, normal: the 2 defective parts were REJECTed with vision_defect. The web got every event with no duplicates. The buffer never held more than 1 item.
- B, Jetson answers 800 ms late: 2 parts timed out and were REJECTed (vision_timeout). The 2 late results were dropped because their seq did not match the part being waited for. No part passed on the result of another part.
- C, Jetson hangs completely: the first part to meet the fault cost one wait until timeout. Then the heartbeat stopped, so 2 parts were REJECTed immediately (vision_offline). Normal inspection resumed when Jetson recovered.
- D, MCU node lost for 2 seconds: the PLC went to DEGRADED and then back to RUN. Part decisions did not change. The web got less data (32 items instead of 42), matching the period when the node was silent.
- E, network down, with lost acks before that: the web "received" 45 times but "stored" 42 items, with 3 duplicates blocked. The data is complete and in order. The outbox was empty after the network came back.
- F, e-stop while the network is down: the conveyor was at 0% throughout the e-stop and returned to RUN only after a reset. The network outage had nothing to do with the stop. The web saw edge_stale and got all the data when the link returned.
- G, network down for 5 seconds with a 25-item buffer: 11 samples were dropped, but all decisions were kept, 11 of 11, and the web received a gap event telling it how many were missing.
The test suite
sim.test.ts has 6 cases. Running node --test sim.test.ts gives 6 pass. It asserts nothing about real time, because the clock is virtual. Here is an example case that checks for duplicate data and gaps in a replay.
test("บัฟเฟอร์เต็ม: ทิ้งเฉพาะ sample เก็บ decision ครบ และแจ้งช่องว่างให้เว็บ", () => {
const r = run({ linkDown: [500, 5500], bufferCap: 25 });
assert.ok(r.gw.dropped > 0);
const ev = [...r.web.seen.values()];
assert.equal(ev.filter((e) => e.type === "decision").length, r.plc.decisions.length);
const gaps = ev.filter((e) => e.type === "gap").reduce((n, e) => n + Number(e.data.dropped), 0);
assert.equal(gaps, r.gw.dropped);
});Another important case is the e-stop test, which confirms that the conveyor is at 0% throughout the press even when the network is down and Jetson still answers. The suite also checks that a late Jetson result is discarded and that the seq the web stores has no gaps. If someone edits the code so that the interlock falls in priority or ids are duplicated, the test suite fails.
Taking it to real use: what the example does not do
- The outbox in the example is in memory and is lost when power fails. A real one writes to disk, for example SQLite in WAL mode, deletes on ack, and sets a maximum size based on disk space and how long the network outage must be tolerated.
- Use real protocols: MQTT (QoS 1 with Clean Session set to 0 if you want the broker to help store messages) or OPC UA for lines that need a data model and security. Between the PLC and Jetson, use Modbus TCP or a protocol the PLC supports. Read the documents for the model you use.
- Time: install NTP or PTP on every layer so ts values are comparable, and do not use ts to decide order. Use seq.
- Have the node announce a will message or an explicit offline status, instead of making the gateway guess from silence alone.
- Set timeouts from measurements, for example the 99th percentile of Jetson's response time plus a margin, not a guessed round number.
- Each safe-state decision must pass a risk analysis of the machine. This example only shows how to write it down clearly.
Hypothetical examples: applying it to three jobs
Defect inspection station on a conveyor
This matches the example: the PLC sorts and Jetson analyzes images. What you must settle with the site is where an uncertain part goes, and the part rate per second must fit the deadline for waiting on the inspection result. If there are frequent REJECTs from timeouts, look at Jetson's latency first before extending the deadline.
Storage room with several temperature nodes
Many MCU nodes send through a gateway to the web, with no Jetson. What you must define is how long a node can be gone before an alert, and data missing during a network outage must show as a gap, not as the old value. Most of this system is about the seq and gap contract. No PLC is needed if there is no control.
Production line sending counts to a web report
The PLC counts and the gateway sends to the web. The real problem is counts that are duplicated or missing after the network returns. An id per event and storing by id are the most useful parts. If the volume is high, consider aggregating into counts per minute before sending.
Common mistakes and a checklist
- Letting the web or Jetson decide to stop or run instead of the PLC
- Using the latest inspection result without checking which part it belongs to (no seq echo)
- A heartbeat that can stay stuck when the program hangs, instead of a counter that keeps changing
- Showing an old sensor value on the web page without saying how old it is
- Sending a backlog without a stable id, which causes duplicates or forces you to guess what was already sent
- An unlimited buffer that fills the disk, or one that drops important data without telling anyone when full
- Setting timeouts without measuring, and not testing recovery. Testing only when everything is normal.
- Believing that a web network failure affects production, when the design can keep them separate
- Write the ownership table before choosing devices. Every decision has a single owner.
- Every data line has seq, ts and a heartbeat that the receiving side really checks.
- Every layer has a safe state for when its neighbor goes silent, written down and tested.
- The interlock and e-stop are in circuits designed to standards and do not depend on Jetson, the MCU or the network.
- The edge has an outbox that survives power loss, deletes on ack, and the web removes duplicates by id.
- Missing data is shown as a gap, not as the old value.
- Test each fault by injecting it before handover, instead of waiting for it to happen.
Cautions about the example: it is a simulation with a virtual clock and does not measure real latency. There is no encryption or authentication on the data lines, no multiple nodes or multiple gateways, and no real safety function. The timeouts are assumed values. Use it as a learning model, not as production-ready code.
References
- OASIS — MQTT Version 3.1.1 (Keep Alive, Will Message, QoS, Clean Session, DUP flag) (accessed 2026-11-21)
- OPC Foundation — OPC Unified Architecture (accessed 2026-11-21)
- Wikipedia — IEC 61131-3 (accessed 2026-11-21)
- Wikipedia — Modbus (data model, request-response) (accessed 2026-11-21)
- Node.js — Running TypeScript natively (type stripping) (accessed 2026-11-21)
- Node.js — Test runner (node --test) (accessed 2026-11-21)




