thingsboard-client-sdk icon indicating copy to clipboard operation
thingsboard-client-sdk copied to clipboard

Device provisioning

Open Rhys79 opened this issue 4 years ago • 4 comments

I'm just starting to dig into this now, but I'd like to see device provisioning added into this library as well. Looking at potentially deploying this as part of the code for several hundred units, and I really don't want to have to manually provision and hand program every unit....

Rhys79 avatar Feb 16 '21 20:02 Rhys79

Ok, so I think I've added in all of the necessary bits and bobs to handle device self registration/provisioning into the library code, and I have it all deployed in my code. I'm am now getting a compiler error though in a piece of code I did not touch in the library.

This bit of code -

inline Telemetry(const char *key, T val) :m_type(TYPE_INT), m_key(key), m_value() { m_value.integer = val; }

is giving me the following error -

In file included from src\main.cpp:12:0: .pio\libdeps\win\ThingsBoard\src/ThingsBoard.h: In instantiation of 'Telemetry::Telemetry(const char*, T) [with T = String; <template-parameter-1-2> = ArduinoJson6172_91::enable_if<false, void>]': src\main.cpp:514:33: required from here .pio\libdeps\win\ThingsBoard\src/ThingsBoard.h:46:64: error: cannot convert 'String' to 'int' in assignment :m_type(TYPE_INT), m_key(key), m_value() { m_value.integer = val; }

Any suggestions on what I managed to somehow screw up in a section of code I didn't touch?

Here's the bulk of what I added to my main code to handle device registration. In the library I just copied a few functions, renamed, and lightly modified them to point to the correct MQTT topics.

RPC_Response processDeviceRegistration(const RPC_Data &data)
{
  Serial.println("Received the device registration response");
  if(data["status"]=="SUCCESS"){
    String credential = data["credentialsValue"];
    EEPROM.write(10, sizeof(credential));
    EEPROM.writeString(20, (String)credential);
    dr.DR_Unsubscribe();
    return RPC_Response("registration response", true);
  }
  else{
    Serial.println("Device registration failed");
    return RPC_Response("registration response", false);
  }
}

const size_t callbacks_size = 2;
RPC_Callback callbacks[callbacks_size] = {
  { "device_registration", processDeviceRegistration }
};

void reinitialize()
{
  if (!dr.RPC_Subscribe(callbacks, 1)) {
      Serial.println("Failed to subscribe for RPC");
      return;
    }
  dr.loop();
  String IMEI = modem.getIMEI();
  const int data_items = 3;
  Telemetry data[data_items] = {
    Telemetry("deviceName", IMEI),
    Telemetry("provisionDeviceKey", provision_device_key),
    Telemetry("provisionDeviceSecret", provision_device_secret),
  };
  if (!dr.sendDR(data, data_items)){
    Serial.println("Device registration send failed");
  }

Rhys79 avatar Feb 17 '21 20:02 Rhys79

Looks like it is this line causing the problem, not my modified library code.

Telemetry("deviceName", IMEI),

For some reason the Telemetry template is detecting the IMEI variable as an int instead of a string and trying to convert it. Not sure why or how to fix that issue?

I'll fork and update the code and send a pull request in the next day or two. I haven't been able to test it yet, but I think the changes I made should otherwise work and allow for auto-provisioning of devices. May take me a bit to get the time to put together an example sketch for it though.

Rhys79 avatar Feb 17 '21 20:02 Rhys79

Well then, helps if the Telemetry constructor includes a standard character array or String type. I duplicated the const char *val line removing the const modifier and my code at least compiles now. Tried to do a String type, but that gave a different error, so just converted my data to a character array and fed that in. Hopefully it actually works when I compile it. If everthing works properly when I actually test it, I'll send a pull request.

Rhys79 avatar Feb 19 '21 19:02 Rhys79

Not 100% sure. but there is a high likely hood that the fault has been fixed with the newest merge, becuase atleast for me the provision works perfectly now.

Would be nice if you could check if the issue is fixed @Rhys79 and if it is close this issue :D.

OekoSolveMG avatar Jun 16 '22 09:06 OekoSolveMG