Traduit de English par IA ·
Vous n’avez pas du tout besoin de tokens par appareil dans le firmware. Séparez le problème en deux.Wi-Fi : conservez WiFiManager. Ajoutez un champ personnalisé dans le portail si vous voulez un nom d’appareil convivial, sinon utilisez l’adresse MAC.Token : laissez le nœud demander le sien. Dans le profil de l’appareil, ouvrez l’onglet Provisioning, réglez la stratégie sur Allow creating new devices, enregistrez, puis cliquez sur Create key et copiez le secret (il n’est affiché qu’une seule fois). Cette clé et ce secret sont identiques pour les 12 builds. Au premier démarrage, le nœud se connecte avec le nom d’utilisateur provision et un mot de passe vide, s’abonne à v1/provision/res et publie sa demande. La réponse contient son token d’accès ; vous le stockez dans la NVS, et ensuite il se connecte normalement.
// 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
Deux points nous ont posé problème sur un site pharmaceutique. Les nouveaux appareils créés de cette façon atterrissent en quarantaine : ils se connectent, mais leurs données sont rejetées tant qu’un administrateur ne les a pas activés. Prévenez donc le responsable de l’organisation de les chercher. Et le nom doit être unique : une seconde demande avec le même nom renvoie device_exists, car elle ne délivre jamais deux fois un identifiant. Ne effacez donc pas la NVS à la légère. Les détails figurent sur 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.

