Device Binding [Required]
About 372 wordsAbout 1 min
2024-11-7
Before connecting a device to the Open Platform, you must bind the device first. Otherwise, the platform will actively disconnect it.
Reference Code
The following is a basic sample. In real projects, modify it based on your own workflow. For example, official firmware binds devices using data submitted from the provisioning page.
#include <esp-ai.h>
ESP_AI esp_ai;
void setup() {
Serial.begin(115200);
// [Required] Enable debug mode for more logs
bool debug = true;
// Wi-Fi config: { wifi name, wifi password }
// If omitted, provisioning page is required. If provided, Wi-Fi connects automatically.
ESP_AI_wifi_config wifi_config = { "MyWiFi", "12345678", "ESP-AI" };
// Server config for Open Platform: usually left empty and set on provisioning page.
// ESP_AI_server_config server_config = { };
// Or set API key directly to skip entering it on provisioning page.
// Note: device binding is still required.
ESP_AI_server_config server_config = {"http", "node.espai.fun", 80, "api_key=your_open_platform_api_key"};
// Or use your own deployed server:
// ESP_AI_server_config server_config = { "http", "192.168.1.5", 8088, "p1=111&p2=test" };
// Offline wake-up scheme: { scheme, threshold }
ESP_AI_wake_up_config wake_up_config = { "pin_high", 1, 10 };
// Make sure API key exists in local data; some features depend on it (e.g. alarm)
esp_ai.setLocalData("api_key", "your_open_platform_api_key");
// Start ESP-AI
esp_ai.begin({debug, wifi_config, server_config, wake_up_config });
// Simulate binding. In production this should be triggered from provisioning page flow.
String binded = esp_ai.getLocalData("binded");
if(binded != "1"){
on_bind_device();
}
}
void loop() {
esp_ai.loop();
}
// Device binding request example
HTTPClient on_bind_device_http;
void on_bind_device()
{
on_bind_device_http.begin("http://api.espai2.fun/devices/add");
on_bind_device_http.addHeader("Content-Type", "application/json");
JSONVar json_params;
json_params["device_id"] = get_device_id();
json_params["api_key"] = "your_open_platform_api_key";
json_params["version"] = "0.0.1"; // Firmware version for OTA
json_params["bin_id"] = "firmware_id"; // Firmware ID for OTA
json_params["wifi_ssid"] = "MyWiFi"; // Wi-Fi name
json_params["wifi_pwd"] = "lykj987654321"; // Wi-Fi password
String send_data = JSON.stringify(json_params);
int httpCode = on_bind_device_http.POST(send_data);
if (httpCode > 0)
{
String payload = on_bind_device_http.getString();
JSONVar parse_res = JSON.parse(payload);
if (JSON.typeof(parse_res) == "undefined" || String(httpCode) != "200")
{
on_bind_device_http.end();
Serial.println("Device binding failed, code:" + String(httpCode));
}
if (parse_res.hasOwnProperty("success"))
{
bool success = (bool)parse_res["success"];
String message = (const char *)parse_res["message"];
if (success == false)
{
on_bind_device_http.end();
Serial.println("Device binding failed: " + message);
}
else
{
on_bind_device_http.end();
Serial.println("Device bound successfully");
esp_ai.setLocalData("binded", "1");
}
}
else
{
on_bind_device_http.end();
Serial.println("Device activation failed: request failed");
}
}
else
{
Serial.println("Device activation failed: request failed");
on_bind_device_http.end();
}
}
