Impossible send more than two messages to Azure IoT Hub with MKR1000

Hello to everyone and thanks in advance.

I’m trying to send telemetry data to Azure Iot Hub. I have my Azure Iot Hub account properly configured, the certificate loaded on my arduino and in fact, I can send messages to Azure…but only two successful messages, after that, all ssl connections with wifiSSLClient.connectSSL allways returns false.

I’ve tried with several versions of the WiFi101.h library, (0.16.0 and 0.14.4) with same results. I’ve tried with different delays between messages: 200ms, 1000ms, 3000ms, 5000ms, 60000ms with the same results. The code is not specially complicated, it’s a slight adaptation of many samples on the web, but in this point, I’ve run out all my resources about the problem.

I tried capture the packets about the communication with Kali/airodump-ng/Wireshark and while I was able to capture the communication between mkr1000 and my AP, I wasn’t able to decrypt ssl communication between mkr1000/Azure Iot hub to try bring light about http errors or whatever info I could get about the problem.

I even tried inject a hardcoded very simple json data message ({‘data’: ‘123’}) and completely avoid the dht library or avoid using messages to monitor serie showing typical wireless network info…i tried change the structure of the code with refactoring the order of the calls while keeping the behaviour… but with any success.

It’s very strange that i’m able to send only two messages (the first two messages and sometimes even only one of them, although this occurs less frequently), and then, nothing, I must reset the arduino or load a new sketch (perhaps with some modifications) and try again.

Following is the code I’m using on my arduino, a very common one found in hundred of samples found in google:

#include <WiFi101.h>
#include <DHT.h>
#include <ArduinoJson.h>

#define DHT_PIN 5
#define DHT_TYPE DHT11

DHT dht(DHT_PIN,DHT_TYPE);

const char ssid[] ="XXXXX";
const char password[] = "XXXXXX";
int wifiStatus = WL_IDLE_STATUS;

const char hostName[] = "XXXXXXXXXX.azure-devices.net";
const char authSAS[] = "SharedAccessSignature sr=XXXXXX.azure-devices.net%2Fdevices%2Fxxx&sig=xxxxx....xxxx";
const String deviceId = "xxxxxxx";
String uriPath = "/devices/" + deviceId + "/messages/events?api-version=2018-06-30";

WiFiSSLClient wifiSSLClient;

void setup() {
  Serial.begin(9600);

  while (!Serial) ;
  if(WiFi.status() == WL_NO_SHIELD){
    Serial.println("WiFi shield not present...");
    while(true);    
  }
  
  //Connect to WPA/WPA2 network:
  Serial.print("Connecting.");
  while( wifiStatus != WL_CONNECTED) {    
    wifiStatus = WiFi.begin(ssid, password);
    Serial.print(".");
    delay(1000);
  }
  printWiFiStatus();
  
  //Initialize DHT11 sensor...
  dht.begin();  
}

void loop() {
  //Capture Telemetry Data...
  int humidity = (int)dht.readHumidity();    
  int temperature = (int)dht.readTemperature();
  String jsonMessage = buildJsonMessage(temperature, humidity);
  
  sendTelemetry(jsonMessage);

  delay(60000);
}

//Post telemetry data to IotHub...
void sendTelemetry(String jsonMessage){    
  if ( !httpPostIotHubTelemetry(jsonMessage)){
    Serial.println("Connection failed...");
    //return;
  }

  unsigned long startTime = millis();
  String response = "";
  char c;
  bool received = false;
  
  //Listen for 5 seconds
  while ((millis() - startTime < 5000) && !received) {   
    while (wifiSSLClient.available()) {
      c = wifiSSLClient.read();
      response.concat(c);
      received = true;
    }    
  }

  if (!response.equals("")) {
    if (response.startsWith("HTTP/1.1 20")) {
      Serial.println(response); 
    } 
    else {
      Serial.println("Error posting " + jsonMessage);
      Serial.println(response);
    }
  }
  else{
    Serial.println("No response"); 
  }
}

String buildJsonMessage(int temperature, int humidity){  
  char temperatureString[5]; 
  char humidityString[5];
  StaticJsonDocument<200> doc;
 
  sprintf(temperatureString, "%2dºC", temperature);
  sprintf(humidityString, "%2d%%", humidity);

  doc["temperature"] = temperatureString;
  doc["humidity"] = humidityString;
  
  //Construimos una cadena con el mensaje a enviar...
  String jsonMessage;
  serializeJson(doc, jsonMessage);
  
  return jsonMessage;
}

boolean httpPostIotHubTelemetry(String jsonMessage){ 
  Serial.print("Connecting to IoT Hub to send message: ");
  Serial.println(jsonMessage);
  
  //Close any connection before send a new request.
  wifiSSLClient.stop();
  if (wifiSSLClient.connectSSL(hostName, 443)) {
    //Request Line:
    wifiSSLClient.print("POST ");
    wifiSSLClient.print(uriPath);
    wifiSSLClient.println(" HTTP/1.1"); 
    
    //Request Headers:
    wifiSSLClient.print("Host: "); 
    wifiSSLClient.println(hostName);
    
    wifiSSLClient.print("Authorization: ");
    wifiSSLClient.println(authSAS); 
    
    wifiSSLClient.println("Connection: close");

    wifiSSLClient.println("Content-Type: application/json");
    
    wifiSSLClient.print("Content-Length: ");
    wifiSSLClient.println(jsonMessage.length());
    
    wifiSSLClient.println();
    wifiSSLClient.println(jsonMessage);

    return true;
  } 
  return false;
}

void printWiFiStatus() {
  byte mac[6];    
  
  //Print the SSID of the network you're attached to:
  Serial.print("\nConnected to WiFi with SSID: ");
  Serial.println(WiFi.SSID());

  //Print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  //Print your WiFi shield's MAC address:  
  WiFi.macAddress(mac);
  Serial.print("MAC Address: ");
  for(int counter=5; counter >= 0; counter--){
    Serial.print(mac[counter], HEX);
    if( counter > 0){
      Serial.print(":");
    }
  }
  Serial.println("\n");    
}

I don’t even know if the problem is with arduino, perhaps may be with Azure, but I haven’t found a log file, a response message, a http code, whatever stuff which could put me in the right way.

My Azure Iot Hub uses the F1, free tier, but apparently if should not be a problem becouse I have 8000 free daily messages, very far what i’m really sending.

I’d appreciate any kind of help, advice or whatever lesson to try resolve my mystery.

Lot of thanks!!

Hello Lace,

Have you found a way to fix your issue yet? I’ve done a very similar test rather using MQTT library and port 8883 and it’s working fine. Attached the sketch if you want give a try. I’ll try to run yours as well and see what happens as I was going to move away from 8883 call anyway due to security reasons.

Regards,
Alexandre

Project_1_wifi_Iot_hub.zip (9.08 KB)