Yapay zekâ tarafından English dilinden çevrildi ·
Firmware içinde cihaz başına token’a hiç ihtiyacınız yok. Sorunu iki parçaya ayırın.Wi-Fi: WiFiManager’ı koruyun. Dostça bir cihaz adı istiyorsanız portala bir özel alan ekleyin; aksi halde MAC adresini kullanın.Token: Düğümün kendi token’ını talep etmesine izin verin. Cihaz profilinde Provisioning sekmesini açın, stratejiyi Allow creating new devices olarak ayarlayın, kaydedin, ardından Create key seçeneğine tıklayıp secret değerini kopyalayın (yalnızca bir kez gösterilir). Bu anahtar ve secret, 12 derlemenin hepsinde aynıdır. İlk açılışta düğüm, kullanıcı adı provision ve boş parola ile bağlanır, v1/provision/res konusuna abone olur ve isteğini yayınlar. Yanıt kendi erişim token’ını taşır; bunu NVS’e kaydedersiniz ve bundan sonra normal şekilde giriş yapar.
// 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
Bir eczane tesisinde iki sorunla karşılaştık. Bu yolla oluşturulan yeni cihazlar karantinaya düşer; bağlanırlar ama bir yönetici etkinleştirene kadar verileri atılır, bu yüzden kuruluşun sahibine onları aramasını söyleyin. Ayrıca ad benzersiz olmalıdır; aynı adla ikinci bir istek device_exists hatası alır, çünkü sistem aynı kimlik bilgisini iki kez vermez. Bu nedenle NVS’i gelişigüzel silmeyin. Ayrıntılar Adding and claiming devices sayfasında.
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.

