Noob question: how to send data through bluetooth



  • Hi everyone, I would like to transmit light intensity measurement through bluetooth but I cannot understand how to do this since I'm a pycom noob and it is not very clear to me the mechanism, I think I lack some basics concepts.
    After some attemps I was able to connect to my phone and discover my Fipy using BLE Scanner app but I don't know how to continue.

    This is my code at the moment

    
    from network import Bluetooth
    import ubinascii
    import time
    import pycom
    from pycoproc_2 import Pycoproc
    import machine
    from LTR329ALS01 import LTR329ALS01
    
    
    pycom.heartbeat(False)
    pycom.rgbled(0x0A0A08) # white
    
    py = Pycoproc()
    
    pybytes_enabled = False
    if 'pybytes' in globals():
        if(pybytes.isconnected()):
            print('Pybytes is connected, sending signals to Pybytes')
            pybytes_enabled = True
    
    lt = LTR329ALS01(py)
    
    bt = Bluetooth()
    bt.set_advertisement(name='FraccaPy', service_uuid=b'1234567890123456')
    bt.advertise(True)
    
    if not bt.isscanning():
        bt.start_scan(-1)    # start scanning with no timeout
    
    
    print('Trying to establish connection with Nord')
    connection = bt.connect('ac5fea133f19')
    
    if (connection.isconnected()):
        print ('Successfully connected to Nord')
    
    # while True:
    #     time.sleep(1)
    #     # print read data on screen
    #     print("Light [channel Blue lux, channel Red lux]: " + str(lt.light()))
    #     # switch on led graduated in relation to external light
    #     red = lt.light()[1]*0.5
    #     blue = lt.light()[0]*0.5
    #     led = round (red + blue)
    #     pycom.rgbled(led << 8)
    #     # transmit data via Wi-Fi
    #     if(pybytes_enabled):
    #         pybytes.send_signal(1, lt.light())
    

    Any help would be greatly appreciated, thanks in advance.



  • @Gijs actually I solved my problem before seeing this , anyway it is very interesting.
    In particular, my problem was solved by removing

    if not bt.isscanning():
        bt.start_scan(-1)    # start scanning with no timeout
    
    
    print('Trying to establish connection with Nord')
    connection = bt.connect('ac5fea133f19')
    

    I found out these commands were not needed and they broke my code.



  • We recently provided an example for this here: https://docs.pycom.io/tutorials/networks/ble/callback/
    Let me know if that works for you



Pycom on Twitter