Sunday, May 16, 2021

Raspberry Pi Pico/CircuitPython + ESP32/adafruit nina-fw (act as WiFi/BLE Co-Processor)

adafruit nina-fw is a slight variant of the Arduino WiFiNINA core. ESP32 with nina-fw flashed, can be used as WiFi/BLE Co-Processor. This post show how to connected ESP32 to Raspberry Pi Pico, flash ESP32 firmware using Pico, and test WiFi/BLE with Pico/CircuitPython; on Raspberry Pi.

The ESP32 used here is a ESP32-S breadout board, flash with current latest version NINA_W102-1.7.3.bin.

Connection:

	ESP32	RPi Pico
	-----	--------
3.3V	3.3V	3.3V
GND	GND	GND
SCK	IO18	GP10
MISO	IO23	GP12
MOSI	IO14	GP11
CS	IO5	GP13
BUSY	IO33	GP14
RESET	EN	GP16
GPIO0	IO0	GP9
RX	RXD0	GP0 (tx)
TX	TXD0	GP1 (rx)

Install nina-fw on ESP32 using Raspberry Pi Pico:

In my practice , Raspberry Pi Pico act as a bridge to program ESP32. esptool is used to flash ESP32, read Install esptool on Raspberry Pi OS.

Visit https://github.com/adafruit/nina-fw/releases/latest, to download latest nina-fw.bin file, NINA_W102-1.7.3.bin currently.


- Download Pico-RP2040-Passthru.UF2
- Power up Pico in Bootloader mode
- Copy Pico-RP2040-Passthru.UF2 to Pico

- Disconnect and connect Pico again. Pico now act as a passthru bridge.
- Run the command to flash nina-fw to ESP32:

$ esptool.py --port /dev/ttyACM0 --before no_reset --baud 115200 write_flash 0 NINA_W102-1.7.3.bin

where:
- /dev/ttyACM0 is the usb port connect to Pico
- NINA_W102-1.7.3.bin is the downloaded nina-fw file.

Install CircuitPython firmware on Raspberry Pi Pico again

Download libraries:

Visit https://circuitpython.org/libraries to download the appropriate bundle for your version of CircuitPython.

Unzip the file and copy adafruit_airlift, adafruit_ble, adafruit_esp32spi folder and adafruit_requests.mpy to lib folder of Pico's CIRCUITPY driver.


Test CircuitPython code on Raspberry Pi Pico:

cpyPico_ESP32_WiFi.py

import board
import busio
from digitalio import DigitalInOut

from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests

print("Raspberry Pi RP2040 - ESP32SPI hardware test")

esp32_cs = DigitalInOut(board.GP13)
esp32_ready = DigitalInOut(board.GP14)
esp32_reset = DigitalInOut(board.GP16)

spi = busio.SPI(board.GP10, board.GP11, board.GP12)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
    print("ESP32 found and in idle mode")
print("Firmware vers.", esp.firmware_version)
print("MAC addr:", [hex(i) for i in esp.MAC_address])

for ap in esp.scan_networks():
    print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi']))

print("Done!")

ref:
Quickstart IoT - Raspberry Pi Pico RP2040 with WiFi

cpyPico_ESP32_BLE.py

import board
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_airlift.esp32 import ESP32

esp32 = ESP32(
    reset=board.GP16,
    gpio0=board.GP9,
    busy=board.GP14,
    chip_select=board.GP13,
    tx=board.GP0,     #connect to ESP32 RX
    rx=board.GP1,     #connect to ESP32 TX
)


adapter = esp32.start_bluetooth()

ble = BLERadio(adapter)
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)

while True:
    ble.start_advertising(advertisement)
    print("waiting to connect")
    while not ble.connected:
        pass
    print("connected: trying to read input")
    while ble.connected:
        # Returns b'' if nothing was read.
        one_byte = uart.read(1)
        if one_byte:
            print(one_byte)
            uart.write(one_byte)

ref:
Quickstart - Raspberry Pi RP2040 with BLE and CircuitPython

Tested with:
~ ESP32 running arduino-esp32 framework BLE UART Client


No comments: