My Cart

How to Count BT Devices with LEDs Using Bluetooth Connectivity Kit

Difficulty Level: Beginner -

1. Introduction

This tutorial allows the user to turn ON different LEDs depending on the number of devices detected by the Bluetooth module.

Ingredients:

    - 1 x Bluetooth Connectivity Kit:
    • 1x Platform
    • 1x Bluetooth PRO
    • 1x XBee Shield
    • 6x LEDs
    • 6x 220Ω resistors
    • 1x Breadboard
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the Bluetooth Pro Shield, consult the main tutorial.

Step 1: Connection

Connect the XBee shield to Arduino and then connect Bluetooth PRO module or you can connect it directly to Raspberry Pi connection bridge. Connect the LEDs with the resistors in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to the VCC supply (5V in Arduino and 3.3V in Raspberry Pi) and ground.

Connect pin 3, 4, 5, 6, 7 and 8 to the longest leg of each LED with a resistor and connect the other leg of the LED to GND.

Step 2: The Code

Arduino:

Code:
/*  
 *  Bluetooth module PRO
 *  
 *  Copyright (C) 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 http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Luis Miguel MartĂ­
 */

byte pinleds[] = {3,4,5,6,7,8};

char error[] = "SYNTAX ERROR";
char devices[400];
char dev;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
  Serial.print("Configurando...");
  while (sendATcommand("AT", "OK", error, 1000) == 2);
  while (sendATcommand("SET BT AUTH * 0000", "", error, 1000) == 2);

  Serial.print("Iniciando...");
  delay(1000);
  for (byte i=0;i<6;i++){
  pinMode(pinleds[i],OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  memset(devices, NULL , 400);
  dev = askForDevices();
  Serial.print("Number of devices: ");
  Serial.println(dev);
  for (byte i=0;i<dev-48;i++){
    digitalWrite(pinleds[i],HIGH);
  }
  for(byte i=dev-48;i<6;i++){
    digitalWrite(pinleds[i],LOW);
  }
}

int8_t sendATcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  Serial.println(ATcommand);    // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        answer = 1;
        Serial.println("Expected response");
      } else if (strstr(response, error_answer) != NULL) {
        answer = 2;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}

char askForDevices () {

  Serial.println("Searching...");
  delay(500);
  while (Serial.available() > 0) {
    Serial.read();
  }
  delay(500);
  while (Serial.available() > 0) {
    Serial.read();
  }
  delay(500);
  while (Serial.available() > 0) {
    Serial.read();
  }
  Serial.println("INQUIRY 3");

  uint32_t time = millis();
  uint16_t i = 0;
  while (millis()-time<4000){
    
    while (Serial.available()>0){
      devices[i] = char(Serial.read());
      delay(10);
      i++;
    }
    
  }
  delay(1000);

  boolean go_on = true;
  uint16_t counter = 15;
  while (go_on) {
    
    if (devices[counter-15] == 'E') {
      if (devices[counter + 1-15] == 'R') {
        if (devices[counter + 2-15] == 'R') {
          if (devices[counter + 3-15] == 'O') {
            if (devices[counter + 4-15] == 'R') {
                  return '0';
            }
          }
        }
      }
    }
                
    
    if (devices[counter] == 'I') {
      if (devices[counter + 1] == 'N') {
        if (devices[counter + 2] == 'Q') {
          if (devices[counter + 3] == 'U') {
            if (devices[counter + 4] == 'I') {
              if (devices[counter + 5] == 'R') {
                if (devices[counter + 6] == 'Y') {
                  if (devices[counter + 7] == ' ') {
                    go_on = false;
                  } else counter++;
                } else counter++;
              } else counter++;
            } else counter++;
          } else counter++;
        } else counter++;
      } else counter++;
    } else counter++;
  }
  return devices[counter + 8];
}
        

Raspberry Pi:

Code:
/*  
 *  Bluetooth module PRO
 *  
 *  Copyright (C) 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 http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */
 
#include "arduPi.h"

byte pinleds[] = {3,4,5,6,7,8};

char error[] = "SYNTAX ERROR";
char devices[400];
char dev;

int8_t sendATcommand(const char* ATcommand, const char* expected_answer, const char* error_answer, unsigned int timeout);
char askForDevices();

void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);
  delay(1000);
  printf("Configurando...\n");
  while (sendATcommand("AT", "OK", error, 1000) == 2);
  while (sendATcommand("SET BT AUTH * 0000", "", error, 1000) == 2);

  printf("Iniciando...\n");
  delay(1000);
  for (byte i=0;i<6;i++){
  pinMode(pinleds[i],OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  memset(devices, 0 , 400);
  dev = askForDevices();
  printf("Number of devices: \n");
  printf("%c\n",dev);
  for (byte i=0;i<dev-48;i++){
    digitalWrite(pinleds[i],HIGH);
  }
  for(byte i=dev-48;i<6;i++){
    digitalWrite(pinleds[i],LOW);
  }
}

int8_t sendATcommand(const char* ATcommand, const char* expected_answer, const char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  Serial.println(ATcommand);    // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        answer = 1;
        printf("Expected response\n");
      } else if (strstr(response, error_answer) != NULL) {
        answer = 2;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}

char askForDevices() {

  printf("Searching...\n");
  delay(500);
  while (Serial.available() > 0) {
    Serial.read();
  }
  delay(500);
  while (Serial.available() > 0) {
    Serial.read();
  }
  delay(500);
  while (Serial.available() > 0) {
    Serial.read();
  }
  Serial.println("INQUIRY 3");

  uint32_t time = millis();
  uint16_t i = 0;
  while (millis()-time < 4000){
    
    while (Serial.available()>0){
      devices[i] =(Serial.read());
      delay(10);
      i++;
    }
    
  }
  delay(1000);
  boolean go_on = true;
  uint16_t counter = 15;
  while (go_on) {
    
    if (devices[counter-15] == 'E') {
      if (devices[counter + 1-15] == 'R') {
        if (devices[counter + 2-15] == 'R') {
          if (devices[counter + 3-15] == 'O') {
            if (devices[counter + 4-15] == 'R') {
                  return '0';
            }
          }
        }
      }
    }
                
    
    if (devices[counter] == 'I') {
      if (devices[counter + 1] == 'N') {
        if (devices[counter + 2] == 'Q') {
          if (devices[counter + 3] == 'U') {
            if (devices[counter + 4] == 'I') {
              if (devices[counter + 5] == 'R') {
                if (devices[counter + 6] == 'Y') {
                  if (devices[counter + 7] == ' ') {
                    go_on = false;
                  } else counter++;
                } else counter++;
              } else counter++;
            } else counter++;
          } else counter++;
        } else counter++;
      } else counter++;
    } else counter++;
  }
  return devices[counter + 8];
}

int main (){
	setup();
	while(1){
		loop();
	}
	return (0);
}

        

Intel Galileo:

Code:
/*  
 *  Bluetooth module PRO
 *  
 *  Copyright (C) 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 http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */
 
byte pinleds[] = {3,4,5,6,7,8};

char error[] = "SYNTAX ERROR";
char devices[400];
char dev;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);
  delay(1000);
  Serial.print("Configurando...");
  while (sendATcommand("AT", "OK", error, 1000) == 2);
  while (sendATcommand("SET BT AUTH * 0000", "", error, 1000) == 2);

  Serial.print("Iniciando...");
  delay(1000);
  for (byte i=0;i<6;i++){
  pinMode(pinleds[i],OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  memset(devices, NULL , 400);
  dev = askForDevices();
  Serial.print("Number of devices: ");
  Serial.println(dev);
  for (byte i=0;i<dev-48;i++){
    digitalWrite(pinleds[i],HIGH);
  }
  for(byte i=dev-48;i<6;i++){
    digitalWrite(pinleds[i],LOW);
  }
}

int8_t sendATcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  Serial1.println(ATcommand);    // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (Serial1.available() != 0) {
      response[x] = Serial1.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        answer = 1;
        Serial.println("Expected response");
      } else if (strstr(response, error_answer) != NULL) {
        answer = 2;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}

char askForDevices () {

  Serial.println("Searching...");
  delay(500);
  while (Serial1.available() > 0) {
    Serial1.read();
  }
  delay(500);
  while (Serial1.available() > 0) {
    Serial1.read();
  }
  delay(500);
  while (Serial1.available() > 0) {
    Serial1.read();
  }
  Serial1.println("INQUIRY 3");

  uint32_t time = millis();
  uint16_t i = 0;
  while (millis()-time<4000){
    
    while (Serial1.available()>0){
      devices[i] = char(Serial1.read());
      delay(10);
      i++;
    }
    
  }
  delay(1000);

  boolean go_on = true;
  uint16_t counter = 15;
  while (go_on) {
    
    if (devices[counter-15] == 'E') {
      if (devices[counter + 1-15] == 'R') {
        if (devices[counter + 2-15] == 'R') {
          if (devices[counter + 3-15] == 'O') {
            if (devices[counter + 4-15] == 'R') {
                  return '0';
            }
          }
        }
      }
    }
                
    
    if (devices[counter] == 'I') {
      if (devices[counter + 1] == 'N') {
        if (devices[counter + 2] == 'Q') {
          if (devices[counter + 3] == 'U') {
            if (devices[counter + 4] == 'I') {
              if (devices[counter + 5] == 'R') {
                if (devices[counter + 6] == 'Y') {
                  if (devices[counter + 7] == ' ') {
                    go_on = false;
                  } else counter++;
                } else counter++;
              } else counter++;
            } else counter++;
          } else counter++;
        } else counter++;
      } else counter++;
    } else counter++;
  }
  return devices[counter + 8];
}
        

Links and Documentation

Related Tutorials

Datasheets and Manuals



If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.

Know more at:

Get the Starter Kits at: