Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peripheral.py returns errors when commanding #2

Open
aofek opened this issue Jan 7, 2022 · 2 comments
Open

peripheral.py returns errors when commanding #2

aofek opened this issue Jan 7, 2022 · 2 comments

Comments

@aofek
Copy link

aofek commented Jan 7, 2022

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')

@jamestiotio
Copy link
Owner

jamestiotio commented Jan 7, 2022

Hi @aofek,

After seeing your error, it seems that you typed 'advertise' instead of advertise (without the quotation marks). The quotation marks in the script are to indicate that it is a string. However, when you enter the command in the console, it should be without quotation marks. Can you attempt this fix and check whether this solves the problem?

EDIT: Hmm the error on your second screenshot seems weird. I suspect the function signature has changed since I made this script. Some suggestions that I have:

  • Try changing the line bt.irq(bt_irq, _IRQ_ALL) to bt.irq(bt_irq) and see whether it works.
  • Check this out regarding an issue with self in MicroPython as well.
  • Maybe it's better for you to refer to the official MicroPython Bluetooth examples here instead? Over there, they should have the latest code that would work with the latest version of MicroPython. This code over here in this repository was used more than 2 years ago so things definitely have changed since then. This code is also quite "specific" to this project, whereas the code in the official MicroPython repository should be more suitable "generally".

Unfortunately, I currently do not possess any devices equipped with MicroPython anymore to reproduce and debug your errors. Hopefully, my pointers above are helpful enough to guide you in the correct direction for the solution.

@aofek
Copy link
Author

aofek commented Jan 7, 2022

Hi James
Thank you
It looks ok now
I am still in learning stage, but when in advertise, it reacts to a connect attempt from the phone.
When in scan, there is no error but it doesn't react to advertising ble around it, so i must dig in deeper.
Thank you very much James

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants