Advertise simple light switch control using BLE characteristic?



  • Hey guys,

    I've read through Bluetooth example and API reference and tried to use the example at the bottom to advertise a value and let it be changed over bluetooth:

    from network import Bluetooth
    
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='LightSwitch', service_uuid=b'1234567890123456')
    
    def conn_cb (bt_o):
        events = bt_o.events()
        if  events & Bluetooth.CLIENT_CONNECTED:
            print("Client connected")
        elif events & Bluetooth.CLIENT_DISCONNECTED:
            print("Client disconnected")
    
    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
    
    
    srv1 = bluetooth.service(uuid=b'1234567890123456', isprimary=True)
    
    chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=5,  permissions = Bluetooth.PROP_READ | Bluetooth.PROP_WRITE)
    
    def char1_cb(chr):
        print("Write request with value = {}".format(chr.value()))
    
    char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=char1_cb)
    
    bluetooth.advertise(True)
    

    I'm using "nRF Connect" on android to test the service and connection simply fails.
    I also tried to use "BLE Scanner" (also on android), and that one can't connect to my service either.
    There's nothing printed in the SiPy's console.
    I'm on the latest version of SiPy - 1.6.7.b1

    I already have a working simple BLE service on Arduino that I want to port to SiPy:

    BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
    BLEService lightswitch = BLEService("FF10");
    BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("FF11", BLERead | BLEWrite);
    BLEDescriptor switchDescriptor = BLEDescriptor("2901", "Switch");
    BLECharCharacteristic stateCharacteristic = BLECharCharacteristic("FF12", BLENotify);
    BLEDescriptor stateDescriptor = BLEDescriptor("2901", "State");
    
    void setup() {
        Serial.begin(9600);
        
        Serial.println(F("Loaded"));
    
        pinMode(LED_PIN, OUTPUT);
        pinMode(BUTTON_PIN, INPUT);
        
        blePeripheral.setLocalName("Light Switch");
        blePeripheral.setDeviceName("Smart Light Switch");
        blePeripheral.setAdvertisedServiceUuid(lightswitch.uuid());
        blePeripheral.addAttribute(lightswitch);
        blePeripheral.addAttribute(switchCharacteristic);
        blePeripheral.addAttribute(switchDescriptor);
        blePeripheral.addAttribute(stateCharacteristic);
        blePeripheral.addAttribute(stateDescriptor);
        switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
        blePeripheral.begin();
        Serial.println(F("Smart Light Switch"));
    }
    

    Does the Bluetooth example work for you?
    Any advice in debugging why it can't connect?



  • Yes it did work, Thank you very much for your help.



  • Hi Grisk,

    I think this line is causing you problems:

    chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=5, permissions = Bluetooth.PROP_READ | Bluetooth.PROP_WRITE)

    PROP_READ and PROP_WRITE are Property attributes and not suitable for Permissions.

    For permissions I think there aren't any preset constants yet but you can use the ones from ESP-IDF directly.

    That line then becomes:

    chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=5, permissions = (1 << 0) | (1 << 4))

    With this change in place it's working for me but let us know how you get on.


Log in to reply
 

Pycom on Twitter