Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Can't connect to Azure

Hi guys,
First of all, kudos for the sleek implementation! I'm using ESP32 with mJS and trying to connect to Microsoft's IoT Hub with no success. I'm following the instructions on the post below. It seems port 443 yields better results (I see it seems I get a connection as MQTT is not trying to reconnect all the time). I can't get my message to get to my portal though. Any ideas?

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

This is what I get on the console log: when I run the MQTT button example:

mqtt_global_connect MQTT connecting to xxxxxx.azure-devices.net:443
mgos_mqtt_ev MQTT Connect (1)
I (14503) wifi: pm start, type:0

Published: no topic: devices/xxxxxx/messages/events/ message: {"free_ram":221992,"total_ram":0}

Comments

  • SergeySergey Dublin, Ireland

    show your code please

  • I'm using the MQTT sample and entering URL, username and password as per article below on the "Device Configuration" tab on the browser (looks like a JSON file). I also

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

    load('api_gpio.js');
    load('api_mqtt.js');
    load('api_sys.js');

    let pin = 4; // GPIO 0 is typically a 'Flash' button
    GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) {
    let topic = 'devices/GarageDoor/messages/events/';
    let message = JSON.stringify({
    total_ram: Sys.total_ram(),
    free_ram: Sys.free_ram()
    });
    let ok = MQTT.pub(topic, message, message.length);
    print('Published:', ok ? 'yes' : 'no', 'topic:', topic, 'message:', message);
    }, true);

    print('Flash button is configured on GPIO pin ', pin);
    print('Press the flash button now!');

  • Perhaps we can do this another way. Mongoose is supposed to support Azure (as website says). Is there a HOWTO somewhere that I can follow to connect to Azure? Or a sample project?

  • SergeySergey Dublin, Ireland

    your publish call may be executed before the MQTT connection gets established.
    Please follow https://mongoose-os.com/video-tutorials.html#video11 and see if that helps you.

  • I definitely get that impression looking at the logs. I thought that was expected. I'll look at the video and post the results. Thanks for the hint anyways!

  • Sorry, I'm lost here. I'm new to JS in general but it seems the example on video 11 also doesn't handle the race condition properly. It does set the handler for the AWS device shadow before setting the handler for button press but I'm not sure that is enough.

    In the MQTT example is even worse as the provided example starts with setting the button/GPIO handler. Looks like the connection is done by the MQTT framework and the documentation has no reference as to how I can even inspect the connection state from code... That alone is a showstopper... So, MQTT seems a work in progress. I'll drop Mongoose OS for now and try Eclipse (ugh!).

    I'll definitely keep an eye on Mongose though. I love the concept and will be back when MQTT is in a better shape.

  • SergeySergey Dublin, Ireland

    I can assure you MQTT is in pretty good shape in Mongoose. You being lost does not mean its underdeveloped or is not working.

    The global MQTT connection tries it's best to stay connected with the server, implementing reconnections, etc. So you don't need to do it yourself.

    The result of MQTT.pub() call tells you whether the publish was successful or not. Handle that return code if you'd like to be sure.

    If you want a low level access to the MQTT state, use C, then it's under your full control.

  • edited April 15

    Well, I don't mean to demean the work done. It's just that it looks like the priority is somewhere else, which is understandable. Hell, the whole ESP32 platform is a work in progress. It's impressive to have something like this at this stage. Yes, I'm lost. It's just 1 method call and I'm using the example provided and an article from M$. As you pointed out, there is no way to avoid race conditions (you can call pub and not be connected yet). Also, no way to query the connection status or know the reason of the failure. It if works you are happy. If not you are as lost as I am.

  • SergeySergey Dublin, Ireland
    edited April 15

    Yeah, and mind that connection can fail at any point, for whatever reason.
    I see your point though. Let's figure out what to do.

    What API would you like to see?

    For example, if you can catch MQTT server connection and disconnection event, would that help?

    MQTT.on(function(ev) {
      if (ev == MQTT.CONNECT) {
      } else if (ev == MQTT.DISCONNECT) {
      }
    }, null);
    

    The MQTT.pub() can give you a failure code, so that you know why it failed, but honestly, would that help much? You'd need to repeat anyway.

  • Well, the return code will be useful to provide me the reason why it's failing so I know the root cause and fix it. Retrying will only get the same result. Is there any documentation on the return code from MQTT.pub()?

    Thanks for the snippet. I guess I can figure out a way to at least not call MQTT.pub() if MQTT is not connected.

Sign In or Register to comment.