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

Parents
  • /* * 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));
        
        }
    
Reply
  • /* * 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));
        
        }
    
Children
No Data
Related