Knowledge Hub

Factora Academy

Knowledge Hub · Courses · IoT.Factora · Technical · 40 min

Factora IoT technology setup

Architecture, data path, MQTT, codecs, Compose, Caddy, security, and troubleshooting for engineers.

Introduction

What this covers

The engineering view of Factora IoT: hosts, containers, the data path from radio to dashboard, and the settings an integrator touches.

Why it matters on the floor

An engineer who does not know which container or host owns a step will restart the wrong service when a reading stops.

By the end you can

  • Name the three public hosts and what each one serves
  • Follow one reading from the gateway through MQTT and the database to a dashboard
  • Find the setting to change without putting secrets into a lesson or a ticket

About 40 minutes. Prerequisites: The Factora IoT Infrastructure Guide, for the plain-language map of the four layers.

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.com is the plant: floor, alerts, devices, connectivity, settings.
  • admin.factoraiot.com is the ops console for platform staff.
  • factoraacademy.factoraerp.com is 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
  1. The sensor joins with OTAA: DevEUI, JoinEUI, and AppKey. The AppKey never travels in the uplink. Session keys encrypt and sign the radio frame.
  2. The gateway is a relay. It adds time, RSSI, and SNR.
  3. Basics Station is a WebSocket inside TLS. The gateway dials out. The old unencrypted UDP forwarder is not used, and its port stays closed.
  4. The gateway bridge publishes gateway events to Mosquitto and delivers downlinks back.
  5. ChirpStack checks the integrity code and the frame counter, removes duplicates, decrypts, and runs the device-profile codec. Decoded uplinks are JSON.
  6. startMqttConnector in src/lib/connector-runtime.ts connects when the connector is enabled and the worker is the process that runs background loops. It subscribes to config.topics, or to each mapping's external id when the topic list is empty. The default QoS is 1.
  7. onMqttMessage finds the first mapping whose external id matches the topic (topicMatches in src/lib/telemetry.ts), reads a number at payloadPath, and calls ingestFromMapping.
  8. 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.
  9. 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.
  10. 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/ingest with 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.

IdeaIn this platform
BrokerMosquitto for LoRaWAN traffic. A second, built-in broker for direct device telemetry.
TopicSlash-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 0At most once. Used for gateway traffic that never leaves the machine.
QoS 1At least once. Factora connectors default to QoS 1. A retry can deliver the same payload twice, so ingest ignores a duplicate message id.
QoS 2Exactly once. The client library allows it. We do not use it.
RetainedA 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.
ACLMosquitto 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 payloadPath as value. ChirpStack's decoded fields are under object, so a mapping created only from that form will not see object.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 id or messageId. ChirpStack sends deduplicationId. A QoS 1 retry can therefore be stored again when the timestamp also differs. The event time field the worker reads is timestamp. ChirpStack sends time, so the stored time is the time the worker received the message unless something else adds timestamp.

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):

ServiceRole
dbPostgreSQL 16. Not published to the internet.
migrateprisma migrate deploy, then it exits. The app waits until this succeeds.
appNext.js on port 3000, internal. Health check is /api/health.
workerSame image, scripts/worker.ts. Shares the app network, so loopback is private to both. Heartbeat file must be fresh.
caddyPorts 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.

HostBehaviour
iot.factoraerp.comPlant app
factoraacademy.factoraerp.comAcademy
admin.factoraiot.comOps 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.comChirpStack 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. .env files 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.com port 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 is mqtt://mosquitto:1883 from 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 mapping and 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 object is 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 payloadPath does not land on a number. object.temperature is a number. object itself is not.
  • The Connect form stored value as 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 id or messageId, so ChirpStack's deduplicationId is not used as the duplicate key.

Connector errors

What you seeWhat to check
Broker URL requiredThe connector config has no brokerUrl or host.
status connecting, then disconnected, last error Connection lostBroker name, port, username, password, and that the worker can resolve mosquitto. Watch the reconnect delay.
status error on saveThat connector failed on its own. Other connectors keep running. Read lastError.
Backpressure: ingest queue fullMore than 250 messages are in flight for this connector. Mappings or the database are too slow. The count of rejected messages climbs.
No device mappingThe topic arrived and no external id matched. Claim it or add a mapping.
Password rejected by MosquittoThe 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 productionMQTT_URL for the built-in broker must be mqtts:// or wss://. The internal LoRaWAN broker stays mqtt://mosquitto:1883.

Create a free account or log in to save this lesson on your progress.

Check understanding

Progress is stored in this browser until you log in. Passing is 70%.

1. Which file subscribes the worker to an MQTT connector?
2. The pattern application/7/device/+/event/up matches which topics?
3. Factora's default connector QoS is
4. A ChirpStack codec result of object.temperature is read by Factora when payloadPath is
5. In production, an external MQTT URL used by the built-in broker must start with
6. A connected connector with last error No device mapping means