How to interface two RaspberryPis over MQTT with Cayenne

NOTE: I posted the same question on slack but eventually it also helps other people here

Hi Guys,
I have following use case: I would like to use two Raspberry Pis to outsource some computation from the first one because the FPS rate got too low for my code. I would like to use the first RPI to publish to MQTT-cayenne and the second one to read the data regularly from the page and do some action based on that (show monitor image etc).

Is it possible to subscribe two devices to the same ID and read the data regularly from it?
the received example needs an action as far as I have seen: https://github.com/myDevicesIoT/Cayenne-MQTT-Python/blob/master/examples/Example-02-ReceiveData.py . Can you also read the data from cayenne without sending something usinng widgets, sliders?

this post is with esp8266 Multiple MQTT Clients on 8266
well, i am not tried on raspberry pi, but if you use node-red then you can subscriber to other device topic.

Thank you shramik for your fast help!
I’ve been going through the project that you’ve referenced, I think it is pretty clear to me now what the code does. However, that example uses a customized arduino-mqtt library.

I have to use cayenne-mqtt with python that works well with the examples:

Is there also a way that DeviceA publishes data over MQTT and DeviceB just fetches the data in certain intervals? If there’s no 1line command for it, can you think of a workaround?

Thank you!

https://pypi.org/project/paho-mqtt/ you can use paho to subscribe manually to the topic

Thank you! I’m currently having a look at the example codes provided:

How would that line look like If i want to connect to cayenne-mqtt:
m = subscribe.simple(topics, hostname=“mqtt.eclipse.org”, retained=False, msg_count=2)

try this code:-
You will need two devices on the cayenne dashboard.
Client_id_B is the subscribe device.
Client_id_A is the publish device.

import paho.mqtt.client as mqtt
import time
import sys

time.sleep(5)  # Sleep to allow wireless to connect before starting MQTT

username = "username"
password = "pass"
clientid = "client_id_B"

topics = "v1/username/things/client_id_A/data/21"


def on_message_msgs(mosq, obj, msg):
    # This callback will only be called for messages with topics that match
    # $SYS/broker/messages/#
    print("MESSAGES: " + msg.topic + " " +
          str(msg.qos) + " " + str(msg.payload))


mqttc = mqtt.Client(client_id=clientid)
mqttc.username_pw_set(username, password=password)
mqttc.connect("mqtt.mydevices.com", port=1883, keepalive=60)
# mqttc.loop_start()
mqttc.message_callback_add(topics, on_message_msgs)
mqttc.subscribe(topics, 0)

topic_dht11_temp = "v1/" + username + "/things/" + clientid + "/data/1"

while True:
    mqttc.loop_forever()
    temp11 = 22
    temp11 = "temp,c=" + str(temp11)
    mqttc.publish(topic_dht11_temp, payload=temp11, retain=True)