Development

Communication / Bluetooth Low Energy:

» BLE 09 Encrypted connection

This code set discoverable/connectable mode on Waspmote to allow connections from other BLE devices. Then wait during 30 seconds for incoming connections. If a connection is established, the connection is encrypted and then Waspmote keeps connected till the master ends the connection.

Required Materials

1 x Waspmote
1 x Battery
1 x Bluetooth Low Energy module
1 x Bluetooth module antenna
1 x Expansion board (if socket 1 is used)
1 x SD card
1 x BLE node to act as master (can be another Waspmote + BLE module or a smartphone).

Notes

Socket 0 is used by default
EEPROM is used in this example by default.
Never unplug module while Waspmote is turned ON.
Bluetooth module antenna should be connected.
The battery must be connected in ANY example.
Work for API V010 and later

Code

/*
 *  ------------------ [BLE_09] - Encrypted connection --------
 *
 *  Explanation: this code set discoverable/connectable mode on Waspmote to 
 *  allow connections from other BLE devices. Then wait during 30 seconds for 
 *  incoming connections. If a connection is established, the connection is 
 *  encrypted and then Waspmote keeps connected till the master ends the connection.
 *
 *  Copyright (C) 2014 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS ARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version:		0.2
 *  Design:		David Gascón
 *  Implementation:	Javier Siscart
 */

#include <WaspBLE.h>

// Variable to store function returning values
uint8_t flag = 0;


void setup() 
{
  USB.println(F("BLE_09 Example"));  

  // 0. Turn BLE module ON
  BLE.ON(SOCKET0);

}

void loop() 
{
  // 1. make Waspmote visible to other BLE modules
  BLE.setDiscoverableMode(BLE_GAP_GENERAL_DISCOVERABLE);

  // 2. make Waspmote connectable to any other BLE device
  BLE.setConnectableMode(BLE_GAP_UNDIRECTED_CONNECTABLE);
  USB.println(F("Waiting for incoming connections..."));

  /* 3. wait for connection status event during 30 seconds. connection status event structure:
   Field:   | Message type | Payload| Msg Class	| Msg ID | Connection | Flags |...
   Length:  |       1      |    1   |     1     |    1   |      1     |    1  |...
   Example: |      80      |   10   |     03    |   00   |      00    |   05  |...
   
   ...|   address    | address type | con interval | timeout | latency | bonding |
   ...|     6        |      1       |   2          |    2    |    2    |   1     |
   ...| bf9e78800700 |      00      |  3c00F       |  6400   |   0000  |   00    |
   */
  flag = BLE.waitEvent(30000);
  if (flag == BLE_EVENT_CONNECTION_STATUS)
  {
    USB.println(F("Connected!"));
    USB.println(F("Now Waspmote is connected as slave.")); 

    // 3.1 Parse the status event to find MAC of master device who initiated the connection.
    // NOTE: The event captured is stored in BLE.event array.
    uint8_t b = 5;
    for(uint8_t a = 0; a < 6 ; a++)
    {
      BLE.BLEDev.mac[a] = BLE.event[b+6];
      b--;
    }

    // 3.2 Print MAC of the Master device.
    USB.print(F("MASTER MAC Address: "));
    for(uint8_t i =0; i<6; i++)
    {
      USB.printHex(BLE.BLEDev.mac[i]);
    }
    USB.println();
    
    
    // 3.3 Encrypt the connection. Handle is 0 by default.
    
    if (BLE.encryptConnection(0) == 1)
    {
      USB.println(F("Connection encrypted"));
    }
    else
    {
      USB.println(F("Error encryting connection"));
    }
    
    // 3.4 now wait to other events forever. If disconnection is detected, exit loop.
    flag = 0;
    while(flag != BLE_EVENT_CONNECTION_DISCONNECTED)
    {

      // if disconnected event, then exist the loop and start again.
      // Possible disconnection event: 80 03 03 04 00 13 02
      flag = BLE.waitEvent(1000);

      if (flag != 0)
      {
        USB.println(F("Event found"));
        USB.printf("flag = %u\r\n",flag);
        
        //
        // NOTE: here the user can add custom actions depending on the received envent.
        //

      }
    }

    // 4. if here, disconnected.
    USB.println(F("Disconnected"));    

  }
  else 
  {
    if (flag == 0)
    {
      // If there are no events, then no one tried to connect Waspmote
      USB.println(F("No events found. No devices tried to connect Waspmote."));
    }
    else
    {
      // Other event received from BLE module
      USB.print(F("Other event found. "));
      USB.printf("flag = %u\r\n",flag);
    }
  }
  
  USB.println(); 
}

Output

D#
BLE_9 Example
Waiting for incoming connections...
Connected!
Now Waspmote is connected as slave.
MASTER MAC Address: CCC3EA6FA4B5
Connection encrypted
Disconnected

Quick Publish: