Home Products Shop Articles Applications Learning Zone About Us

  • Up






  • Using a Raspberry Pi with MiniBee
    (also applies to RelayBee and Mini-RelayBee )

     The following details show how to control a MiniBee using a program written in Python running under the Raspbian operating system on a Raspberry Pi model B single board Computer. Since the RelayBee and Mini-RelayBee are simply MiniBee's with some of their outputs replaced by relays, the following also applies to them.
    ( If you are not familiar with basic Minibee functionality, more details can be found here )

    The Raspberry Pi has two standard USB sockets. One of them is usually dedicated to the keyboard (or keyboard and mouse where a small USB hub has been used). It is assumed the MiniBee is connected via a standard USB lead to one of these ports or to a free USB port on a hub if one is connected.

    The following simple example of Python Code is all that is required to turn on output 1 on the MiniBee. Have a look at this code and then see the explanation that follows for detailed description of each of the lines.

    import usb.core

    dev = usb.core.find(idVendor=0x04d8, idProduct=0x0003)

    if dev is None:
        raise ValueError('Device not found')
    else:
        try:
            dev.detach_kernel_driver(0)
        except:
            pass
        dev.set_configuration()

        data=[4, 0b00000010, 0b00000000]
        dev.write(2,data)

    import usb.core
        This line of code imports the very popular PyUSB library of functions for communicating with USB devices. It avoids having to write any of your own code to access USB devices and provides a good range of functions to meet many requirements. In our case we only need to use four of its functions. It is freely available to download and can be installed on your Raspberry Pi very easily. There is no point in duplicating install instructions here so I will just give you a link to follow....
    http://sourceforge.net/projects/pyusb/     (opens in new window)

    dev = usb.core.find(idVendor=0x04d8, idProduct=0x0003)
        This uses one of the PyUSB functions to "find" the attached MiniBee.  The way it finds the MiniBee is by checking all available USB ports to find the one with a device that has the unique identifier associated with the MiniBee. This identifier is made up to two parts: the Vendor ID and the Product ID.  For the MiniBee this is the two hexadecimal numbers 0x04d8 and 0x0003 respectively. If the device is found, the "dev" object is created for the MiniBee and can then be used for susequent operations on the MiniBee. It is only necessary to use this statement once in your program, but obviously, it needs to be before any other use of the "dev" functions.

    if dev is None:
      
     raise ValueError('Device not found
    ')
        It is always good programming practice to check if the MiniBee has actually been found before continuing. This "if" statement will "raise" an exception and halt the program if it is not.
    Assuming it is found, execution continues with the statements after the else:

        try:
            dev.detach_kernel_driver(0)
       
    except:
           
    pass
     
        The function dev.detach_kernel_driver(0) gets round a possible problem with the operating system restricting the use of the MiniBee. When the MiniBee is first plugged into the USB port the operating system tries to be helpful and associates one of its standard drivers to deal with all operations to the board. We don't want this, but, with it "attached" to the MiniBee it won't then allow any other direct operations.  This line "detaches" this driver from the MiniBee allowing us to access it directly. Note that this only needs to be done the first time the program is run after connecting the MiniBee (or after reboot). If you try and detach the driver when it is already detached the program will raise an exception and halt the program. I have found the best way to deal with this is simply to put the detach function at the head of the program inside a "try... except" clause.  In this way it will detach the driver if necessary and ignore it if already detached.

    dev.set_configuration()
        USB devices have different "configurations" that they can be set to depending on the task they are required to perform. In the case of the MiniBee, it simply has one configuration which is it's default. However, this still needs to be "set" as the active one using the statement shown. This only needs to done once in your program and before any other code that communicates with the MiniBee.

     data=[4, 0b00000010, 0b00000000]
        The MiniBee obeys the usual rules of a standard USB device which requires data to be sent to it in the form of message blocks (in USB terms these are called Endpoints).  In Python we specifiy this message block by using a number sequence using the square brackets as shown.  A message block for the MiniBee consists of a message type number and two 8-bit numbers that correspond to the on/off pattern of the outputs. The message type number is simply '4'. This indicates to the MiniBee that it should set its outputs to the bit pattern supplied in the next two numbers (these numbers are shown in binary form to make them easier to relate to the outputs, but could just as easily be specifed in decimal or hexadecimal).  The bit associated with each MiniBee output is shown below...

    A logic '1' corresponds to the associated output being ON and logic '0' is OFF.

    dev.write(2,data)
        Once the data sequence has been specified it is simply sent to the MiniBee using the "write" function within the "dev" object as shown. The number '2' used, is just the name of the internal buffer to use within the MinBee, or , as previously mentioned, the USB "endpoint" number.  For the MiniBee this is always set to 2.   The MinBee outputs will now be set accordingly.

        In the above description its worth noting that the first few lines may look slightly complicated but, once your program has "found" the MiniBee, detached the kernel driver (if necessary) and set the MiniBee configuration, the control of the minibee outputs is simply a case of using the "dev.write" function (with the appropriate bits set in the data) as often as you like.

        All code and descriptions on this page describing the control of the MiniBee outputs also apply to the RelayBee and Mini-RelayBee boards. The relay bee is simply a minibee with its first seven outputs replaced by relays. The mini relay bee just uses the first two outputs, both of which are relays.

     Raspbian Note:   Always remember that running code in Raspbian that accesses USB devices requires you to be in "superuser" mode to have the necessary permissions. This may mean running your Python program with the usual "sudo ...." prefix or, if using the graphical environment with IDLE as your programming environment, you may need to run it from a terminal window (i.e.  typing "sudo idle" in the terminal window) for the programs created to work properly.

    Some examples:

    # set output3 on and all others off
    outputs1= 0b00001000
    outputs2=0b00000000
    data=[4,outputs1,outputs2]
    dev.write(2, data)

    # set outputs 1,5 and 14 on and all others off
    outputs1= 0b00100010
    outputs2=0b10000000
    data=[4,outputs1,outputs2]
    dev.write(2, data)

    # set all outputs on
    data=[4, 0b11111110, 0b01111111]
    dev.write(2, data)

    # set output1 on and all others off (using heaxadecimal)
    outputs1= 0x02
    outputs2=0x00
    data=[4,outputs1,outputs2]
    dev.write(2, data)

    # set outputs 3,4 and 5 on and all others off (using decimal)
    outputs1= 56
    outputs2=0
    data=[4,outputs1,outputs2]
    dev.write(2, data)

     

      Disclaimer:   Please note that the above code and descriptions are designed to help our customers use our boards with the Raspberry Pi. We offer no warranty or guarantees to the suitability of the code for your application. We are also unable to enter into discussions about any errors your code may have and will not, under any curcumstances, attempt to look for programming errors in code sent to us. This applies equally to Python, PyUSB, LibUSB. Linux (all distros) and any Raspberry Pi specifics.

       
    Raspberry Pi is a trademark of the Raspberry Pi Foundation

     

     

     

     

     

     

     

     

    12739  

    Featured
    Items

     
    Just Text:
    New Software
    for the MotorBee



    ConText
    Control by Text


    Security Camera:
    A new article
    by Brian M



    Microscope Control:
    A new article
    by Harald K. A.



     Use our boards with
    Raspberry Pi.


    Pi-Mobile:
    A new article
    by Ava L (UK)