How-to
Configuring MQTT parking-gate counting on ctrlX CORE
Short answer
Commission the camera-to-broker-to-counter chain hop by hop: ping both ways, open TCP to the broker on port 8883, read the CONNACK return code, and confirm a wildcard subscriber sees the camera publish. Retain only state topics such as occupancy and status, never event topics, and carry an event ID because QoS 1 delivers at least once.

On this page
- Which hops carry a vehicle event from the camera to the parking app?
- Which port does MQTT use between the camera and the ctrlX CORE broker?
- What does each CONNACK return code tell you before you change anything?
- Which client settings keep the camera and Node-RED sessions alive?
- How do you prove the camera publish reaches Node-RED?
- Which topics get the retain flag in a parking counter?
- How many bytes does a counted car cost at QoS 1?
- What do you commission, and in what order?
- Questions asked
Count vehicles on ctrlX CORE by commissioning the chain in four checks before you write any counter logic: ping the camera and controller both ways, open a TCP session to the broker port, read the CONNACK return code, and watch a wildcard subscriber receive the camera's publish. Production runs MQTT over TLS on port 8883; the plain port stays on the bench. Retain only state topics, never events, and put an event ID in every payload because QoS 1 delivers at least once. The order below turns a camera, a Mosquitto broker app and Node-RED on one ctrlX CORE, for example a CORE X3, into an occupancy counter a parking app can trust.
Which hops carry a vehicle event from the camera to the parking app? #
A vehicle event crosses eight hops on its way from the camera's analytics engine to the parking app, and hops 3 through 7 are the ones you own on the ctrlX CORE, so prove each one with a single check before writing any payload logic.
| Hop | Stops when | First check |
|---|---|---|
| 1. Camera analytics to camera Ethernet port | Vision task or licence not active | Camera shows a detection event locally |
| 2. Camera to switch to ctrlX CORE interface | No link, wrong VLAN, wrong subnet | Link LED, then ping both directions |
| 3. TCP session to the broker | Port closed, firewall, wrong address | TCP connect to the broker port |
| 4. MQTT CONNECT and CONNACK | Bad credentials, duplicate client ID, TLS mismatch | CONNACK return code |
| 5. PUBLISH from camera to topic | Wrong topic, ACL denies publish | Wildcard subscriber sees it |
| 6. Node-RED subscription | Topic filter does not match | Debug node prints payload |
| 7. Node-RED PUBLISH to the app topic | Counter logic, retain or QoS not set | Second subscriber sees the count |
| 8. Parking app subscription | App points at wrong broker or topic | App displays the retained value |
MQTT suits this path because each event is a few dozen bytes carried over one persistent TCP session instead of polling. Lighting on DALI and free-space sensors on field protocols reach the same controller through ctrlX IO; they do not ride this MQTT path unless you bridge them into Node-RED yourself. Note that the camera software, not the controller, defines what the vision task can publish, so confirm its topic and payload format before planning hops 5 to 7.
Before continuing, write the broker address, port, client IDs and topic names for hops 3 to 8 on one sheet. Every later check refers to it.
Which port does MQTT use between the camera and the ctrlX CORE broker? #
Production traffic uses port 8883, the registered MQTT-over-TLS port, while port 1883, the registered plain-MQTT port, belongs only on an isolated bench segment; from a laptop on the camera's subnet, a TCP connect to the broker address on 8883 must succeed before you configure any client.
Prove layer 1 and 2 first: link and speed on the camera port and the controller port, both ends in the same VLAN or routed deliberately, and the camera on a static address or DHCP reservation, because broker ACLs and firewall rules refer to that address. Then test the TCP hop from a Windows engineering laptop on the camera subnet, with 192.168.10.10 as the example broker address:
Test-NetConnection -ComputerName 192.168.10.10 -Port 8883
The result must show TcpTestSucceeded : True on the port you will actually use. On a Linux bench, nc -zv 192.168.10.10 8883 proves the same thing.
One safety note: never expose the plain listener with real camera credentials on the plant network, because MQTT carries the password in clear; open the unencrypted port only on an isolated bench segment, and move to TLS before the camera goes live. A plain client pointed at the TLS listener, or the reverse, dies before any CONNACK — the article on Node-RED MQTT failing when the broker rejects a plain connection walks through that signature.
What does each CONNACK return code tell you before you change anything? #
Read the CONNACK return code before touching any setting: 1 means unacceptable protocol version, 2 identifier rejected, 3 server unavailable, 4 bad user name or password, and 5 not authorized, and each code names exactly one fix instead of five guesses.
| CONNACK code | Meaning | Fix |
|---|---|---|
| 1 | Unacceptable protocol version | Set the same MQTT version on client and broker |
| 2 | Identifier rejected | Correct or shorten the client ID |
| 3 | Server unavailable | Broker app not running; recheck the TCP hop |
| 4 | Bad user name or password | Re-enter the credentials on the client |
| 5 | Not authorized | Fix the account or the ACL on the broker |
Codes are defined in MQTT 3.1.1, and the specification's intent for code 1 is worth knowing: when the client presents an unsupported protocol level, the broker should return code 1 rather than close silently. A documented Mosquitto issue records that some builds instead drop the TCP session without sending any CONNACK for a bad protocol level, so on this broker family a connect that succeeds at TCP and then closes with no CONNACK means a TLS mismatch, a plain client on the TLS port, or a protocol-version mismatch — three causes, one symptom. The article on finding the failing hop when TLS connects but no CONNACK arrives covers that diagnosis step by step.
Your pass condition for this section: a real client receives a CONNACK, and you have recorded its code, even when the code is a failure code. The code is the diagnosis.
Which client settings keep the camera and Node-RED sessions alive? #
Four client settings decide whether the camera and Node-RED stay connected: a client ID unique to each client, a keep-alive the broker enforces at about 1.5 times its value, a retained Last Will on a status topic, and clean session disabled on the QoS 1 subscriber.
| Setting | Value to choose | Why |
|---|---|---|
| Client ID | Unique per client, for example camera-entry-gate1, nodered-counter, parking-app | A second connection with the same ID makes the broker drop the first, producing a reconnect loop |
| Keep-alive | Short enough to detect a dead link within your alarm time | The broker declares the client gone after about 1.5 times this interval |
| Last Will | Retained offline payload on a status topic | Management sees the camera or controller as down without polling it |
| Online message | Retained online payload published after each CONNACK | Overwrites the will after every reconnect |
| Clean session | Disabled on the Node-RED subscriber when you use QoS 1 | The broker queues messages while Node-RED restarts |
These are protocol-level settings, so the field names differ per product, but every MQTT client exposes them. Clean session deserves its own check: with it disabled and QoS 1 subscriptions in place, the broker queues messages for the subscription while Node-RED restarts; with it enabled, everything published during a restart is gone. If your counter loses events exactly while the controller updates apps, the article on a subscriber that stops receiving after a broker restart explains that mechanism.
Check before continuing: a laptop subscriber stays connected through several keep-alive periods without a reconnect loop in the broker log.
How do you prove the camera publish reaches Node-RED? #
A laptop subscriber with the wildcard filter garage/# that prints the camera's event payloads proves the TCP session, the broker session and the publish path in one view, and it should stay connected through several keep-alive intervals so an intermittent drop cannot hide.
Run it from any machine that can reach the broker, again with the example address and a named broker account:
mosquitto_sub -h 192.168.10.10 -p 8883 --cafile ca.crt -u gate-viewer -t "garage/#" -v
Add the password option for that account when the broker requires one. Each line the tool prints shows the topic and the payload exactly as the broker delivered them. If the camera detects a vehicle on its local view but nothing appears here, the fault is in the camera's broker configuration — wrong address, wrong topic, an ACL that denies the publish, or a TLS certificate problem. If events appear here but not in the Node-RED debug node, the fault is in the subscription topic filter, typically a level mismatch such as subscribing to garage/p1 when the camera publishes garage/p1/entry/event.
Check before continuing: every camera detection produces one line in the wildcard subscriber and one message in the Node-RED debug node.
Which topics get the retain flag in a parking counter? #
Retain only state topics — the occupancy count and the online status carried by the Last Will — and never the entry or exit event topics, so a freshly connected app receives the current free-space value immediately instead of replaying an old vehicle.
| Topic (illustrative) | Publisher | Retain | QoS |
|---|---|---|---|
| garage/p1/entry/event | Camera | No | 1 |
| garage/p1/exit/event | Camera | No | 1 |
| garage/p1/occupancy | Node-RED | Yes | 1 |
| garage/p1/status | Node-RED, with Last Will | Yes | 1 |
A retained event topic is a real bug, not a style choice: every reconnecting subscriber replays the last vehicle event as if it just happened, and a cold app shows a car that left hours ago. To clear a wrong retained value, publish a zero-length payload with the retain flag to the same topic.
Delivery at QoS 1 is at-least-once, so after a lost acknowledgement the broker can deliver the same event twice. No QoS level fixes a camera that re-reads the same plate and publishes a second message, because that is a new publish. QoS 2 removes duplication within one hop and session, but it costs extra round trips per event, so QoS 1 with an event ID is the right trade here. Note also that the effective QoS toward a subscriber is the lower of the publish QoS and the subscription QoS, so a QoS 0 subscription silently downgrades the chain.
How many bytes does a counted car cost at QoS 1? #
One counted car costs about 50 bytes at QoS 1, made up of a 46-byte PUBLISH and a 4-byte PUBACK, so a garage with 40 entries and 40 exits per hour moves about 4,000 bytes per hour across the broker.
Worked example with an illustrative topic and payload. Topic garage/p1/entry/count is 21 bytes; payload {"in":1,"free":112} is 19 bytes. The remaining length is the 2-byte topic length field plus 21 plus 19, which is 42 bytes — under 128, so the variable-length encoding takes one byte. The QoS 0 PUBLISH is therefore 1 fixed-header byte plus 1 length byte plus 42, which is 44 bytes. QoS 1 adds a 2-byte packet identifier, giving 46 bytes, and the broker's PUBACK is 4 bytes, so each confirmed event is 50 bytes.
At 80 events per hour that is 80 times 50, or 4,000 bytes per hour, which averages 4,000 divided by 3,600, about 1.1 bytes per second. Even a garage ten times busier stays in the tens of kilobytes per hour — the protocol overhead, not the payload size, is never your bandwidth problem on a plant network. What consumes the link instead is a misconfigured publisher retrying a failed session, which is why the session settings above come before any byte counting.
What do you commission, and in what order? #
Commission in a fixed order — address, ping, TCP, CONNACK, wildcard subscribe, retained counter, second subscriber, app — because a fault that surfaces at the counter step almost always lives two steps earlier at the link or the session.
- Give the camera a static address or DHCP reservation, and confirm the vision task publishes its detection locally.
- Ping the camera from the ctrlX CORE interface and the controller from the camera subnet, both directions.
- Open a TCP session to the broker on 8883 from the camera subnet and record
TcpTestSucceeded : True. - Connect a client with its unique client ID and read the CONNACK code; fix exactly what that code names.
- Subscribe with the wildcard filter on a laptop and confirm every detection produces one topic-and-payload line.
- Build the counter in Node-RED and publish the occupancy and status topics with the retain flag set.
- Connect a second subscriber cold and confirm it receives the retained occupancy immediately, not an old event.
- Point the parking app at the broker and topic, then reconcile the displayed free-space value against the cars on site.
Each step's check must pass before the next begins. When step 6's counter misbehaves, walk back to step 3 and 4 first — in a chain commissioned this way, the payload logic is the last thing that is ever wrong.
Questions asked
Can the broker and Node-RED run on the same ctrlX CORE?
Yes. The ctrlX AUTOMATION app listing provides a Mosquitto broker app for the ctrlX CORE alongside the Node-RED app, and both run as apps on one controller such as a CORE X3. Give every client its own client ID, and if you bench-test on the plain listener, keep that port on an isolated segment away from the plant network.
Why does the camera reconnect every few minutes?
Almost always a duplicate client ID. When a second connection presents the same client ID, the broker drops the first session, so the camera reconnects, collides again and loops. Give the camera, the Node-RED broker config and the parking app distinct identifiers such as camera-entry-gate1, nodered-counter and parking-app.
How do I clear a wrong retained occupancy value?
Publish a zero-length payload with the retain flag set to the same topic. The broker stores the empty message, overwrites the wrong value and sends it to new subscribers, so the app shows an empty state until the counter publishes the next real count.
Which MQTT protocol version should the camera and the broker share?
Pick one version and set it identically on both ends. An unsupported protocol level should draw CONNACK code 1, unacceptable protocol version, but a documented Mosquitto issue shows that some broker builds close the TCP session without any CONNACK instead, so treat a silent close as a version or TLS mismatch too.
Is QoS 2 better than QoS 1 for car counts?
QoS 2 removes duplicate delivery within one hop and session, but it cannot stop a camera that re-reads the same plate and publishes a new message anyway. Carry an event ID in the payload and drop repeats in the counter; QoS 1 with deduplication costs fewer bytes and round trips.


