Get ALL repl data over BLE?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Defiance3D
Posts: 3
Joined: Wed Feb 17, 2021 6:53 pm

Get ALL repl data over BLE?

Post by Defiance3D » Wed Feb 17, 2021 7:10 pm

I have this code running that halfway works as repl over BLE. I can send commands over Bluetooth, but in order to get anything back I have to specifically send it with ble.send(). I tried looking at ble_uart_repl.py for guidance, but i just can't seem to get anything to work. Is there a simple modification to this code that will allow me to get all the repl data over Bluetooth vs just what i specifically send with ble.send?

Code: Select all

from machine import Pin, Timer
from time import sleep_ms
import ubluetooth

class BLE():


    def __init__(self, name):
        
        self.name = name
        self.ble = ubluetooth.BLE()
        self.ble.active(True)

        self.timer1 = Timer(0)
        self.timer2 = Timer(1)
        
        self.ble.irq(self.ble_irq)
        self.register()
        self.advertiser()


    def connected(self):
        
        self.timer1.deinit()
        self.timer2.deinit()
    

    def ble_irq(self, event, data):

        if event == 1:
         '''Central connected'''
         self.connected()
         
        
        elif event == 2:
            '''Central disconnected'''
            self.advertiser()
        
        elif event == 3:
            '''New message received'''
            
            buffer = self.ble.gatts_read(self.rx)
            message = buffer.decode('UTF-8')[:-1]
            exec(message)
            

            
    def register(self):
        
        # Nordic UART Service (NUS)
        NUS_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
        RX_UUID = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
        TX_UUID = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'
            
        BLE_NUS = ubluetooth.UUID(NUS_UUID)
        BLE_RX = (ubluetooth.UUID(RX_UUID), ubluetooth.FLAG_WRITE)
        BLE_TX = (ubluetooth.UUID(TX_UUID), ubluetooth.FLAG_NOTIFY)
            
        BLE_UART = (BLE_NUS, (BLE_TX, BLE_RX,))
        SERVICES = (BLE_UART, )
        ((self.tx, self.rx,), ) = self.ble.gatts_register_services(SERVICES)


    def send(self, data):
        self.ble.gatts_notify(0, self.tx, data + '\n')


    def advertiser(self):
        name = bytes(self.name, 'UTF-8')
        self.ble.gap_advertise(100, bytearray('\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name)
        
ble = BLE("Infinity-BT")

Online
User avatar
jimmo
Posts: 2243
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Get ALL repl data over BLE?

Post by jimmo » Thu Feb 18, 2021 12:57 am

Defiance3D wrote:
Wed Feb 17, 2021 7:10 pm
Is there a simple modification to this code that will allow me to get all the repl data over Bluetooth vs just what i specifically send with ble.send?
Not really a simple modification unfortunately, but the key thing from ble_uart_repl.py is that it dupterm's in order to get access to the REPL data (which it then sends to the BLE uart).

It's been a while since I tested it (although nothing should have changed) but does ble_uart_repl.py work on your board?
i.e. copy ble_uart_repl.py and ble_uart_peripheral.py to the board then

Code: Select all

>>> import ble_uart_repl
>>> ble_uart_repl.start()

Defiance3D
Posts: 3
Joined: Wed Feb 17, 2021 6:53 pm

Re: Get ALL repl data over BLE?

Post by Defiance3D » Thu Feb 18, 2021 11:24 am

I've never been able to get that to work. I tried to post the response I got but they disapproved my post because it contained links to pirated software? I guess I'm not allowed to post terminal responses on here. I hope I'm allowed to tell you that it fails at line 12 when I try that

Defiance3D
Posts: 3
Joined: Wed Feb 17, 2021 6:53 pm

Re: Get ALL repl data over BLE?

Post by Defiance3D » Thu Feb 18, 2021 1:49 pm

Nevermind, I got it to work. Thanks!

Post Reply