New Homepage Forums Bug Reports 1Sheeld MQTT message big delay

This topic contains 3 replies, has 2 voices, and was last updated by avatar perezoft 2 years, 9 months ago. This post has been viewed 486 times

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #61459
    avatarperezoft
    Participant

    Hello

    I have finally get my Arduino working with IOT_shield and subscribed to a MQTT_HOST/TOPIC. The example you provided have a bug, because the connection to mqqt server is executed on setup() routine. When arduino starts the first action is to run setup() but at this time the connection between 1sheeld and the phone still disconnected and the IOT shield is not enabled yet.

    I have fixed this, verifying in loop() using isConnected(). If false then try to connect (same steps as setup()).

    Now I can subscribe arduino to a MQTT_HOST/TOPIC. From another device I can publish a message for this TOPIC and almost instantly I can see these message on the APP_IOT_SCREEN(Subscriptions) and later on the arduino (OnNewMessage event).

    My concern is that there is a big delay (about 8 seconds) from time when the APP receives the message and the time arduino receives the OnNewMessage event.

    ¿Is there something that I can configure to reduce this delay?

    Regards

    Víctor Pérez

    #61503
    avatarEslam Ali
    Participant

    Hi Victor,

    For the first point about the bug in the example, Actually we intended to do this to make sure the user experience the steps by pressing Arduino’s reset button after uploading the sketch and switch the 1Sheeld board to the communicating mode and not miss any step, but i think you have a point getting it inside the loop and make all the connection under a certain condition so i will check the cases and we will make an update even here for you or the library :).
    In case the delay between the app and arduino, there’s no delay in callbacks (hence called callback to be called once a message received) and it should work smoothly, I wonder if you have any delays inside the loop() if so please remove them or use OneSheeld.delay(/*Seconds*/) instead. Please paste your code here too to speed up the debugging 😉

    #61558
    avatarperezoft
    Participant

    Eslam

    Thank you for your reply.

    I have uploaded my code

    Thanks

    #61561
    avatarperezoft
    Participant

    #define CUSTOM_SETTINGS
    #define INCLUDE_IOT_SHIELD
    #define INCLUDE_TERMINAL_SHIELD

    /* Set your host name */
    #define HOST_NAME “broker.mqttdashboard.com”

    /* Include 1Sheeld library. */
    #include <OneSheeld.h>

    /* Set an LED on pin 13. */
    int ledPin = 13;
    /* Subscribe to topic 1Sheeld/MyArduino/led . */
    const char * myTopic = “madin/rgvpr”;

    void setup()
    {

    /* LED pin mode output. */
    pinMode(ledPin,OUTPUT);

    /* Conexión al MQTT */
    ASGA_IOT_CONNECT();

    }

    void ASGA_IOT_CONNECT()
    {

    /* Start communication. */
    OneSheeld.begin();
    /* Disconnect from broker. */
    IOT.disconnect();
    /* Reset all connection variables to default */
    IOT.resetConnectionParametersToDefaults();
    /* Connect to mosquitto’s public broker. */
    IOT.connect(HOST_NAME);
    /* Subscribe to new messages. */
    IOT.setOnNewMessage(&newMessage);
    /* Subscribe to connnection status callback. */
    IOT.setOnConnectionStatusChange(&connectionStatus);
    /* Subscribe to error callback. */
    IOT.setOnError(&error);
    /* Some time for app to connect. */
    delay(3000);
    /* Subscribe to led topic. */
    IOT.subscribe(myTopic);

    if(IOT.isConnected())
    {
    Terminal.println(“Conectado “);
    }

    }

    void loop()
    {

    if(!IOT.isConnected())
    {
    ASGA_IOT_CONNECT();
    }

    }

    void newMessage(char * incomingTopic, char * payload, byte qos, bool retained)
    {
    /* Check on incomingTopic. */
    if(!strcmp(myTopic,incomingTopic))
    {

    Terminal.println(payload);

    /* If payload states ON. */
    if(!strcmp(“ON”,payload))
    {
    /* Turn on the led. */
    Terminal.println(“ENCIENDO”);
    IOT.publish(“madin/rgvpr”,”<ARDUINO>ENCIENDO”,QOS_0);
    digitalWrite(ledPin,HIGH);
    }
    /* If payload states OFF. */
    else if(!strcmp(“OFF”,payload))
    {
    /* Turn off the led. */
    Terminal.println(“APAGO”);
    IOT.publish(“madin/rgvpr”,”<ARDUINO>APAGO”,QOS_0);
    digitalWrite(ledPin,LOW);
    }
    }
    }

    void connectionStatus(byte statusCode)
    {
    /* Check connection code and display. */
    switch(statusCode)
    {
    case CONNECTION_SUCCESSFUL: Terminal.println(“CONNECTION_SUCCESSFUL”);break;
    case CONNECTION_FAILED: Terminal.println(“CONNECTION_FAILED”);break;
    case CONNECTION_LOST: Terminal.println(“CONNECTION_LOST”);break;
    case CONNECTION_LOST_RECONNECTING: Terminal.println(“CONNECTION_LOST_RECONNECTING”);break;
    case NOT_CONNECTED_YET: Terminal.println(“NOT_CONNECTED_YET”);break;
    case MISSING_HOST: Terminal.println(“MISSING_HOST”);break;
    }
    }

    void error(byte errorCode)
    {
    /* Check error code and display. */
    switch(errorCode)
    {
    case CONNECTION_REFUSED : Terminal.println(“CONNECTION_REFUSED”);break;
    case ILLEGAL_MESSAGE_RECEIVED : Terminal.println(“ILLEGAL_MESSAGE_RECEIVED”);break;
    case DROPPING_OUT_GOING_MESSAGE : Terminal.println(“DROPPING_OUT_GOING_MESSAGE”);break;
    case ENCODER_NOT_READY : Terminal.println(“ENCODER_NOT_READY”);break;
    case INVALID_CONNACK_RECEIVED : Terminal.println(“INVALID_CONNACK_RECEIVED”);break;
    case NO_CONNACK_RECEIVED : Terminal.println(“NO_CONNACK_RECEIVED”);break;
    case CONNACK_UNACCEPTABLEP_ROTOCOLVERSION : Terminal.println(“CONNACK_UNACCEPTABLEP_ROTOCOLVERSION”);break;
    case CONNACK_IDENTIFIER_REJECTED : Terminal.println(“CONNACK_IDENTIFIER_REJECTED”);break;
    case CONNACK_SERVER_UNAVAILABLE : Terminal.println(“CONNACK_SERVER_UNAVAILABLE”);break;
    case CONNACK_AUTHENTICATION_FAILED : Terminal.println(“CONNACK_AUTHENTICATION_FAILED”);break;
    case CONNACK_NOT_AUTHORIZED : Terminal.println(“CONNACK_NOT_AUTHORIZED”);break;
    case CONNACK_RESERVED : Terminal.println(“CONNACK_RESERVED”);break;
    }
    }`

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.