Skip to content

peripheral.py returns errors when commanding  #2

@aofek

Description

@aofek

Dear James
using esp32 with your "peripheral.py" returns a message as in the pictures, while the "basic_ble.py" activates the advertising, connects and communicates with "serial bluetooth terminal" app on the phone.

I am searching for "notify a close-by ble device" which means using the esp32 periodically on scan/advertise mode without connection , preferably with rssi signal level upon detection.

could you help me , please, understand what is the nature of the error, so I can switch between the 2 modes with time sharing?
``
the esp32 dev board is a basic board, nothing fancy, "Lolin D32"

error message 1

error message 2

---code---- basic_ble.py

boot section

import esp
import esp32
import micropython
from micropython import const
import machine
from machine import Pin,I2C,SoftI2C,ADC,Timer
import utime
import time
from time import sleep_ms
import struct
import random
import network
import ubluetooth
from ubluetooth import BLE
adcamp1=ADC(Pin(32))
adcamp2=ADC(Pin(33))
adcamp3=ADC(Pin(34))
adcbat=ADC(Pin(35))
led=Pin(2,Pin.OUT) # pin 2 in devkit1
machine.freq(240000000)

#------------------------------------------

class BLE(): # BLE class
def init(self, name):
self.name = name
self.ble = ubluetooth.BLE()
self.ble.active(True)

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

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

def disconnected(self):        
    self.timer1.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: self.led(1))
    sleep_ms(10)
    self.timer2.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: self.led(0))   

def ble_irq(self, event, data):
    if event == 1:
        '''Central disconnected'''
        self.connected()
        self.led(1)
                
    elif event == 2:
        '''Central disconnected'''
        self.advertiser()
        self.disconnected()
    
    elif event == 3:
        '''New message received'''            
        buffer = self.ble.gatts_read(self.rx)
        message = buffer.decode('UTF-8').strip()
        print(message)            
        if message == 'led':
            led.value(not led.value())
            print('led', led.value())
            ble.send('led' + str(led.value()))
        if message=='hmc':
            magdat=adc_loop()
            ble.send(str(magdat))  

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):
    utime.sleep(0.001)
    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)

#----------------------------------------
def adc_loop():
amp1 =adcamp1.read()
amp1=amp1+1000
amp2 =adcamp2.read()
amp2=amp2+1000
amp3 =adcamp3.read()
amp3=amp3+1000
bat =adcbat.read()
bat=bat+1000
magdat=[amp1]+[amp2]+[amp3]+[bat]
print(magdat)
return magdat
#--------------------------------------------------------

if name == 'main':
ble = BLE('hmc001')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions