Package: upydevice
uPydevicePython library to interface with MicroPython devices through REPL*:Websockets/WebREPL (WiFi)BleREPL (Bluetooth Low Energy)Serial REPL (USB)*Device REPL must be accesible.Upydevice allows to integrate device interaction from simple scripts to automated services,  test suites, command line interfaces or even GUI applications.Installpip install upydeviceorpip install --upgrade upydeviceExample usage:DEVICE:Device>>>fromupydeviceimportDeviceThis will return a Device based on address (first argument) type.e.g.Serial (USB)>>>esp32=Device('/dev/cu.usbserial-016418E3',init=True)>>>esp32SerialDevice@/dev/cu.usbserial-016418E3,Type:esp32,Class:SerialDeviceFirmware:MicroPythonv1.19.1-285-gc4e3ed964-dirtyon2022-08-12;ESP32modulewithESP32CP2104USBtoUARTBridgeController,Manufacturer:SiliconLabs(MAC:30:ae:a4:23:35:64)WiFi (WebSockets/WebREPL)This requiresWebREPLto be enabled in the device.>>>esp32=Device('192.168.1.53','mypass',init=True)>>>esp32WebSocketDevice@ws://192.168.1.53:8266,Type:esp32,Class:WebSocketDeviceFirmware:MicroPythonv1.19.1-285-gc4e3ed964-dirtyon2022-08-12;ESP32modulewithESP32(MAC:30:ae:a4:23:35:64,HostName:espdev,RSSI:-45dBm)If device hostname name is set e.g. (in devicemain.py)....wlan=network.WLAN(network.STA_IF)wlan.active(True)wlan.config(hostname='espdev')wlan.connect('your-ssid','your-key')...Now the device can be connected with mDNS name.>>>esp32=Device('espdev.local','mypass',init=True)>>>esp32WebSocketDevice@ws://192.168.1.53:8266,Type:esp32,Class:WebSocketDeviceFirmware:MicroPythonv1.19.1-285-gc4e3ed964-dirtyon2022-08-12;ESP32modulewithESP32(MAC:30:ae:a4:23:35:64,HostName:espdev,RSSI:-45dBm)BLE (BleREPL)This requiresBleREPLto be enabled in the device. (This is still experimental and performance may be platform and device dependent)>>>esp32=Device('00FEFE2D-5983-4D6C-9679-01F732CBA9D9',init=True)>>>esp32BleDevice@00FEFE2D-5983-4D6C-9679-01F732CBA9D9,Type:esp32,Class:BleDeviceFirmware:MicroPythonv1.18-128-g2ea21abae-dirtyon2022-02-19;4MB/OTABLEmodulewithESP32(MAC:ec:94:cb:54:8e:14,LocalName:espble,RSSI:-38dBm)Now the device can recive commands and send back the output. e.g.>>>esp32.wr_cmd('led.on()')>>>esp32.wr_cmd("os.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.wr_cmd('x = [1,2,3];my_var = len(x);print(my_var)')3# Soft Reset:>>>esp32.reset()Rebootingdevice...Done!Testing devices with Pytest:Undertestdirectory there are example tests to run with devices. This allows to test MicroPython code in devices interactively, e.g. button press, screen swipes, sensor calibration, actuators, servo/stepper/dc motors ...
e.g.$pytesttest_esp_serial.py-s===========================================testsessionstarts===========================================platformdarwin--Python3.7.9,pytest-7.1.2,pluggy-1.0.0benchmark:3.4.1(defaults:timer=time.perf_counterdisable_gc=Falsemin_rounds=5min_time=0.000005max_time=1.0calibration_precision=10warmup=Falsewarmup_iterations=100000)rootdir:/Users/carlosgilgonzalez/Desktop/IBM_PROJECTS/MICROPYTHON/TOOLS/upydevice_.nosync/test,configfile:pytest.iniplugins:benchmark-3.4.1collected7itemstest_esp_serial.py::test_devnamePASSED[14%]test_esp_serial.py::test_platform----------------------------------------------livelogcall----------------------------------------------00:13:26[pytest][sdev][ESP32]:RunningSerialDevicetest...00:13:26[pytest][sdev][ESP32]:DEVPLATFORM:esp3200:13:26[pytest][sdev][ESP32]:DEVPLATFORMTEST:[✔]PASSED[28%]test_esp_serial.py::test_blink_led----------------------------------------------livelogcall----------------------------------------------00:13:28[pytest][sdev][ESP32]:BLINKLEDTEST:[✔]PASSED[42%]test_esp_serial.py::test_run_script----------------------------------------------livelogcall----------------------------------------------00:13:28[pytest][sdev][ESP32]:RUNSCRIPTTEST:test_code.py00:13:31[pytest][sdev][ESP32]:RUNSCRIPTTEST:[✔]PASSED[57%]test_esp_serial.py::test_raise_device_exception----------------------------------------------livelogcall----------------------------------------------00:13:31[pytest][sdev][ESP32]:DEVICEEXCEPTIONTEST:b=1/000:13:31[pytest][sdev][ESP32]:DEVICEEXCEPTIONTEST:[✔]PASSED[71%]test_esp_serial.py::test_reset----------------------------------------------livelogcall----------------------------------------------00:13:31[pytest][sdev][ESP32]:DEVICERESETTEST00:13:32[pytest][sdev][ESP32]:DEVICERESETTEST:[✔]PASSED[85%]test_esp_serial.py::test_disconnect----------------------------------------------livelogcall----------------------------------------------00:13:32[pytest][sdev][ESP32]:DEVICEDISCONNECTTEST00:13:32[pytest][sdev][ESP32]:DEVICEDISCONNECTTEST:[✔]PASSED[100%]============================================7passedin6.74s============================================Made with upydevice:upydevJupyter_upydevice_kernelExamples (scripts, GUI ...)