lopy4 Bluetooth low energy receiving data



  • I am a beginner in lopy
    I am trying to send pysense data from BLE to my Android phone.
    But I cannot receive my data, my code is as follows

    from network import Bluetooth
    import pycom
    import time
    from LIS2HH12 import LIS2HH12
    from pysense import Pysense
    
    
    
    py = Pysense()
    
    pycom.heartbeat(False)
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='WiPy', service_uuid=b'1234567890123456')
    
    def conn_cb (bt_o):
        events = bt_o.events()
        if  events & Bluetooth.CLIENT_CONNECTED:
            print("Client connected")
            pycom.rgbled(0x007f00) # green
        elif events & Bluetooth.CLIENT_DISCONNECTED:
            print("Client disconnected")
            pycom.rgbled(0x7f0000) # red
    
    li = LIS2HH12(py)
    apin = str(li.roll())
    
    
    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
    
    bluetooth.advertise(True)
    
    srv1 = bluetooth.service(uuid=b'1234567890123456', isprimary=True)
    
    chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=25)
    
    char1_read_counter = 0
    def char1_cb_handler(chr):
        global char1_read_counter
        char1_read_counter += 1
    
        events = chr.events()
        if  events & Bluetooth.CHAR_WRITE_EVENT:
            print("Write request with value = {}".format(value()))
        else:
            if char1_read_counter < 3:
                print('Read request on char 1')
            else:
                return 'ABC DEF'
    
    char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
    
    srv2 = bluetooth.service(uuid=1234, isprimary=True)
    
    chr2 = srv2.characteristic(uuid=4567, value=0x1234)
    char2_read_counter = 0xF0
    def char2_cb_handler(chr):
        global char2_read_counter
        char2_read_counter += 1
        if char2_read_counter > 0xF1:
            return char2_read_counter
    
    char2_cb = chr2.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char2_cb_handler)
    
    while True:
        time.sleep(10)
        dy = round(apin)
        print(dy)
        if char1_read_counter:
            chr1.value(dy)
    


  • @jcaron
    Now I just did a test, the value is the voltage
    I am making an APP that cannot accept data. I can see the received data using nrf, but there will be a string of codes in front of it.



  • @zhongyuan-ren what are you connecting/reading with, and happens exactly?


Log in to reply
 

Pycom on Twitter