Package: pyduin
pyduinPyduin 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 libraryA firmware to be loaded onto the deviceA 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 writeDigital read and writePWMPin modesOneWire (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 supportIn theory, any device supported byplatformiocan work. Currently, the following devices are supportedArduino UnoArduino NanoSparkfun Pro Micro (ATMEGA32U4)Installationpyduin moduleOnlypipinstalls are available.pipinstallpyduinDependency socatOpening a serial connectionresets most Arduinos.pyduincircumvents this drawback with asocatproxy.To make meaningful use of the command-line features, the installation and usage ofsoactis recommended. To activate usage, edit~/.pyduin.ymland setuse_socattoyes(default).serial:use_socat:yesIfsocatis 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 connectionswill not reset the Arduino. The proxy will stop safely on device disconnect. The proxy will also be stopped for flashing.UsageAs python moduleAfter installation thepyduinmodule can be imported.frompyduinimportarduinofrompyduinimport_utilsasutilsboard='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-lineThe command-line interface provides a help page for all options and commands.pyduin --helpEvery positional argument that serves as a subcommand, has it's own help page.pyduin firmware|pin|dependencies|versions --helpMost 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 125To connect to a device--ttyand--boardarguments are required.Configuration filePyduin creates a configuration file in~/.pyduin.ymlfrom a template. This file contains some generic settings and the buddy list.The buddy listThe buddy-list feature allows one to define known devices aka buddies. In~/.pyduin.ymla dictionary of buddies can be declared.buddies:uber:board:nanoatmega328# as in platformio. required.tty:/dev/uber# requiredbaudrate:115200# default derived from pinfile, optionalunder:board:unotty:/dev/ttyUSB0The buddies can be used in the command line interface.pyduin -B uber pin 13 highDefault buddyAdefault_buddycan 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 highFlashing firmware to the Arduinopyduin --buddy uber firmware flashIt can also be done without the buddy list.pyduin --board nanoatmega328 --tty=/dev/mytty fw fControl the Arduinos pinsUsing 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 followspyduin -B uber pin 4 mode {input|ouput|input_pullup,pwm}A pin can also be read from. Resulting in0or1for digital pins and a value between0and1024(10bit) for analog pins. The analog pins also have aliases configured according to the Arduino conventions.pyduin p A0 readControl the builtin ledsThe builtin leds defined in the pinfile can be addressed by their corresponding namespyduin-Bfooled1{on|off}Pyduin determines the correct read command in the background depending on the pins nature.Get firmware version from the Arduinopyduin--buddyuberfirmwareversion[device|available]Get free memory from the Arduinopyduin--buddyuberfreeContributemkdir pyduindev && cd !$
git git@github.com:SteffenKockel/pyduin.git
virtualenv .
. bin/activate
pip install -e .Pull requests welcome.Add deviceAdding a device works, by editing the~/.pyduin/platformio.iniand and provide apinfile. These files and folders gets created, when attempting to flash firmware. Changes made here are preserved. A device must also provide apinfile. 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-poption can be used.