简体   繁体   中英

Python MQTT Publish JSONified Numpy Array

I am currently implementing a MQTT protocol to be used between two raspberry pis. The first is a Pi 0 and will have a pi camera connected to it. It will be converting each captured frame to a numpy array and then publish it to the master Pi which will then convert the numpy array to an image using PIL. I am doing it this way since I want the main image processing operations to be performed on the master Pi.

My problem is that no messages are being received by the master Pi. I have preformed debugging and everything is working fine on the servant script. But for some reason nothing is received by the master Pi.

Here are both scripts:

servant.py:

import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    frame_list = frame.tolist()
    MQTT_MESSAGE = json.dumps(frame_list)
    mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
    time.sleep(1)

master.py:

import paho.mqtt.client as mqtt
import numpy as np
import json
import PIL

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

def on_connect(client, userdata, flags, rc):
    print("connected with result code " + str(rc))
    client.subscribe(MQTT_PATH)

def on_message(client, userdata, msg):
    data = json.loads(msg.payload)
    array = np.array(data)
    img = PIL.Image.fromarray(array)
    cv2.imshow('image', img)
    cv2.waitKey()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)

client.loop_forever()

In your publishing code you are not actually giving the client any time to process the message it is trying to send. This is even more of a problem because the message is likely to be larger than a single network packet (being an image). To fix this you have to call the MQTT client loop function (or start the loop in the b

import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    frame_list = frame.tolist()
    MQTT_MESSAGE = json.dumps(frame_list)
    mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
    mqttc.loop()
    time.sleep(1)

or like this:

import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

mqttc.start_loop()

while True:
    ret, frame = cap.read()
    frame_list = frame.tolist()
    MQTT_MESSAGE = json.dumps(frame_list)
    mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
    time.sleep(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM