In my previous post I discussed how to discover information about the Seneye device, here I will describe some simple code to read values from it and push these to a MQTT broker. I have this running on a small Raspberry Pi Zero W, on which I also have the Motion software and a small streaming web cam.
Having got lots of good information from the Linux commands you start by finding if the device is attached:
dev = usb.core.find(idVendor=9463, idProduct=8708)
Next, ensure that the operating system does not have control of the device:
interface = 0 if dev.is_kernel_driver_active(interface) is True: kernel_driver_active = True dev.detach_kernel_driver(interface)
Then set the first configuration and claim the interface – and it needs to be done in that order, apparently!
dev.set_configuration() usb.util.claim_interface(dev, interface) cfg = dev.get_active_configuration() intf = cfg[(0,0)]
Alternate settings may be ignored as most devices do not have them, so we move straight to the endpoint and search for the first in/out.
epIn = usb.util.find_descriptor(interface, custom_match= lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN) epOut = usb.util.find_descriptor(interface, custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
Then send the READING message to the device, read the response, and read again with longer timeout so that the measurements can be read and returned.
msg="READING" rc=dev.write(epOut,msg) ret=dev.read(epIn,epIn.wMaxPacketSize,1000) ret=dev.read(epIn,epIn.wMaxPacketSize,10000)
Once done the fun of picking the bit flags and integers out of the results starts and I have provided just six of them, as they were the ones I could confirm from the Seneye C++ program. At the end you need to send a closing message called “BYESUD”, I guess to tell the device to go into sleep mode or whatever.
All this code is available in my GitHub repository and I’d encourage you to read the code, try it on one of your systems, and if you want to improve it fork the repo and submit pull requests to me. I’m not looking to make it complex with control functions and displays, just something simple and lightweight.