Skip to main content

Python library to interface with wireless/serial Micropython devices

Project description

uPydevice

Python library to interface with Micropython devices through WebREPL protocol or through Serial connection.

Requirements

upydev and pyserial will be automatically installed with pip

to install picocom do: brew install picocom

Install

pip install upydevice

Example usage:

WIRELESS DEVICE (WebREPL Protocol)

    >>> from upydevice import W_UPYDEVICE
# Setup and configurate a device :

    >>> esp32 = W_UPYDEVICE('192.168.1.56', 'mypass') # (target_ip, password)

# Send command:
    >>> esp32.cmd('led.on()')

    >>> esp32.cmd("uos.listdir('/')")
      ['boot.py', 'webrepl_cfg.py', 'main.py'] # this output is stored in [upydevice].output

    >>> esp32.output
      ['boot.py', 'webrepl_cfg.py', 'main.py']

    >>> esp32.cmd('foo()')

    >>> esp32.cmd('x = [1,2,3];my_var = len(x);print(my_var)')
    3

# Soft Reset:
    >>> esp32.reset()
    Rebooting device...

    ### closed ###

    Done!

SERIAL DEVICE (Picocom, Pyserial)

    >>> from upydevice import S_UPYDEVICE

# Setup and configurate a device :
    >>> esp32 = S_UPYDEVICE('/dev/tty.SLAB_USBtoUART', 1000, 115200) # defaults (serial_port, timeout=1000, baudrate=9600)

# Send command:
    >>> esp32.cmd('led.on()')

    >>> esp32.cmd("uos.listdir('/')")
    ['boot.py', 'webrepl_cfg.py', 'main.py'] # this output is stored in [upydevice].output

    >>> esp32.output
    ['boot.py', 'webrepl_cfg.py', 'main.py']

    >>> esp32.cmd('foo()')

    >>> esp32.cmd('x = [1,2,3];my_var = len(x);print(my_var)')
    3

# Soft Reset:
    >>> esp32.reset()
    Rebooting device...
    Done!

PYBOARD (Picocom, Pyserial)

    >>> from upydevice import PYBOARD

# Setup and configurate a device :
    pyboard = PYBOARD('/dev/tty.usbmodem3370377430372') # defaults (serial_port, timeout=1000, baudrate=9600)

# Send command:
    >>> pyboard.cmd('pyb.LED(1).toggle()',100)

    >>> pyboard.cmd("import uos;uos.listdir('/flash')")
    ['main.py', 'pybcdc.inf', 'README.txt', 'boot.py', '.fseventsd', '.Trashes'] # this output is stored in [upydevice].output

    >>> pyboard.output
    ['main.py', 'pybcdc.inf', 'README.txt', 'boot.py', '.fseventsd', '.Trashes']

    >>> pyboard.cmd('foo()')

    >>> pyboard.cmd('x = [1,2,3];my_var = len(x);print(my_var)')
    3

# Soft Reset:
    >>> pyboard.reset()
    Rebooting pyboard...
    Done!

GROUP (to send commands to several devices at a time)

# Setup and configurate the devices :
    >>> from upydevice import W_UPYDEVICE, PYBOARD, GROUP
# PYBOARD
    >>> pyboard = PYBOARD('/dev/tty.usbmodem387E386731342')
# ESP32
    >>> esp32_A = W_UPYDEVICE('192.168.1.53', 'keyespw')
    >>> esp32_B = W_UPYDEVICE('192.168.1.40', 'keyespw')

# Setup and configurate the group:
    >>> my_group = GROUP([esp32_A, esp32_B, pyboard])

# Each upydevice has a name attribute that can be set at creation momment or after
# pyboard = PYBOARD('/dev/tty.usbmodem387E386731342', name='my_pyboard_1'); or pyboard.name = 'my_pyboard_1')
# If not set an automatic name will be set as 'upydev_class'+'ip or serial port'

# Send command:
    >>> my_group.cmd('import machine;import  ubinascii;ubinascii.hexlify(machine.unique_id())')
    Sending command to wupydev_53
    b'30aea4233564'

    Sending command to wupydev_40
    b'807d3a809b30'

    Sending command to pyboard_tty.usbmodem387E386731342
    b'33004e000351343134383038'

# There is an option to silent the group messages with group_silent = True, and or each device ouput with device_silent=True

# Output is stored in group output attribute:
    >>> my_group.output
    {'wupydev_53': b'30aea4233564', 'wupydev_40': b'807d3a809b30', 'pyboard_tty.usbmodem387E386731342': b'33004e000351343134383038'}

# Send command parallel mode **: (experimental mode, may not work 100% of the times, depends on the connection quality (for wireless devices))
    >>> my_group.cmd_p('6*12')
    Sending command to: wupydev_53, wupydev_40, pyboard_tty.usbmodem387E386731342
    72


    72

    72

    Done!
# To see which ouput corresponds to which device use 'id=True' parameter:

    >>> my_group.cmd_p('ubinascii.hexlify(machine.unique_id())', id=True)
    Sending command to: wupydev_53, wupydev_40, pyboard_tty.usbmodem387E386731342
    pyboard_tty.usbmodem387E386731342:b'33004e000351343134383038'
    pyboard_tty.usbmodem387E386731342:
    pyboard_tty.usbmodem387E386731342:
    wupydev_40:b'807d3a809b30'
    wupydev_53:b'30aea4233564'
    wupydev_40:
    wupydev_53:
    Done!
    >>>
    >>> my_group.output
    {'wupydev_53': b'30aea4233564', 'wupydev_40': b'807d3a809b30', 'pyboard_tty.usbmodem387E386731342': b'33004e000351343134383038'}

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

upydevice-0.0.5.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

upydevice-0.0.5-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file upydevice-0.0.5.tar.gz.

File metadata

  • Download URL: upydevice-0.0.5.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.4

File hashes

Hashes for upydevice-0.0.5.tar.gz
Algorithm Hash digest
SHA256 ec09c32c549bb2363ced5abc2670d29c6595a2874a5a41a02defcf1cfd969cf7
MD5 55c255624e66d1ff574f23fe91fc6525
BLAKE2b-256 2f9608b4d7c0fdd42ab0bad573f1f5b360ad0b34824413cc9b9b2140124994b1

See more details on using hashes here.

File details

Details for the file upydevice-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: upydevice-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.4

File hashes

Hashes for upydevice-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f2e1af777bc534a50638dfebc7b42481ccdd8f328db3be81a3d138b78f706aed
MD5 393f65a8be6b96b8ebb043e1c4905023
BLAKE2b-256 f650bf64697fed9db0e53d468e92fb3271d47cf76c01649e3f4912623f96e402

See more details on using hashes here.

Supported by

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