Forum Discussion
VPavlov
Aug 19, 2022Copper Contributor
Connecting Arduino MKR 1010 WiFi to Azure IoT Hub Device
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 t...
VPavlov
Sep 10, 2022Copper Contributor
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
henderjl
Aug 28, 2023Copper Contributor
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.
- mkgungorAug 28, 2023Copper ContributorThat would be great actually, why don't you make a video and post on youtube - then everyone else would benefit.
- henderjlAug 28, 2023Copper Contributor
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); }