Hi,
I working on a standalone application for the Oadk-D-POE and I thought to start getting the IMU data out via MQTT. I cobbled together the code below with a switch to run the script connected or standalone. The code works perfectly in connected mode, but in standalone mode nothing happens. What am I doing wrong?
Dirkjan
P.S. to run the code you need paho-mqtt.py from here
import depthai as dai
import marshal
client = True
# Start defining a pipeline
pipeline = dai.Pipeline()
# Define sources and outputs
imu = pipeline.create(dai.node.IMU)
# enable ROTATION_VECTOR at 400 hz rate
imu.enableIMUSensor(dai.IMUSensor.ROTATION_VECTOR, 100)
imu.setBatchReportThreshold(20)
imu.setMaxBatchReports(20)
# Script node
script = pipeline.create(dai.node.Script)
script.setProcessor(dai.ProcessorType.LEON_CSS)
imu.out.link(script.inputs['quaternions'])
script_text = f"""
import time
import socket
import struct
import json
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
from fcntl import ioctl
return socket.inet_ntoa(ioctl(
s.fileno(),
-1071617759, # SIOCGIFADDR
struct.pack('256s', ifname[:15].encode())
)[20:24])
mqttc = Client()
mqttc.connect("192.168.100.33", 1883, 60)
mqttc.loop_start()
ip = get_ip_address('re0') # '192.168.0.110'
msg = {{'device': 'Luxonis OAK-D-POE', 'settings': {{'mode': '{"client" if client else "standalone"}' }}, 'ip': {{'addr': ip }} }}
info = mqttc.publish("Antea_test_topic/alive", json.dumps(msg))
while True:
rv = node.io['quaternions'].get().packets[0].rotationVector
rv_d = {{'i': rv.i, 'j': rv.j, 'k': rv.k, 'real': rv.real }}
msg = json.dumps(dict(rv_d))
info = mqttc.publish("Antea_test_topic/quat_antea", msg)
"""
# print(script_text)
with open("paho-mqtt.py", "r") as f:
paho_script = f.read()
script.setScript(f"{paho_script}\n{script_text}")
if (client):
xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName('end')
script.outputs['out'].link(xout.input)
# After initializing the device, enable log levels
device_info = dai.DeviceInfo('192.168.100.37')
with dai.Device(pipeline, device_info) as device:
device.setLogLevel(dai.LogLevel.WARN)
device.setLogOutputLevel(dai.LogLevel.WARN)
while True:
print(marshal.loads(device.getOutputQueue("end").get().getData())) # Wait for the "end" msg
else:
(f, bl) = dai.DeviceBootloader.getFirstAvailableDevice()
bootloader = dai.DeviceBootloader(bl)
progress = lambda p : print(f'Flashing progress: {p*100:.1f}%')
bootloader.flash(progress, pipeline)