Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Advertise simple light switch control using BLE characteristic?

    Scheduled Pinned Locked Moved Discussion
    bluetooth
    3 Posts 2 Posters 1.4k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G Offline
      Grisk
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • J Offline
        jmarcelino
        last edited by jmarcelino

        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.

        1 Reply Last reply Reply Quote 2
        • G Offline
          Grisk
          last edited by

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

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom