grove.py Python Library

repository·master·Indexed 41 days ago

https://github.com/seeed-studio/grove.py

A Python library for controlling Seeed Studio Grove devices on embedded Linux platforms, with optimized support for Raspberry Pi. It provides a Command Line Interface (CLI) for quick interaction and a programmatic API via classes like Factory, GroveLed, and GroveUltrasonicRanger to manage I2C, digital I/O, analog, and PWM devices.

Tokens
6.2K
Snippets
30
Records
35
Agent score
51%

What's inside grove.py

  1. Use Factory to control Relay, Buzzer, or Electromagnet

    master

    The Factory class provides a generic getGpioWrapper method to control various GPIO-based components.

    Common identifiers include:

    • "Relay"
    • "Buzzer"
    • "Electromagnet"
    • "PIRMotion" (for mini PIR motion sensors)

    Methods available on the returned wrapper include .on(), .off(), and for motion sensors, .has_motion().

    from grove.factory import Factory
    
    # Example: Relay
    relay = Factory.getGpioWrapper("Relay", 16)
    relay.on()
    
    # Example: PIR Motion Sensor
    pir = Factory.getGpioWrapper("PIRMotion", 5)
    if pir.has_motion():
        print("Motion detected")
  2. Prepare the RPi environment for documentation generation

    master

    To generate API documentation on a Raspberry Pi (Raspbian), you must first clone the repository, install the grove.py package, and install the necessary Sphinx and AutoAPI tool packages.

    1. Clone the repository:
      cd
      git clone https://github.com/Seeed-Studio/grove.py.git
    2. Install the grove.py package (refer to the main installation guide).
    3. Install system dependencies and Python packages:
      sudo apt-get install python-sphinx python3-sphinx graphviz
      sudo pip install autoapi sphinx-rtd-theme
      sudo pip3 install autoapi sphinx-rtd-theme
    cd
    git clone https://github.com/Seeed-Studio/grove.py.git
    
    sudo apt-get install python-sphinx python3-sphinx graphviz
    sudo pip install autoapi sphinx-rtd-theme
    sudo pip3 install autoapi sphinx-rtd-theme
  3. Set up the Grove SGP30 module

    master

    To use the Grove SGP30 module with grove.py, ensure you have the module physically connected to one of the I2C ports on your GrovePi Plus.

    Assuming grove.py is already installed, you can access the sensor by importing the SGP30 class from the grove.modules.sgp30 package.

    from grove.modules.sgp30 import SGP30
  4. Add the Seeed Studio repository to Raspberry Pi

    master

    To install dependencies via apt, you must first add the Seeed Studio repository to your Raspberry Pi's sources list.

    echo "deb https://seeed-studio.github.io/pi_repo/ stretch main" | sudo tee /etc/apt/sources.list.d/seeed.list
  5. Use the grove.py Command Line Interface (CLI)

    master

    After installing grove.py, you can interact with Grove devices directly from the terminal using commands prefixed with grove_.

    Argument Rules

    • I2C Devices: These use the default I2C bus (I2C 1 on Raspberry Pi). No additional arguments are typically required for the bus.
    • Digital I/O Devices: You must provide the digital pin number as an argument.
    • Analog Devices: You must provide the slot number (the n in An on the PCB sink).
    • PWM Devices: You must provide the PWM pin number.

    Permissions

    Some commands require root privileges. If you are not running as a root user, you must prefix the command with sudo if it is marked with a • in the reference table.

    Note: If you run a command without specifying a required argument, most commands will list the available arguments or pins.

    # Example: Using a digital button on pin 5
    sudo grove_button 5
    
    # Example: Using an analog light sensor on slot 0
    grove_light_sensor_v1_2 0
  6. Set up a Python 3 virtual environment

    master

    For later versions of Python 3, it is recommended to use a virtualenv for isolated package management to avoid conflicts with system packages.

    sudo apt install python3-virtualenv
    virtualenv -p python3 env
    source env/bin/activate
  7. Add the Seeed Studio public GPG key

    master

    Add the public GPG key to verify the repository packages. You can use either curl or the apt-key keyserver method.

    curl https://seeed-studio.github.io/pi_repo/public.key | sudo apt-key add -
    # or
    # sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BB8F40F3
  8. Configure Raspberry Pi Serial Port for Grove GPS Module

    master

    On newer Raspberry Pi models, the hardware serial port (/dev/ttyAMA0) is often reserved for Bluetooth, leaving the slower mini UART (/dev/ttyS0) for the GPIO pins. To ensure the Grove GPS module works correctly with the grove.py library, you must reassign the hardware serial to the GPIO pins by following these steps:

    1. Modify /boot/config.txt: Add or modify the following lines to disable Bluetooth and enable the hardware UART:

      dtoverlay=pi3-miniuart-bt
      dtoverlay=pi3-disable-bt
      enable_uart=1
    2. Modify /boot/cmdline.txt: Remove the two console statements (e.g., console=serial0,115200 and console=tty1) from the file.

      Example before: dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes root wait

      Example after: dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes root wait

    3. Reboot: A system reboot is required for these changes to take effect.

    dtoverlay=pi3-miniuart-bt
    dtoverlay=pi3-disable-bt
    enable_uart=1
  9. Install grove.py via online installation

    master

    For beginners or standard library users, use the online installation script.

    To install into a virtual environment, first activate your virtualenv and run:

    curl -sL https://github.com/Seeed-Studio/grove.py/raw/master/install.sh | bash -s -- --user-local --bypass-gui-installation

    To install into the system (requires sudo privileges), run:

    curl -sL https://github.com/Seeed-Studio/grove.py/raw/master/install.sh | sudo bash -s -
    curl -sL https://github.com/Seeed-Studio/grove.py/raw/master/install.sh | bash -s -- --user-local --bypass-gui-installation
  10. Install grove.py dependencies

    master

    Before installing from source, you may need to install system dependencies by adding the Seeed Studio repository and installing specific libraries.

    # Add repository
    echo "deb https://seeed-studio.github.io/pi_repo/ stretch main" | sudo tee /etc/apt/sources.list.d/seeed.list
    
    # Add GPG key
    curl https://seeed-studio.github.io/pi_repo/public.key | sudo apt-key add -
    sudo apt update
    
    # Install libraries
    sudo apt install libbmi088 libbma456
    echo "deb https://seeed-studio.github.io/pi_repo/ stretch main" | sudo tee /etc/apt/sources.list.d/seeed.list
    curl https://seeed-studio.github.io/pi_repo/public.key | sudo apt-key add -
    sudo apt update
    sudo apt install libbmi088 libbma456