Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for multiple devices in SUDevice() #8

Open
meteosat007 opened this issue Mar 17, 2020 · 7 comments
Open

Adding support for multiple devices in SUDevice() #8

meteosat007 opened this issue Mar 17, 2020 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@meteosat007
Copy link

Hi,

Been using this code for 2 years now and will be adding a second USB to the Pi and want to extract values from both. I can see that d=SUDevice() delivers the serial number, but despite many attempts I cannot work out how to use <pyseneye.sud.SUDevice object at 0x76a505d0> my response, in the next request d.action(Action.ENTER_INTERACTIVE_MODE) to allow me to retrieve data from 2 USB Seneye connected to 1 RPi.

Sure its a simple fix but it has me scratching my head here.

Regards

@mcclown
Copy link
Owner

mcclown commented Mar 18, 2020

Great to hear that you've been using this code for so long successfully!

I never built the library with more than 1 device in mind an unfortunately I don't have 2 to test/validate. Looking at the code I suspect that the change required would be somewhere between line 522 and 533 in sud.py.

The most likely candidate is line 522, according to the documentation here you can pass find_all=True to usb.core.find and that will return multiple devices.

I'd try modifying sud.py to include the following below. Sorry I haven't been able to test this as my Pi is being used for something else right now...so this is really back of a napkin coding, off the top of my head.

class SUDevice:

    def __init__(self, dev_index = 0):

        dev_list = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID, find_all=True)
        dev = dev_list[dev_index]

If you still have an issue I'll try to borrow a friends Seneye and extend to add this functionality. If you do get it to work then add a pull request and I'll release a new version with this new functionality.

Let me know if you need any more help.

@mcclown mcclown changed the title Question on the response to d=SUDevice() Adding support for multiple devices in SUDevice() Mar 18, 2020
@mcclown mcclown self-assigned this Mar 18, 2020
@mcclown mcclown added the enhancement New feature or request label Mar 18, 2020
@meteosat007
Copy link
Author

meteosat007 commented Mar 18, 2020 via email

@mcclown
Copy link
Owner

mcclown commented Mar 18, 2020

What I've specified isn't going to use the serial numbers of the devices, it will just identify them in the order the USB identifies them in. If that order is the same every time, then what I've specified should work.

You'll call SUDevice(0) for the first device and SUDevice(1) for the second device. SUDevice() will just select the first device by default.

If that doesn't consistently identify the devices in the same order, then we'll have to get a bit more involved and try to identify them by their serial numbers.

@meteosat007
Copy link
Author

Great to hear that you've been using this code for so long successfully!

I never built the library with more than 1 device in mind an unfortunately I don't have 2 to test/validate. Looking at the code I suspect that the change required would be somewhere between line 522 and 533 in sud.py.

The most likely candidate is line 522, according to the documentation here you can pass find_all=True to usb.core.find and that will return multiple devices.

I'd try modifying sud.py to include the following below. Sorry I haven't been able to test this as my Pi is being used for something else right now...so this is really back of a napkin coding, off the top of my head.

class SUDevice:

    def __init__(self, dev_index = 0):

        dev_list = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID, find_all=True)
        dev = dev_list[dev_index]

If you still have an issue I'll try to borrow a friends Seneye and extend to add this functionality. If you do get it to work then add a pull request and I'll release a new version with this new functionality.

Let me know if you need any more help.

I made changes as below but get this error.

class SUDevice:
"""Encapsulates a Seneye USB Device and it's capabilities."""

def __init__(self**, dev_index = 0**):
    """Initialise and open connection to Seneye USB Device.

    Allowing for actions to be processed by the Seneye device.

    ..  note:: When finished SUDevice.close() should be called, to
        free the USB device, otherwise subsequent calls may fail.

    ..  note:: Device will need to be in interactive mode, before taking
        any readings. Send Action.ENTER_INTERACTIVE_MODE to do this.
        Devices can be left in interactive mode but readings will not be
        cached to be sent to the Seneye.me cloud service later.

    :raises ValueError: If USB device not found.
    :raises usb.core.USBError: If permissions or communications error
    occur while trying to connect to USB device.

    :Example:
        >>> from pyseneye.sud import SUDevice, Action
        >>> d.action(Action.ENTER_INTERACTIVE_MODE)
        >>> s = d.action(Action.SENSOR_READING)
        >>> s.ph
        8.16
        >>> s.nh3
        0.007
        >>> s.temperature
        25.125
        >>> d.action(Action.LEAVE_INTERACTIVE_MODE)
        >>> d.close()
    """
    dev_list = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID**, find_all=True**)
    **dev = dev_list[dev_index]**

    if dev is None:
        raise ValueError('Device not found')

Error seen is with d=SUDevice() or d=SUDevice(0)

sudo python3 seneye-home.py
Traceback (most recent call last):
File "seneye-home.py", line 70, in
main()
File "seneye-home.py", line 15, in main
d = SUDevice(0)
File "/usr/local/lib/python3.4/dist-packages/pyseneye/sud.py", line 523, in init
dev = dev_list[dev_index]
TypeError: 'generator' object is not subscriptable

Sorry for all the questions but this level of code not normally my area...

Alan

@mcclown
Copy link
Owner

mcclown commented Mar 18, 2020

Ah, this may take a little more thought. Let me see if I can get a python environment spun up at the weekend, then I can take a look.

@meteosat007
Copy link
Author

meteosat007 commented Mar 18, 2020 via email

@meteosat007
Copy link
Author

meteosat007 commented Mar 20, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants