Connecting Arduino MKR 1010 WiFi to Azure IoT Hub Device

Copper Contributor

Hi Community fam,

I am facing some issues connecting my Arduino MKR 1010 to Azure IoT Hub and I do not have any other options than open a discussion here. I really hope that someone will be able to help. So, what I did is:
1. Create CA signed certificate, using ECC algorithm for signing, following this guide - https://github.com/Azure/azure-iot-sdk-c/blob/main/tools/CACertificates/CACertificateOverview.md
2. Then, generated leaf certificate for my IoT Hub Device, again using the CACertificates scripts.
3. I create IoT Hub where I created a device with authentication type: X509 CA Signed certificate.
4. Afterwards, I created the Device Provisioning Service that is connect to the create IoT Hub.
5. I added the Root certificate that was generated in the first step.

6. Then, I used the following code: https://github.com/Vitomir2/Digital-Twins-Azure-IoT-Hub/blob/main/source/auth-x509-certs-arduino-ske...
7. After uploading the code on the board, it returns me: Connect Error: -2. I know this error comes from the MQTT and not exactly from the Azure, but I hope someone will have experience with that.

Just tell me if you need more information about my setup, but basically, in summary that is it.

Any help will be appreciated and also I am in a hurry a lot, because I have deadline to finish some measurements when connect successfully to the IoT Hub device.

#iot-hub, #x509-certificate #arduino

Best regards,

Vitomir

5 Replies

@VPavlov Have you found a solution for that?

Have you seen this post: https://paul-bruffett.medium.com/iot-azure-pipeline-9725ac2b6a00

 

@mgungor I have not, but I decided to proceed with just a self-signed X509 certificate just to finish my project, because my deadline was 30th of August.

About the post you forwarded, I think I have seen it before, but it uses SAS token for the authentication, however, I had to use X509 CA signed certificate. Thanks for the highlight.

I plan to create my own example and to push it to an Arduino Library, but not sure if I am going to have time for this. If you also need this, but you have time to wait, I raised a ticket on github for sample how the X509 CA Signed certificate authentication can be used with boards that use ATECC508/608 crypto chips.

BR,
Vitomir

@VPavlov Do you still need help with this? I have numerous Arduino devices connected to my Azure IOT Hub and I can show you an easy way to do this.

That would be great actually, why don't you make a video and post on youtube - then everyone else would benefit.

@mkgungor 

I appreciate your suggestion about posting on Youtube, however, I don't believe that code writing lends itself very well to video. I get weary of watching people correcting their typos.

 

Here is a very simple way to send MQTT data to an Azure IOT hub. The following uses a simplified ketch to connect to Wifi, connect to Azure using MQTT, and then send a packet of data to the hub, in this case it is sending the elapsed time in milliseconds.

 

Procedure:

1. Create an IOT Hub in Azure.

2. In the hub, create a device. The only entry needed is a name for the device, in this case I will use "MKRTest", but it can be anything.

3. Open the Azure terminal using the >_ icon at the top of the page. Add the CLI extension by entering "az extension add --name azure-cli-iot-ext" without the quotes.

4. To get a token for your device enter:

"az iot hub generate-sas-token -d <device name> -n <hub name> --du 63072000"

without the quotes. Use your names for the items marked with <>. Do not use the <>.

5. To monitor your device in the Azure terminal, enter:

"az iot hub monitor-events -n <hub name> -t 0 --output table", again no quotes.

6. Download and run the following sketch on your MKR Wifi 1010. Add your data in the values marked with the carets (<>). Do not use the carets.

Let me know how you make out.

 

// Sketch for Arduino MKR WiFi 1010

// Libraries
// Uses WiFiNINA https://www.arduino.cc/en/Reference/WiFiNINA
// Search on "Nina".
// Joël Gähwiler's MQTT Library https://github.com/256dpi/arduino-mqtt
// Search on "lwmqtt".
 #include <WiFiNINA.h>
 #include <MQTT.h>
 #include <Arduino.h>

// Azure hub connection info
const char server[] = "<YourHubName>.azure-devices.net";
const char clientId[] = "MKRTest"; //Device must be created in Azure IOT Hub.
const char username[] = "<YourHubName>.azure-devices.net/MKRTest/api-version=2016-11-14";
const char password[] = "SharedAccessSignature sr=<YourSASToken>"; //Get in Azure terminal.
String feedbackTopic = "devices/MKRTest/messages/events/";
String payload;

WiFiSSLClient net;
MQTTClient mqtt(1024); 

void setup() {
  //Start serial. Wait for it to start.
  Serial.begin(9600);
  while (!Serial) { } //This for interactive use only. Outside the IDE, it will loop forever.
  Serial.println();
  Serial.println("Serial started");
  
  //Connect WiFi
  Serial.println("Connecting WiFi");
  WiFi.begin("<YourWiFiSSID>","<YourWiFiPassword>");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(5000);
  }
  Serial.print("IP address = ");
  Serial.println( WiFi.localIP());

  //Connect MQTT
  Serial.println("Connecting MQTT");
  mqtt.begin(server, 8883, net);
  mqtt.loop();
  while (!mqtt.connect(clientId, username, password)){
    Serial.print(".");
    delay(5000);
  }
  Serial.print("mqtt.connected=");
  Serial.println(mqtt.connected());
}

void loop() {
  payload="Mkr1010 " + String(millis());
  Serial.println(payload);
  mqtt.publish(feedbackTopic, payload);   
  delay(10000);
}