This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Using BLEPeripheral and MySensors libraries

I am using BLEPeripheral and MySensors libraries and nRF51822 device(client side). I want to change functionality, if I press key of nRF51822 device it should start to advertise otherwise connect with another device (nodeMCU nad nRf24L01) act as a gateway. here client is connecting and communicating with the gateway and if I am pressing key communication is stopped but nRf51822 is not advertising. I am using Arduino platform. I tried separately both library it is working fine. please help how to resolve this issue.

Thank you

  • Hi,

    • What is the code doing when you press the key?
    • Could you share some code on what you are doing?
  • /* * Copyright (c) 2016 Intel Corporation. All rights reserved. * See the bottom of this file for the license terms. */ // Enable debug prints to serial monitor #define MY_DEBUG

        //#define MY_DEBUG_VERBOSE_SIGNING
        #define MY_RADIO_NRF5_ESB
        
        
        
        #include <BLEPeripheral.h>
        #include <MySensors.h>
        #include <SPI.h>
        
        #define CHILD_ID 3
        #define CHILD_LED 4
        //#define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
        // Change to V_LIGHT if you use S_LIGHT in presentation below
        MyMessage msg(CHILD_ID, V_LIGHT);
        MyMessage msgs(CHILD_LED, V_STATUS);
        
        BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
        BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
        
        // BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
        BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify);
        
        const int ledPin = 19; // pin to use for the LED
        int button = 18;
        static int count;
        int buttonstate;
        int ledPin1 = 20;
        
        
        void setup() {
          Serial.begin(115200);
        
          // set LED pin to output mode
          pinMode(ledPin, OUTPUT);
          pinMode(button, INPUT);
          pinMode(ledPin1,OUTPUT);
        
          // set advertised local name and service UUID:
          blePeripheral.setLocalName("Irayya");
          blePeripheral.setAdvertisedServiceUuid(ledService.uuid());
        
          // add service and characteristic:
          blePeripheral.addAttribute(ledService);
          blePeripheral.addAttribute(switchCharacteristic);
        
          // set the initial value for the characeristic:
          switchCharacteristic.setValue(0);
        
          // begin advertising BLE service:
          blePeripheral.begin();
        
          Serial.println("BLE LED Peripheral");
        }
        
        
        
        void loop() {
        
        
          buttonstate=digitalRead(button);
          if(buttonstate==HIGH)
          {
          
            
            //digitalWrite(ledPin,HIGH);
            digitalWrite(ledPin1,HIGH);
            Serial.println("Now button state is high");
           count=HIGH;                // using static variable to hold value because (pushbutton)
            //count=LOW;
            
          
          if(count==HIGH)
          {
          
          // listen for BLE peripherals to connect:
          Serial.println("Now count state is high");
          BLECentral central = blePeripheral.central();
        
          // if a central is connected to peripheral:
          if (central) {
            Serial.print("Connected to central: ");
            // print the central's MAC address:
            Serial.println(central.address());
            digitalWrite(ledPin,HIGH);
        
            // while the central is still connected to peripheral:
            while (central.connected()) {
              Serial.println("central is connected");
              // if the remote device wrote to the characteristic,
              // use the value to control the LED:
              if (switchCharacteristic.written()) {
                if (switchCharacteristic.value()) {   // any value other than 0
                  Serial.println("LED on");
                  digitalWrite(ledPin, HIGH);         // will turn the LED on
                } else {                              // a 0 value
                  Serial.println(F("LED off"));
                  digitalWrite(ledPin, LOW);          // will turn the LED off
                }
              }
            }
        
            // when the central disconnects, print it out:
            Serial.print(F("Disconnected from central: "));
            Serial.println(central.address());
            count=LOW;                              // after disconnect count should be low       
             Serial.println("Now count state is LOW");
            
            
          }
        }
          }
          
        if(count==LOW)
          {
           Serial.println("Else part");
          digitalWrite(ledPin, LOW); 
          present(CHILD_ID, S_LIGHT);
          present(CHILD_LED, S_TEMP);
          send(msg.set("LOW"));
          delay(3000);
        
        }
        }
        void receive(const MyMessage &message)
        {
           Serial.println("hello receive");
          String command = message.data;
         
          Serial.println(command);
          Serial.println("the msg type");
          Serial.println(message.type);
        //  if (message.type == V_STATUS) {
        //    Serial.println("RECV: ");
        //    String command = message.data;
        //    Serial.println(message.data);}
            if(command=="ON")
            {
               Serial.println("for enter to turn on led 1");
            digitalWrite(ledPin1,HIGH);
            }
            if(command=="OFF")
            {
              Serial.println("for enter to turn OFF led 1");
            digitalWrite(ledPin1,LOW);
            }
            send(msg.set("OK"));
            send(msg.set(message.data));
        
        }
    
  • Could be that you need to restart the advertising after the central disconnect / you press the key ?

    Maybe you can call blePeripheral.end(); and then call blePeripheral.begin(); ?

    You can also use the function blePeripheral.disconnect(); to disconnect if you the press the button.

  • Thanks Sir for your reply, the problem is our device is not able to advertise so how to connect central device. I asked a question in My Sensor forum they told you cannot access ble feature and we cannot use both api together like BLEPeripheral.h and MySensor.h. Someone told DFU bootloader conflicts with the NVM library, which is used to store the MySensors configuration. MySensor library API is never tested with a device which is having softdevice. please help me to resolve the memory issue.

  • Yes, according to this website this seems to be true.

    The Arduino port for nRF52 MCUs provided for Arduino Primo Boards is not compatible with MySensors at the moment. The reason is the existence of the SoftDevice handling BLE connections. MySensors can be compiled for the Primo board, but the NVM driver is incompatible. When Arduino Primo has an option to disable the SoftDevice, then MySensors can be used with the Arduino Primo variant. You can use the Arduino Primo variants with a custom board definition with the MySensors board definition.

    I'm not familiar with the MySensors library. But it seems that if you need to use this library, you will need to turn off the Bluetooth while you are using the library. For further help on this issue, you might have better luck asking in the MySensor forum and other forums for the Arduino platform.

Related