BT Pro 11: Sending frames

Create a transparent link with a remote Bluetooth module and send a frame with info

Required Materials

1 x Waspmote PRO 1 x Battery 2 x Bluetooth module Pro 2 x Bluetooth module antenna 1 x Expansion board (if socket 1 is used) 1 x Gateway

Notes

- Do not confuse Bluetooth sleep mode and Waspmote sleep modes. - Bluetooth module wakes up through UART. Take into account that if you send bytes through same socket where Bluetooth module is connected, It will be woken up. - Socket 1 will be used to connect Bluetooth module (using expansion board) - Socket 0 can also be used, plugging module directly. - It is recommended to set RTC time to your actual time. - SD card must be inserted to allow saving discovered devices. - Never unplug module while Waspmote is turned ON. - Bluetooth module antenna should be connected. - The battery must be connected in ANY example. - This example can be executed in Waspmote v12 and Waspmote v15

Code

/*  
 *  --------------- [BT PRO_11] - Sending frames -------- 
 *  
 *  Explanation: Create a transparent link with a remote Bluetooth 
 *  module and send a frame with info
 *      
 *  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 FOR A PARTICULAR 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 . 
 *  
 *  Version:		1.3
 *  Design:			David Gascón
 *  Implementation:	Javier Siscart
 */

#include "WaspBT_Pro.h"
#include "WaspFrame.h"

// Variable to store Mac address of remote Bluetooth module
char mac[18] = "00:07:80:6d:4d:3d";

void setup()
{

  USB.println(F("BT_PRO_11 Example"));

  // Turn On Bluetooth module
  BT_Pro.ON(SOCKET1);

}


void loop()
{

  ////////////////////////////////////////////////
  // 1. Create frame
  ////////////////////////////////////////////////  

  // 1.1 Create new frame
  frame.createFrame(ASCII, "WASPMOTE_BT");  

  // 1.2 Add frame fields
  frame.addSensor(SENSOR_STR, "Bluetooth frame");
  frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel()); 
  frame.showFrame();

  ////////////////////////////////////////////////
  // 2. Establish connection and send data
  ////////////////////////////////////////////////  

  // 2.1 Connect to the desired device
  USB.print(F("Scan for device:"));
  USB.println(mac);

  if(BT_Pro.scanDevice(mac,10,TX_POWER_6))
  {
    // 2.2 If found, make a transparent conenction.
    USB.println(F("Device found. Now connecting.."));

    if(BT_Pro.createConnection(mac))
    {
      // 2.3 If connected, send a dummy message.
      USB.println(F("Connected. Now sending data.."));
      if(BT_Pro.sendData(frame.buffer, frame.length)) 
      {
        USB.println(F("Data sent"));
      }

      // 2.4 Stay conencted 10 seconds.
      USB.println(F("Stay connected 10 seconds."));
      for(int i=0; i<10; i++) 
      {
        USB.print(F(".")); 
        BT_Pro.sendData(".");
        delay(1000);
        
      }

      // 2.5 Get Link received signal strength indicator (RSSI)
      BT_Pro.getRSSI(),     
      USB.print(F("Link RSSI: "));
      USB.println(BT_Pro.linkRSSI);

      // 2.6 End conneciton
      if(BT_Pro.removeConnection()) 
      {
        USB.println(F("Connection removed"));
      }
      else 
      {
        USB.println(F("Not removed"));
      }
    }
    else
    {
      USB.println(F("Not conencted"));
    }
  }
  else 
  {
    USB.println(F("Device not found"));
  }

  USB.println();

}

Output

H#
BT_PRO_11 Example
===============================
Current ASCII Frame:
Length: 57
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 32 35 34 38 39 32 31 23 57 41 53 50 4D 4F 54 45 5F 42 54 23 30 23 53 54 52 3A 42 6C 75 65 74 6F 6F 74 68 20 66 72 61 6D 65 23 42 41 54 3A 34 36 23
String: <=>€#382548921#WASPMOTE_BT#0#STR:Bluetooth frame#BAT:46#
===============================
Scan for device:00:07:80:6d:4d:3d
Device found. Now connecting..
Connected. Now sending data..
Data sent
Stay connected 10 seconds.
..........Link RSSI: -39
Connection removed

===============================
Current ASCII Frame:
Length: 57
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 32 35 34 38 39 32 31 23 57 41 53 50 4D 4F 54 45 5F 42 54 23 31 23 53 54 52 3A 42 6C 75 65 74 6F 6F 74 68 20 66 72 61 6D 65 23 42 41 54 3A 34 36 23
String: <=>€#382548921#WASPMOTE_BT#1#STR:Bluetooth frame#BAT:46#
===============================
Scan for device:00:07:80:6d:4d:3d
Device found. Now connecting..
Connected. Now sending data..
Data sent
Stay connected 10 seconds.
..........Link RSSI: -40
Connection removed


++++++++++++++++++++
GATEWAY OUTPUT:
++++++++++++++++++++
RING 0 00:07:80:6d:4d:5d 1 RFCOMM
<=>\0x80\0x02#382548921#WASPMOTE_BT#0#STR:Bluetooth frame#BAT:46#..........NO CARRIER 0 ERROR 0
RING 0 00:07:80:6d:4d:5d 1 RFCOMM
<=>\0x80\0x02#382548921#WASPMOTE_BT#1#STR:Bluetooth frame#BAT:46#..........NO CARRIER 0 ERROR 0 

Last updated