3

I am trying to connect my NodeMCU ESP-12N to Azure IoT hub.

I have created client with code like this:

esp8266 = mqtt.Client(DEVICE, 240, USER, PASSWD)

and receiving error on calling esp8266:connect

PANIC: unprotected error in call to Lua API (init.lua:127: ssl not available)

When I previously have connect my Arduino, then it was important to flash Wi-Fi with SSL certificate. However I can't do it with NodeMCU (or more right to say - don't know how).

I can get certificates with:

openssl s_client -showcerts -connect ArduinoDemoHub.azure-devices.net:8883

When I have add (thank you @MarcelStör for hint, I have tried previously net.cert.verify)

tls.cert.verify([[
-----BEGIN CERTIFICATE-----
MIIGsTCCBJmgADJKJFdWgAIQF5Koy/50vI3cQAAAAhAXjANBgkqhkiG9w0BAQsF
......
lz3ZDUcyzRgG6TRtA3SjASEUlJMt3f7xuI2nNpt8p5gy9pXuRw==
-----END CERTIFICATE-----
]])

tls.cert.verify(true)

I'v received another error:

Error Connecting: -5

UPDATE: There is another similar topic, and I have set time, but still without result.

UPDATE 2: Here are some links that I trying to use:

Use Azure IoT Hub without client libraries (MQTT)

Sending Device-to-Cloud (D2C) Messages

sankarcheppali gitHub with samples

Here is official Link Communicate with your IoT hub using the MQTT protocol that tells:

To use the MQTT protocol directly, your client must connect over TLS/SSL. Attempts to skip this will fail with connection errors.

This link also provides username that is different from old code samples:

{iothubhostname}/{device_id}/api-version=2016-11-14

3 Answers 3

2

TLS/SSL by default is disabled in the firmware build configuration. CLIENT_SSL_ENABLE in user_config.h must be uncommented before compiling/building the firmware.

The easiest is to use the cloud builder at https://nodemcu-build.com (by yours truly) and check the TLS checkbox.

Also, all TLS/SSL related functions in the net module (you use net.cert.verify) were extracted into the tls module a while ago. Unless there's convincing arguments for not using a current firmware build I suggest you try the dev branch. When you enable "TLS/SSL support" in the builder you get the tls module automatically.

UPDATE: There is nice article about Azure IoT hub and certificates: Certificate between IoT hub and devices connection

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That is exactly that service I have used. But seems something else also required.
Still no luck with dev. Keep trying.
1

I have created SAS with help of Device Explorer, that is looks like:

SharedAccessSignature sr={your hub name}.azure-devices.net%2Fdevices%2FMyDevice01%2Fapi-version%3D2016-11-14&sig=vSgHBMUG.....Ntg%3d&se=1456481802

This value is set to PASSWD variable. Have changed USER variable to:

USER   = "MyAzureIoTHub.azure-devices.net/MyDeviceID/api-version=2016-11-14"

And has set to false tls.cert.verify

tls.cert.verify(false)

Works now :)

Comments

0

You can refer to the following two libraries to connect NodeMCU ESP-12N to Azure IoT hub, these two libraries could be searched in Library Manager:

  • AzureIoTHubMQTTClient
  • AzureIoTProtocol_MQTT enter image description here

After installation,you can refer to the samples. enter image description here

6 Comments

Thank you, Michael, it might be helpful for me. But I am trying use Lua, and still question stays how to add IoT hub SSL/TSL certificate. As I know connection should be secured.
You can set up secure transportation for MQTT Mosquitto broker with SSL/TLS.You can search the demo by googling "How to use Arduino ESP32 MQTTS with MQTTS Mosquitto broker (TLS/SSL)".
Thank you! This is more closer, but still not Lua
The OP is working with the NodeMCU firmware (Lua), not Arduino. This answer, albeit a good one, seems irrelevant in this context.
@MichaelXu-MSFT, have you tried this "Thermostat" example yourself? As I see it doesn't using certificate. And even when I try it doesn't connect.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.