Visualizzazione post con etichetta MicroPython. Mostra tutti i post
Visualizzazione post con etichetta MicroPython. Mostra tutti i post

giovedì 14 maggio 2020

MicroPython Bluetooth su Esp-Wroom-32

Ho provato a convertire la mia ESP32 DevKit V2 Esp-Wroom-32 per utilzzare Micropython



Per fare cio' per prima cosa si deve eliminare il firmware esistente

esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash

e poi flashare il firmware con MicroPython da questo link. Ne esistono due: il primo con il supporto completo indicato da V3 ed uno con il solo supporto BLE indicato da V4

esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 /home/luca/Downloads/esp32-idf4-20191220-v1.12.bin 

Al termine, per vedere se ha funzionato si puo' aprire un terminale seriale come Minicom. Attenzione che devono disabilitati Hardware Flow e Software Flow

minicom -s
CTRL+A+Z
Serial Port Setup  115200 8N1 
Hardware Flow NO 
Software Flow NO

ci si trova davanti al prompt REPL di Python 

al posto di scrivere sulla shell tramite Minicom si puo' effettuare l'upload di un file tramite

pip install adafruit-ampy

per copiare un file sulla scheda si usa
ampy --port=/dev/ttyUSB0 put ble_advertising.py 

mentre per eseguirlo
ampy --port=/dev/ttyUSB0 run ble_advertising.py 

i comandi completi sono i seguenti

  get    Retrieve a file from the board.
  ls     List contents of a directory on the board.
  mkdir  Create a directory on the board.
  put    Put a file or folder and its contents on the board.
  reset  Perform soft reset/reboot of the board.
  rm     Remove a file from the board.
  rmdir  Forcefully remove a folder and all its children from the board.
  run    Run a script and print its output.

(questi codici erano gia' stati visti con M5Stack)

per avere gli esempi di MicroPython si puo' clonare

git clone https://github.com/micropython/micropython


un altro comodo sistema per gestire il dispotivo e' RShell con cui si puo' interagire con  il  file system


root@debian:~# rshell --buffer-size=30 -p /dev/ttyUSB0
Using buffer-size of 30
Connecting to /dev/ttyUSB0 (buffer-size 30)...
Trying to connect to REPL  connected
Testing if ubinascii.unhexlify exists ... Y
Retrieving root directories ... /boot.py/ /ble_temperature.py/ /advertise.py/ /socialdistance.py/ /ble_temperature_central.py/ /scan.py/
Setting time ... May 12, 2020 19:08:12
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 2000
Welcome to rshell. Use Control-D (or the exit command) to exit rshell.
/root> 


gli script python si trovano nella directory /pyboard quindi basta fare 

cd /pyboard
ls

per osservarli 

edit nome_file.py

apre vi per permettere l'editing (l'editor non si trova sull'ESP32 ma sul computer)

per fare un scansione di dispositivi BLE con Micropython si puo' usare 

--------------------------------------------------------------------
import bluetooth
from micropython import const
import time

_IRQ_CENTRAL_CONNECT                 = const(1 << 0)
_IRQ_CENTRAL_DISCONNECT              = const(1 << 1)
_IRQ_GATTS_WRITE                     = const(1 << 2)
_IRQ_GATTS_READ_REQUEST              = const(1 << 3)
_IRQ_SCAN_RESULT                     = const(1 << 4)
_IRQ_SCAN_COMPLETE                   = const(1 << 5)
_IRQ_PERIPHERAL_CONNECT              = const(1 << 6)
_IRQ_PERIPHERAL_DISCONNECT           = const(1 << 7)
_IRQ_GATTC_SERVICE_RESULT            = const(1 << 8)
_IRQ_GATTC_CHARACTERISTIC_RESULT     = const(1 << 9)
_IRQ_GATTC_DESCRIPTOR_RESULT         = const(1 << 10)
_IRQ_GATTC_READ_RESULT               = const(1 << 11)
_IRQ_GATTC_WRITE_STATUS              = const(1 << 12)
_IRQ_GATTC_NOTIFY                    = const(1 << 13)
_IRQ_GATTC_INDICATE                  = const(1 << 14)


def bt_irq(event, data):
    if event == _IRQ_SCAN_RESULT:
        addr_type, addr, connectabt, rssi, adv_data = data
        print(data)
       
    elif event == _IRQ_SCAN_COMPLETE:
        print("scan complete")

bt = bluetooth.BLE()
bt.active(True) 
bt.gap_scan(10000, 10000, 10000)
bt.irq(bt_irq)

time.sleep(20)

--------------------------------------------------------------------

per creare un dispositivo che fa solo l'advertise in BLE

---------------------------------------------------------------------
import bluetooth
import struct
import time
from ble_advertising import advertising_payload
from micropython import const

_ENV_SENSE_UUID = bluetooth.UUID(0x181A)
_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768)


class BLETemperature:
    def __init__(self, ble, name="SocialDistance"):
        self._ble = ble
        self._ble.active(True)
        self._payload = advertising_payload(
            name=name, services=[_ENV_SENSE_UUID], appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER
        )
        self._advertise()
    def _advertise(self, interval_us=500000):
        self._ble.gap_advertise(interval_us, adv_data=self._payload)


def demo():
    ble = bluetooth.BLE()
    temp = BLETemperature(ble)
    while True:
print("Sociale")

if __name__ == "__main__":
    demo()
---------------------------------------------------------------------


martedì 8 ottobre 2019

M5Stack Fire





Per interagire con MicroPython direttamente su M5Stack Fire ci si puo' collegare via USB

Si collega il cavo, si apre un terminale seriale e si preme CTRL+C dopo aver premuto il tasto di reset.




Se si usa Minicom si deve disabilitare il controllo di flusso (altrimenti non viene lanciato CTRL+C)


A questo punto si possono inserire comandi Python mediante REPL

Per interagire con il filesystem si puo' usare ampy

ampy --port /dev/ttyUSB0 rm file.py  (cancella file)
ampy --port /dev/ttyUSB0 mkdir test  (crea dir)
ampy --port /dev/ttyUSB0 ls  (lista)
ampy --port /dev/ttyUSB0 put test.py  test/test.py  (carica un file dal computer al Fire)

Un altro metodo comodo per programmare e' usare VSCode (anche in Linux) usando il plugin m5stack-mpy (si installa usando le istruzioni a questo link)


Una volta installato il plugin si clicca Add M5Stack nella barra in basso e si seleziona la checkbox della porta seriale. Si apre quindi il menu a tendina M5Stack Device da cui si possono gestire anche la cancellazione e creazione dei file nel filesystem

Il metodo forse piu' potente per programmare M5Stack Fire e' usare la Arduino IDE
Si deve aggiungere tra le board il link

https://dl.espressif.com/dl/package_esp32_index.json

Si aggiunge poi la libreria M5Stack direttamente la library manager di Arduino IDE

Geologi

  E so anche espatriare senza praticamente toccare strada asfaltata