Connecting to Azure IoT Central

Hi, I’m trying to connect to the Azure IoT Central vía DPS. This snippet of code retrieved from the Dev Center works great:

#require "AzureIoTHub.agent.lib.nut:5.1.0"

const AZURE_IOT_CENTRAL_SCOPE_ID = "<YOUR_AZURE_IOT_CENTRAL_SCOPE_ID>";
const AZURE_IOT_CENTRAL_DEVICE_ID = "<YOUR_AZURE_IOT_CENTRAL_DEVICE_ID>";
const AZURE_IOT_CENTRAL_GROUP_KEY = "<YOUR_AZURE_IOT_CENTRAL_GROUP_KEY>";
local AZURE_IOT_CENTRAL_DEVICE_KEY = http.base64encode(crypto.hmacsha256(AZURE_IOT_CENTRAL_DEVICE_ID, http.base64decode(AZURE_IOT_CENTRAL_GROUP_KEY)));

local client = null;
dps <- AzureIoTHub.DPS(AZURE_IOT_CENTRAL_SCOPE_ID, AZURE_IOT_CENTRAL_DEVICE_ID, AZURE_IOT_CENTRAL_DEVICE_KEY);

function onConnected(err) {
    if (err != 0) {
        server.error("Connection failed: " + err);
    }
}

local registrationCalled = false;
local onCompleted = function(err, resp, connStr) {
    if (err == 0) {
        server.log(registrationCalled ? "Device has been registered" : "Device is registered already");
        ::client <- AzureIoTHub.Client(connStr, onConnected);
    } else if (err == AZURE_DPS_ERROR_NOT_REGISTERED && !registrationCalled) {
        // The device is not registered
        server.log("Device is not registered. Starting registration...");
        registrationCalled = true;

        // Register the device
        dps.register(onCompleted);
    } else {
        server.error("Error occurred: code = " + err + ", response = " + http.jsonencode(resp));
    }
}.bindenv(this);

// Try to get a Device Connection String
dps.getConnectionString(onCompleted);

And I get the following message on the logger:

2021-07-14T19:04:27.825 +00:00	[Agent]	Device is registered already

But, when I attempt to connect to the Azure IoT Central with this code (notice that I only added one line at the bottom from the last code):

#require "AzureIoTHub.agent.lib.nut:5.1.0"

const AZURE_IOT_CENTRAL_SCOPE_ID = "<YOUR_AZURE_IOT_CENTRAL_SCOPE_ID>";
const AZURE_IOT_CENTRAL_DEVICE_ID = "<YOUR_AZURE_IOT_CENTRAL_DEVICE_ID>";
const AZURE_IOT_CENTRAL_GROUP_KEY = "<YOUR_AZURE_IOT_CENTRAL_GROUP_KEY>";
local AZURE_IOT_CENTRAL_DEVICE_KEY = http.base64encode(crypto.hmacsha256(AZURE_IOT_CENTRAL_DEVICE_ID, http.base64decode(AZURE_IOT_CENTRAL_GROUP_KEY)));

local client = null;
dps <- AzureIoTHub.DPS(AZURE_IOT_CENTRAL_SCOPE_ID, AZURE_IOT_CENTRAL_DEVICE_ID, AZURE_IOT_CENTRAL_DEVICE_KEY);

function onConnected(err) {
    if (err != 0) {
        server.error("Connection failed: " + err);
    }
}

local registrationCalled = false;
local onCompleted = function(err, resp, connStr) {
    if (err == 0) {
        server.log(registrationCalled ? "Device has been registered" : "Device is registered already");
        ::client <- AzureIoTHub.Client(connStr, onConnected);
    } else if (err == AZURE_DPS_ERROR_NOT_REGISTERED && !registrationCalled) {
        // The device is not registered
        server.log("Device is not registered. Starting registration...");
        registrationCalled = true;

        // Register the device
        dps.register(onCompleted);
    } else {
        server.error("Error occurred: code = " + err + ", response = " + http.jsonencode(resp));
    }
}.bindenv(this);

// Try to get a Device Connection String
dps.getConnectionString(onCompleted);
client.connect();

I get the following error:

2021-07-14T19:05:50.175 +00:00	[Agent]	ERROR: the index 'connect' does not exist
2021-07-14T19:05:50.175 +00:00	[Agent]	ERROR:   in main agent_code:36

What’s the problem? :disappointed_relieved:

PD: I think that is something related to Squirrel, I don’t have too much experience with this programming language.

client is only instantiated when onCompleted is called. Maybe try calling client.connect() from inside onCompleted?

Same problem when is inside of onCompleted, but inside of onConnected it works. However, since client is a global variable, why can’t I use it outside of those functions?

Ah - client is not a global variable. It’s a local defined in the main context, but as soon as your program runs off the end of your code, it’ll be deleted.

If you change:
local client = null
to
client <- null

…then it’s a global and will persist.