Argon/Photon hard fault when trying to connect to Azure IoT Hub

Hi all,

I’m having trouble connecting the Particle and the Argon to my Azure IoT Hub.
Even when I’m using the example code from the AzureIotHubClient library both, the Particle and the Argon, are crashing directly after flashing the code.
I’ve already tried to switch the keys/ connection strings of the IoT devices in Azure, add a new IoT device in Azure and also tried to connect the Particle/Argon to a different Azure IoT Hub - nothing works.

But what really irritates me is that I’ve tested the code with another Photon and on this one it is working perfectly fine. Now, after 2 days of trying I don’t know what else I could try and hope you guys might have a solution.
I’m using the AzureIotHubClient version 2.1.0 and the ArduinoJson 5.13.5.

Many thanks in advance!

// This #include statement was automatically added by the Particle IDE.
#include "ArduinoJson.h"
#include "AzureIotHubClient.h"

#define CONNECTON_STRING "<Your Azure IoT Hub or Azure IoT Central Connection String>"

int count = 0;
int msgId = 0;
char telemetryBuffer[256];


void setup()

void loop()
  digitalWrite(LED_BUILTIN, HIGH);

  //loop returns true if connected to Azure IoT Hub
  if (hub.loop())
    if (count++ % 25 == 0)
      Serial.printf("msg id: %d\n", msgId);

  delay(20); // allow for a short blink before turning led off
  digitalWrite(LED_BUILTIN, LOW);

char *telemetryToJson()
    use to calculate JSON_OBJECT_SIZE
    Have allowed for a few extra json fields that actually being used at the moment
  DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(12) + 200);
  JsonObject &root = jsonBuffer.createObject();

  root["deviceid"] = hub.getDeviceId();

  root["temp"] = 20 + random(-3, 3); // random temperature for sample
  root["humidity"] = 70 + random(-20, 20);
  root["pressure"] = 1080 + random(-100, 100);
  root["light"] = 50 + random(-50, 50);

  root["geo"] = "Sydney";
  root["utc"] = Time.format(, TIME_FORMAT_ISO8601_FULL).c_str();
  root["mem"] = System.freeMemory();
  root["id"] = ++msgId;
  root["schema"] = 1;

  root.printTo(telemetryBuffer, sizeof(telemetryBuffer));

  return telemetryBuffer;

Since you’ve allocated the DynamicJsonBuffer on the stack inside telemetryToJson(), the memory block where the JSON is stored is freed when the function exits. However, you’ve returned the pointer to it as the result of the function, but by the time the caller gets it, the pointer is no longer valid.

There’s a chance that the pointer will still work sometimes because the underlying memory has not changed, but it’s mostly a recipe for disaster.

Many thanks for the quick answer!
Just one followup question: Where should I put the DynamicJsonBuffer then?

Another update:

I’ve tried to place it outside the telemetryToJson() but it didn’t changed it.
Also, I’ve tried to shorten the code to an absolute minimum (without JSON) but it hasn’t helped either.

As soon as the code reaches the hub.loop(), the Argon/Photon crashes. It publishes the String to particle one time and then crashes.

// This #include statement was automatically added by the Particle IDE.
#include <AzureIotHubClient.h>

// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>

#define CONNECTON_STRING "HostName=xxx;DeviceId=xxx;SharedAccessKey=xxx"


void setup()


void loop()
    Particle.publish("---------Works Until Here---------");
  //loop returns true if connected to Azure IoT Hub
 if (hub.loop()){
      //run only if connected
    hub.publish("hello world");
    Particle.publish("---------CONNECTED TO AZURE---------");