aswathy
aswathy

Reputation: 139

Subscribe to get messages not working in Robot framework MQTT Library

I am new to Robot framework and MQTT. I have brought up a subscriber/publisher setup using mosquitto. I have written a code to publish to a subscribed topic. The code for Publish is working fine and I am able to see the output in the subscriber window. However, the subscribe keyword is not working for me.

I have tried both Subscribe and subscribe and validate. For the former, I am getting Messages=[], and for the latter 'The expected payload didn't arrive in the topic'. The screenshot of the codes are attached.

Subscribe and get messages
    Subscribe   MQTTtest2   qos=2   timeout=1   limit=0
    I Publish an MQTT request
    ${messages}=    Subscribe   MQTTtest2   qos=2   timeout=5   limit=0
    log to console  Messages=${messages}

I Publish an MQTT request
    connect   127.0.0.1
    publish  MQTTtest2   testmessage  2  ${false}
    disconnect

MQTT_SUBSCRIBE_AND_VALIDATE
[Tags]  mqtt
[Setup]     Connect     127.0.0.1
I SUBSCRIBE to a Topic
[Teardown]  Disconnect

I SUBSCRIBE to a Topic
#connect  ${MQTT.hostname}
subscribe and validate  ${MQTT.topic}  ${MQTT.qos}  ${MQTT.message}  5
#disconnect

Can you please let me know what I am doing wrong? Subscribe Publish

Upvotes: 0

Views: 1429

Answers (2)

Matt
Matt

Reputation: 11

Subscribe and Validate seems to be working now.

    *** Variable ***
${mqtt-server}   127.0.0.1
${mqtt-path}    ABCD
${message-payload}  MQTT-Test-2.robot payload1 test message from RobotFramework
${message-payload2}  MQTT-Test-2.robot payload2 test message from RobotFramework

*** Keywords ***
Subscribe And Get Messages
    Subscribe   topic=${mqtt-path}  qos=2   timeout=5   limit=0
    #I Publish An MQTT Request
    Publish     topic=${mqtt-path}    message=${message-payload}
    ${messages}=    Subscribe    topic=${mqtt-path}   qos=1   timeout=5   limit=0
    Log To Console  Messages=${messages}

Subscribe And Validate Messages
    Subscribe   topic=${mqtt-path}  qos=2   timeout=5   limit=0
    #I Publish An MQTT Request
    Publish     topic=${mqtt-path}    message=${message-payload2}    
    ${messages}=    Subscribe And Validate    topic=${mqtt-path}   qos=1   timeout=5    payload=${message-payload2}
    Log To Console  Messages=${messages}    

Publish An MQTT Request
    Publish     topic=${mqtt-path}    message=${message-payload}

*** Test Cases ***
MQTT test
    [Tags]  mqtt
    [Setup]  Connect    127.0.0.1
    Publish An MQTT Request
    Subscribe And Get Messages
    Subscribe And Validate Messages
    [Teardown]  Disconnect

==============================================================================

MQTT-Test-3 :: This is a MQTT test project: MQTT-Test-2.robot that explores...

MQTT test ..Messages=['MQTT-Test-2.robot payload1 test message from RobotFramework'] .Messages=None

MQTT test | PASS |

MQTT-Test-3 :: This is a MQTT test project: MQTT-Test-2.robot that... | PASS | 1 critical test, 1 passed, 0 failed

1 test total, 1 passed, 0 failed

Upvotes: 1

Bence Kaulics
Bence Kaulics

Reputation: 7271

You have to connect to the broker in case of subscribe operation too with the Connect keyword.

Currently you have not established connection before any of the Subscribe keywords. In your I Publish an MQTT request you do a connect so that succeeds, but then you do a disconnect as well so the next subscribe fails again.

If you modify your test like this:

*** Keywords ***
Subscribe And Get Messages
    Subscribe   topic=MQTTtest2   qos=2   timeout=5   limit=0
    I Publish An MQTT Request
    ${messages}=    Subscribe   topic=MQTTtest2   qos=2   timeout=5   limit=0
    Log To Console  Messages=${messages}

I Publish An MQTT Request
    Publish  topic=MQTTtest2   testmessage  2  ${false}

*** Test Cases ***
MQTT test
    [Tags]  mqtt
    [Setup]     Connect     127.0.0.1
    Subscribe And Get Messages
    [Teardown]  Disconnect

It should work correctly:

==============================================================================
Robot Framework.My Test
==============================================================================
MQTT test                                                             .Messages=[b'testmessage']
MQTT test                                                             | PASS |

Also it seems the MQTT library registers as a new client with each Connect so if you do separate connect/disconnect for each subscribe and publish, that probably won't work for you as you would expect.

Upvotes: 0

Related Questions