0

I am trying to create an example in which a client issues a message to a thing and the thing replies back to the client. The thing is connected to Eclipse Ditto via a MQTT connection and the client is simulated via a curl command. In order to do so, I took some parts from the following two tutorials: https://www.eclipse.org/ditto/2018-12-05-example-command-and-control.html and https://github.com/eclipse/ditto-examples/tree/master/mqtt-bidirectional.

Besides the fact that Ditto does not route the messages from the client to the thing in a very reliable way (I would say that one out of three messages is not delivered to the client), the client is not able to receive the response message from the thing, even with a very high timeout value.

This is my Python code that acts as the thing:

import logging
import time
import random
import json
import paho.mqtt.client as mqtt

def on_message(client, userdata, message):
    response = json.dumps({
                    "topic": "org.eclipse.ditto/teapot/things/live/messages/brew",
                    "headers": {
                      "content-type": "application/json",
                      "correlation-id": "command-and-control"
                    },
                    "path": "/inbox/messages/brew",
                    "value": {
                      "eta": 58
                    },
                    "status": 200
                  })                          
    client.publish(outTopic, response)

inTopic = "ditto-tutorial/org.eclipse.ditto:teapot/#";
outTopic = "ditto-tutorial/";
thingId = "teapot";

interval = 10

broker_address = "test.mosquitto.org"
client = mqtt.Client(thingId)         #create new instance
client.on_message = on_message        #attach function to callback
client.connect(broker_address)        #connect to broker
client.loop_start()                   #start the loop
client.subscribe(inTopic)

while True:
    time.sleep(interval)

This is my curl command that simulates the client:

curl -i -X POST 'http://localhost:8080/api/2/things/org.eclipse.ditto:teapot/inbox/messages/brew?timeout=60' \
     -u ditto:ditto \
     -H 'x-correlation-id: command-and-control' \
     -d '{"targetTemperature":85}'

At the beginning I thought that I was doing something wrong with the Ditto protocol, but I believe this is not the case, since the correlation-id is the same in both request and responde and the other fields seem to be OK.

1 Answers1

1

I think the problem might be that you are using a fixed correlation-id for your commands and responses. Try to use a random, e.g. a UUID.