Factora IoT technology setup
This lesson is for engineers and integrators. It follows one reading from the radio to a dashboard, then the settings you will touch. The plain-language map is the Factora IoT Infrastructure Guide. The click-by-click factory checklist is How to set up a new factory.
No hardware brand is assumed. Do not put passwords, keys, or network addresses into a lesson, a ticket, or a chat.
Architecture
Factora IoT is one Next.js application with three public host names:
iot.factoraerp.comis the plant: floor, alerts, devices, connectivity, settings.admin.factoraiot.comis the ops console for platform staff.factoraacademy.factoraerp.comis this academy.
The app container serves pages and APIs on port 3000 inside Docker. Caddy is the only process that publishes web ports. A second container, the worker (FACTORA_ROLE=worker, scripts/worker.ts), does not serve pages. It holds MQTT connections, runs connectors, and writes the database.
PostgreSQL is reached through Prisma (prisma/schema.prisma). A Company is one factory. Areas, production lines, devices, readings, alert rules, users, connectors, and mappings all carry that companyId. Plant queries are filtered by the signed-in factory. A paused factory rejects a plant login. An ops owner can still enter it.
The LoRaWAN stack is a separate Compose project. Restarting it does not restart Factora, and the reverse. ChirpStack has its own PostgreSQL database and a Redis store. Factora does not use Redis.
Data path
sensor
-> LoRa (EU868)
-> gateway
-> Basics Station TLS, wss://lns.factoraiot.com:3001
-> ChirpStack v4 (EU868)
-> Mosquitto
-> topic application/<application id>/device/<DevEUI>/event/up
-> Factora worker MQTT connector (src/lib/connector-runtime.ts)
-> PostgreSQL via Prisma
-> alert rules and dashboards
- The sensor joins with OTAA: DevEUI, JoinEUI, and AppKey. The AppKey never travels in the uplink. Session keys encrypt and sign the radio frame.
- The gateway is a relay. It adds time, RSSI, and SNR.
- Basics Station is a WebSocket inside TLS. The gateway dials out. The old unencrypted UDP forwarder is not used, and its port stays closed.
- The gateway bridge publishes gateway events to Mosquitto and delivers downlinks back.
- ChirpStack checks the integrity code and the frame counter, removes duplicates, decrypts, and runs the device-profile codec. Decoded uplinks are JSON.
startMqttConnectorinsrc/lib/connector-runtime.tsconnects when the connector is enabled and the worker is the process that runs background loops. It subscribes toconfig.topics, or to each mapping's external id when the topic list is empty. The default QoS is 1.onMqttMessagefinds the first mapping whose external id matches the topic (topicMatchesinsrc/lib/telemetry.ts), reads a number atpayloadPath, and callsingestFromMapping.- Ingest writes a Reading (value, unit, device, company, time, source, quality), updates last value and last seen, and skips a repeat when the message id or the same timestamp was already stored. If PostgreSQL is unreachable, the reading can sit in the edge buffer and replay later.
- Alert rules for that device run on the new value. The worker maintenance loop runs about every 5 seconds. A separate offline scan runs about every 20 seconds for devices that have gone quiet.
- The Next.js app reads those rows for the floor, alerts, and dashboards. It does not invent a reading.
Two other doors exist for equipment that is not LoRaWAN:
- HTTP
POST /api/ingestwith the device ingest token. - The built-in MQTT broker inside the worker, bound to the server loopback address on port 1883. The username is the device code and the password is the ingest token. The only allowed publish topic is
factora/<factory slug>/devices/<device code>/telemetry, and the payload must be 16 KB or smaller.
The connector for the first LoRaWAN factory should use the internal broker URL mqtt://mosquitto:1883. That address is only on the Docker network. It is not open on the internet.
MQTT
MQTT is publish and subscribe. A client connects to a broker, publishes a payload to a topic, and the broker copies it to every client that subscribed to that topic. The publisher does not need to know the subscribers.
| Idea | In this platform |
|---|---|
| Broker | Mosquitto for LoRaWAN traffic. A second, built-in broker for direct device telemetry. |
| Topic | Slash-separated path. Application uplinks use application/<application id>/device/<DevEUI>/event/up. |
+ | One level. application/7/device/+/event/up is every device in application 7. |
# | The rest of the topic. application/7/# is everything under that application. # only matches when it is the last level in the pattern our matcher uses. |
| QoS 0 | At most once. Used for gateway traffic that never leaves the machine. |
| QoS 1 | At least once. Factora connectors default to QoS 1. A retry can deliver the same payload twice, so ingest ignores a duplicate message id. |
| QoS 2 | Exactly once. The client library allows it. We do not use it. |
| Retained | A retained message is the broker's last copy of a topic, delivered to a new subscriber. ChirpStack uplinks are live events, not retained configuration. The worker stores whatever arrives. It does not publish a retained reading of its own. |
| ACL | Mosquitto users are limited to topics. The gateway bridge may use eu868/gateway/#. A factory user may read only application/<that factory's application id>/# and may not publish. |
topicMatches compares a pattern to a topic level by level. + is one level. # matches from that level to the end. A pattern without a wildcard must be the same length as the topic.
Reconnect waits step through 5, 10, 30, and 60 seconds. Each connector keeps at most 250 messages in flight. Past that, the message is counted as rejected and the last error is backpressure. One connector failing does not stop the others.
Payload codecs
A codec turns the sensor's raw bytes into JSON. That program lives on the ChirpStack device profile, named for the measurement (temperature, humidity, energy, level), not for a manufacturer. ChirpStack then publishes the decoded object on the uplink event.
Factora does not run that codec. The mapping transform has payloadPath, and extractNumeric walks that dotted path looking for a finite number. A temperature uplink is usually under object.temperature, so the path is object.temperature. Scale and offset run after that.
Two limits in the current code matter:
- The Connect form always saves
payloadPathasvalue. ChirpStack's decoded fields are underobject, so a mapping created only from that form will not seeobject.temperature. The API accepts another path. The screen does not ask for one. See the factory lesson. - The worker reads a message id from JSON fields
idormessageId. ChirpStack sendsdeduplicationId. A QoS 1 retry can therefore be stored again when the timestamp also differs. The event time field the worker reads istimestamp. ChirpStack sendstime, so the stored time is the time the worker received the message unless something else addstimestamp.
One matching mapping wins. Two mappings on the same topic do not split one uplink into two Factora devices. Create one Factora device for the measurement you will chart, and point the path at that field.
Docker Compose
Factora production (docker-compose.prod.yml):
| Service | Role |
|---|---|
| db | PostgreSQL 16. Not published to the internet. |
| migrate | prisma migrate deploy, then it exits. The app waits until this succeeds. |
| app | Next.js on port 3000, internal. Health check is /api/health. |
| worker | Same image, scripts/worker.ts. Shares the app network, so loopback is private to both. Heartbeat file must be fresh. |
| caddy | Ports 80 and 443. |
The LoRaWAN project is separate. Its services are ChirpStack, the Basics Station gateway bridge on port 3001, Mosquitto, PostgreSQL, and Redis. Mosquitto joins the Factora network so the worker can use the name mosquitto. Redis and the ChirpStack database stay on the private LoRaWAN network.
Secrets are read from a .env file beside each Compose file. They are not committed. Container logs rotate. App and worker drop to an unprivileged user after fixing data-folder ownership.
Caddy and HTTPS
Caddy terminates HTTPS, redirects plain HTTP for the named hosts, and sends the request to the app. Security headers include HSTS, nosniff, no framing, a strict referrer policy, and camera, microphone, and location disabled.
| Host | Behaviour |
|---|---|
| iot.factoraerp.com | Plant app |
| factoraacademy.factoraerp.com | Academy |
| admin.factoraiot.com | Ops console. Open on the internet; sign-in needs a password and an authenticator code. OPSCADDYALLOWLIST and OPSIPALLOWLIST can still narrow it to fixed networks. |
| lns.factoraiot.com | ChirpStack admin UI, on its own allowlist. Gateways do not use this web port. |
Port 3001 is the gateway bridge, not Caddy. Visits to an unknown host are refused. Certificates come from Let's Encrypt and renew on their own. A daily job copies the lns.factoraiot.com certificate to the gateway bridge when it changes.
Inside one machine, Factora to Mosquitto stays on the Docker network as mqtt://. The built-in broker refuses an external MQTT_URL in production unless the URL starts with mqtts:// or wss:// (src/lib/mqtt.ts).
Security model
- MFA. Ops login requires a time-based authenticator code (
OPSREQUIRE2FA). Each factory has Require MFA. A platform switch can force it for every factory. Secrets are stored encrypted. Recovery codes are hashed. A code is not accepted twice in the same time step. Password hashes are scrypt. - Roles. Plant roles are admin, maintenance, and operator. Admin manages the team, devices, connectors, and settings. Maintenance can map devices and handle work orders. Operators acknowledge alerts and run the floor. Platform roles are owner and viewer. Only an owner creates a factory or enters it.
- Allowlists. The ops console and the ChirpStack admin each have an allowlist. Changing one does not change the other. Port 3001 is not limited by those web allowlists, because gateways are not browsers.
- Secrets.
.envfiles stay on the server, mode restricted to the administrator, and out of Git and out of the backup bucket. Connector passwords and certificates are encrypted in the database and removed from API responses and logs. Device ingest tokens are shown when they are created or rotated, then stored as a hash. - Least privilege. The app and worker do not keep running as root. Poll URLs must pass a host allowlist so a connector cannot be aimed at an internal address.
Troubleshooting
Work from the outside in: radio, gateway, ChirpStack, Mosquitto, connector, mapping, database, screen.
Gateway offline
- In ChirpStack, the gateway's last seen time is old or the Basics Station connection is down.
- The gateway can reach
lns.factoraiot.comport 3001 outbound. The factory firewall does not need an inbound rule. - The gateway id in ChirpStack matches the id in the gateway. The token or client certificate matches the one registered for that gateway.
- The bridge is running and its TLS certificate is the current one for
lns.factoraiot.com. - Ethernet or the mobile backhaul has a route to the internet. Power and the antenna are seated.
- In Factora, Connect shows a separate gateway row. That row goes stale about 90 seconds after the last heartbeat, and offline after about three times that. A stale Factora heartbeat does not, by itself, mark every sensor offline. A ChirpStack gateway can be up while this Factora row was never heartbeated, because the two registries are not linked yet.
No uplinks
- The sensor has joined (OTAA). DevEUI, JoinEUI, and AppKey match the label, and the device is enabled in the right tenant.
- The device profile region is EU868, the same region as the gateway bridge.
- ChirpStack shows live frames for that DevEUI. If frames exist and the codec object is empty, this is a codec problem, not a radio problem.
- Mosquitto shows the application uplink topic. The factory user is allowed to read
application/<application id>/#. - The Factora connector is enabled, status is
connected, and the broker URL ismqtt://mosquitto:1883from the worker network. - The subscribed topic matches
application/<application id>/device/+/event/up, including the real application id. - A mapping exists. Otherwise the connector last error is
No device mappingand the topic appears under Discovered. - The worker container is running and its heartbeat is fresh. Connectors start only in the worker.
- The factory is not paused. Live mode is on when you expect real readings rather than the demo simulator.
Decoding errors
- ChirpStack codec log or the uplink
objectis empty or an error. Fix the codec on the device profile. Factora never sees the raw radio bytes. - Connector or ingest reports a malformed payload or a missing numeric value. The body is not JSON, or
payloadPathdoes not land on a number.object.temperatureis a number.objectitself is not. - The Connect form stored
valueas the path. Set the path that matches the codec. That control is not on the form today. - Units disagree (a power in kW mapped onto an energy device in kWh). The reading may store and still be the wrong quantity.
- Duplicate rows after a reconnect: the payload has no
idormessageId, so ChirpStack'sdeduplicationIdis not used as the duplicate key.
Connector errors
| What you see | What to check |
|---|---|
Broker URL required | The connector config has no brokerUrl or host. |
status connecting, then disconnected, last error Connection lost | Broker name, port, username, password, and that the worker can resolve mosquitto. Watch the reconnect delay. |
status error on save | That connector failed on its own. Other connectors keep running. Read lastError. |
Backpressure: ingest queue full | More than 250 messages are in flight for this connector. Mappings or the database are too slow. The count of rejected messages climbs. |
No device mapping | The topic arrived and no external id matched. Claim it or add a mapping. |
| Password rejected by Mosquitto | The Mosquitto user and ACL were reloaded, and the password stored on the connector is the current one. The screen never shows the stored secret again. |
| External broker refused in production | MQTT_URL for the built-in broker must be mqtts:// or wss://. The internal LoRaWAN broker stays mqtt://mosquitto:1883. |
