Skip to main content

Extensive Arduino abstraction

Project description

pyduin

Pylint Yamllint Pytest and Coverage PyPI license PyPI download month PyPI version fury.io GitHub latest commit PyPI status

Pyduin is a Python wrapper for Arduino and other IOT devices such as ESP. It aims to support everything, that platformio supports. The following components are part of the package.

  • A python library
  • A firmware to be loaded onto the device
  • A command-line interface (to flash the firmware)

What for?

Pyduin makes it easy to interact with an Arduino or other IOT device from within Python. Once a device has the correct firmware applied, one can set pin modes, pin states, pwm values and more.

This makes it easy to wire a sensor, switch to an IOT device, connect it to a computer and start working with the sensor values in Python. The library supports:

  • Analog read and write
  • Digital read and write
  • PWM
  • Pin modes
  • OneWire (firmware works, not yet implemented in lib)
  • DHT Sensors (firmware works, not yet implemented in lib)
  • SPI (firmware works, not yet implemented in lib)
  • ...

Device support

In theory, any device supported by platformio can work. Currently, the following devices are supported

  • Arduino Uno
  • Arduino Nano
  • Sparkfun Pro Micro (ATMEGA32U4)

Installation

pyduin module

Only pip installs are available.

pip install pyduin

Dependency socat

Opening a serial connection resets most Arduinos. pyduin circumvents this drawback with a socat proxy.

To make meaningful use of the command-line features, the installation and usage of soact is recommended. To activate usage, edit ~/.pyduin.yml and set use_socat to yes (default).

serial:
  use_socat: yes

If socat is installed, a proxy will be started for every device that connections are made to. The pins get set up according to the pinfile and the initial modes get set on first connect. The following connections will not reset the Arduino. The proxy will stop safely on device disconnect. The proxy will also be stopped for flashing.

Usage

As python module

After installation the pyduin module can be imported.

from pyduin import arduino
from pyduin import _utils as utils

board = 'nanoatmega328'
boardfile = utils.board_boardfile(board)

Arduino = arduino.Arduino(board=board,
                          tty='/dev/ttyUSB0',
                          boardfile=boardfile,
                          wait=True)
print(Arduino.firmware_version)
pin = Arduino.get_pin(13)
pin.set_mode('OUTPUT')
pin.high()
print(Arduino.free_memory)

Command-line

The command-line interface provides a help page for all options and commands.

pyduin --help

Every positional argument that serves as a subcommand, has it's own help page.

pyduin firmware|pin|dependencies|versions --help

Most of the commands have shorter aliases. The following command sets the pwm value for pin 5 to 125.

pyduin --tty /dev/USB0 --baudrate 115200 --board nanoatmega328 pin 5 pwm 125

To connect to a device --tty and --board arguments are required.

Configuration file

Pyduin creates a configuration file in ~/.pyduin.yml from a template. This file contains some generic settings and the buddy list.

The buddy list

The buddy-list feature allows one to define known devices aka buddies. In ~/.pyduin.yml a dictionary of buddies can be declared.

buddies:
  uber:
    board: nanoatmega328 # as in platformio. required.
    tty: /dev/uber # required
    baudrate: 115200 # default derived from pinfile, optional
  under:
    board: uno
    tty: /dev/ttyUSB0

The buddies can be used in the command line interface.

pyduin -B uber pin 13 high

Default buddy

A default_buddy can be defined in the configuration file. This allows to target a device that is known and appropriately configured, without specifying the buddy option.

pyduin pin 13 high

Flashing firmware to the Arduino

pyduin --buddy uber firmware flash

It can also be done without the buddy list.

pyduin --board nanoatmega328 --tty=/dev/mytty fw f

Control the Arduinos pins

Using the command-line, the pins can be controlled as follows. The following command can be used to switch on and off digital pins.

pyduin --buddy uber pin 4 {high|low}

The pin mode can be set as follows

pyduin -B uber pin 4 mode {input|ouput|input_pullup,pwm}

A pin can also be read from. Resulting in 0 or 1 for digital pins and a value between 0 and 1024 (10bit) for analog pins. The analog pins also have aliases configured according to the Arduino conventions.

pyduin p A0 read

Control the builtin leds

The builtin leds defined in the pinfile can be addressed by their corresponding names

pyduin -B foo led1 {on|off}

Pyduin determines the correct read command in the background depending on the pins nature.

Get firmware version from the Arduino

pyduin --buddy uber firmware version [device|available]

Get free memory from the Arduino

pyduin --buddy uber free

Contribute

mkdir pyduindev && cd !$
git git@github.com:SteffenKockel/pyduin.git
virtualenv .
. bin/activate
pip install -e .

Pull requests welcome.

Add device

Adding a device works, by editing the ~/.pyduin/platformio.ini and and provide a pinfile. These files and folders gets created, when attempting to flash firmware. Changes made here are preserved. A device must also provide a pinfile. The name of the pinfile should have the name of the corresponding board name (as in platformio). When developing, the pinfile can just be added in the Repository structure. To test a pinfile while not in development mode, the -p option can be used.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyduin-0.6.4.tar.gz (32.3 kB view details)

Uploaded Source

Built Distribution

pyduin-0.6.4-py2.py3-none-any.whl (30.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyduin-0.6.4.tar.gz.

File metadata

  • Download URL: pyduin-0.6.4.tar.gz
  • Upload date:
  • Size: 32.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pyduin-0.6.4.tar.gz
Algorithm Hash digest
SHA256 3367adee79b632d212c98eecb937c50bac8ebb13a4ab95b120a0afa1a8f476ee
MD5 254a759fea67d7da722614ebac6c628d
BLAKE2b-256 d8ac33fa65fc2dc495720c6a78247c6fae128856da1664185fdd5f9de0da9416

See more details on using hashes here.

File details

Details for the file pyduin-0.6.4-py2.py3-none-any.whl.

File metadata

  • Download URL: pyduin-0.6.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pyduin-0.6.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 50ceb2d8724b8dcc63806b27c05b5b3db5b5fe1bc7b806e94987fa981af7ceeb
MD5 037c55a4fe389174a83d9bb545d93648
BLAKE2b-256 b0b5210debf2ed3b458cb4f88377fbef198e9d4b9a711100730ea33e4693ccf7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page