Trouble connecting NodeMCU to Microsoft Azure IoT Hub

让人想犯罪 __ 提交于 2020-01-06 19:53:14

问题


I am trying to connect my ESP8266, running the latest NodeMCU build, to a Microsoft Azure IoT Hub via MQTT Protocol.

It appears that this is possible, as it is shown here...

http://thinglabs.io/workshop/esp8266/sending-d2c-messages/

I am using the correct syntax as far as I can see from the MS Azure help...

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support

Unlike the example in ThingLabs, which creates a SAS token using NodeMCU, I have followed the MS document and generated a SAS token using the Device Explorer, for testing purposes.

My LUA code is as follows...

-- Create variables

DEVICE = "testdevice"
IOTHUB = "mynewiothub.azure-devices.net"
PORT = 8883
USER = IOTHUB.."/"..DEVICE.."/api-version=2016-11-14"
PASS = "SharedAccessSignature sr=mynewiothub.azure-devices.net%2Fdevices%2Ftestdevice&sig=Roa5P8BPiGj...v2Vu%2Bm1j9sas%3D&se=1485704099"

-- Create an MQTT Client

azure = mqtt.Client(DEVICE, 60, USER, PASS)

-- Connect to IoTHub via MQTT

azure:connect(IOTHUB, PORT, 1, 0, 
    -- Callback for a successful connection
    function(client)
        print("Connected")     
    end,
    -- Error callback, if connection fails
    function(client, reason)
        print("Error Connecting: "..reason)
    end
)

The response is always Error Connecting: -5 which translates to "There is no broker listening at the specified IP Address and Port".

I suspected that NodeMCU was struggling with the SSL (port 8883) connection. So I ensured that I was able to make HTTPS connections (after verifying the SSL root certificate) with the following code, this worked but did not help.

http.get("https://example.com/info", nil,
function (code, resp)
     print(code, resp)
end)

I cannot seem to get this to work! @Marcel Stör - any thoughts?

Many thanks in advance.


回答1:


I had the same error Error Connecting: -5 , I have set the time on the ESP and it fix this error.

You will find some information about sntp on http://thinglabs.io/workshop/esp8266/sending-d2c-messages/ (as you mention it).



来源:https://stackoverflow.com/questions/41834637/trouble-connecting-nodemcu-to-microsoft-azure-iot-hub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!