Copyright ©

Mongoose OS Forum


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?

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

mqtt_global_connect MQTT connecting to
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}


  • 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


    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 =, 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 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 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 2017

    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 2017

    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 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

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

Sign In or Register to comment.