Von English mit KI übersetzt ·
Du brauchst im Firmware-Code überhaupt keine Tokens pro Gerät. Teile das Problem in zwei Teile.WLAN: Behalte WiFiManager bei. Wenn du einen gut lesbaren Gerätenamen möchtest, füge im Portal ein benutzerdefiniertes Feld hinzu, andernfalls verwende die MAC.Token: Lass den Knoten sein eigenes anfordern. Öffne im Geräteprofil den Reiter Provisioning, setze die Strategie auf Allow creating new devices, speichere, klicke dann auf Create key und kopiere das Secret (es wird nur einmal angezeigt). Dieser Schlüssel mit Secret ist für alle 12 Builds gleich. Beim ersten Start verbindet sich der Knoten mit dem Benutzernamen provision und einem leeren Passwort, abonniert v1/provision/res und veröffentlicht seine Anfrage. Die Antwort enthält sein Zugriffstoken; du speicherst es im NVS, und ab dann meldet sich der Knoten normal an.
// first boot only, after Wi-Fi is up
mqtt.setServer(host, 8883); // WiFiClientSecure, TLS
mqtt.connect(clientId, "provision", "");
mqtt.subscribe("v1/provision/res", 1);
String req = String("{"deviceName":"aq-") + mac6 +
"","provisionDeviceKey":"pk_xxx","provisionDeviceSecret":"ps_xxx"}";
mqtt.publish("v1/provision/req", req.c_str());
// in the callback: parse credentialsValue when status == "SUCCESS",
// prefs.putString("tok", value); then reconnect with the token as username
Zwei Dinge haben uns auf einer Pharma-Anlage erwischt. Neue Geräte, die so angelegt werden, landen in Quarantäne: Sie verbinden sich, aber ihre Daten werden verworfen, bis ein Administrator sie aktiviert. Sag deshalb dem Verantwortlichen der Organisation, dass er nach ihnen suchen soll. Außerdem muss der Name eindeutig sein; eine zweite Anfrage mit demselben Namen erhält device_exists, weil nie zweimal eine Zugangsdaten ausgegeben wird. Lösche den NVS deshalb nicht leichtfertig. Details stehen unter Adding and claiming devices.
You don’t need per-device tokens in the firmware at all. Split it in two problems.
Wi-Fi: keep WiFiManager. Add one custom field in the portal if you want a friendly device name, otherwise use the MAC.
Token: let the node ask for its own. On the device profile open the Provisioning tab, set the strategy to Allow creating new devices, save, then Create key and copy the secret (it’s shown once). That key + secret is the same for all 12 builds. On first boot the node connects with username provision and an empty password, subscribes to v1/provision/res and publishes its request. The answer carries its access token, you store that in NVS and from then on it logs in normally.
// first boot only, after Wi-Fi is up
mqtt.setServer(host, 8883); // WiFiClientSecure, TLS
mqtt.connect(clientId, "provision", "");
mqtt.subscribe("v1/provision/res", 1);
String req = String("{"deviceName":"aq-") + mac6 +
"","provisionDeviceKey":"pk_xxx","provisionDeviceSecret":"ps_xxx"}";
mqtt.publish("v1/provision/req", req.c_str());
// in the callback: parse credentialsValue when status == "SUCCESS",
// prefs.putString("tok", value); then reconnect with the token as username
Two things bit us on a pharma site. New devices created this way land in quarantine, they connect but their data is dropped until an admin activates them, so tell whoever owns the org to look for them. And the name must be unique, a second request with the same name gets device_exists because it never hands out a credential twice. So never wipe NVS casually. Details are on Adding and claiming devices.

