
And now let’s write a little code using ‘bbmagic_lib’ library for Raspberry Pi.
Application files
Our project contains several files:
- bbmagic_lib_1.2.a – it is library for receiving data from BBMagic devices through Bluetooth Low Energy radio link. It’s for Raspberry Pi zero W and Raspberry Pi 3 which have Bluetooth LE chipset onboard.
- bbmagic_lib.h – it is bbmagic_lib header file containing definitions and constants
- libbluetooth.a – it is library from libbluetooth-dev Debian package that contains development files for using the BlueZ Linux Bluetooth library.
- bbmagic_motion_sketch.c – it is our main application ‘C’ file
- Makefile – it contains compilation instructions for make program
Project preparation
Make new directory for our project on your Raspberry Pi: mkdir bbmagic_motion_sketch
and go there:cd bbmagic_motion_sketch
Open your favorite file editor and if you have not one try nano – its simple and intuitive:nano bbmagic_motion_sketch.c
Writing app for BBMagic MOTION
1. Include header files required and put main function#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include "bbmagic_lib.h"
int main(void)
{
2. Variables declarationsunsigned char bbm_buf[BBLIB_FRAME_SIZE] ;
int i, adc1, adc2, data_length ;
float vcc_f ;
time_t timestamp ;
struct tm *loctime ;
char time_buffer[TIME_BUF_SIZE] ;;
- bbm_buf – buffer for BBMagic MOTION sensor data. Its size ‘BBLIB_FRAME_SIZE’ is defined in ‘bbmagic_lib.h’ file
- vcc_f – contains BBMagic MAGNETO power supply voltage
- adc1, adc2 – for samples from ADC_1 and ADC_2 inputs
- timestamp,*loctime, time_buffer[TIME_BUF_SIZE] – for local time operation
- i, data_length – data counters
3. Checking out bbmagic_lib library version
If this is 0x0102 version its ok – our app can play with it.
i = bbm_bt_lib_version() ;
printf("bbm_lib_version: %0.4X - ",i) ;
if(i == 0x0102) printf("ok\n") ;
else
{
printf("nok - stop\n\n") ;
exit(1) ;
}
4. Turn on BBMagic Bluetooth communication
i = bbm_bt_open(17) ;
if(i) exit(2) ;
THe bbm_bt_open(..) function gets pin number with LED connected. It indicates BBMagic Bluetooth transfer. Pin number should be in range from 1 to 27. If out of this range BBMagic Bluetooth transmission indication is off.
You can connect LED in two different ways:
do
{
data_length = bbm_bt_read(bbm_buf) ;
if(data_length > 0)
{
If data arrives it is in ‘bbm_buf’ buffer.
- data_length > 0 : received ‘data_length’ bytes of data from BBMagic sensor
- data_length == 0 : there is no data received
- data_length == -1 : user break by ‘ctrl+c’
- data_length == -10 : BBMagic data authentication error
All possible returned values are defined in ‘bbmagic_lib.h’ file.
6. Is received data from BBMagic MOTION?
switch(bbm_buf[BBMAGIC_DEVICE_TYPE])
{
case BBMAGIC_M_MOTION :
Constants ‘BBMAGIC_DEVICE_TYPE’ and ‘BBMAGIC_M_MOTION’ are defined in ‘bbmagic_lib.h’ file.
7. Displaying time
timestamp = time(NULL) ; //-Get the current time
loctime = localtime(×tamp) ; //-Convert it to local time representation
strftime (time_buffer, TIME_BUF_SIZE, "%X", loctime) ; //-Convert it to string
printf("\n %s ", time_buffer) ; //-Send to console
8. Checking motion detection flag
If the flag is set show information:if(bbm_buf[BBM_MOTION_FLAGS] & BBM_MOTION_ALERT_MASK)
{
printf(" OBJECT DETECTED !!") ;
}
Constants ‘BBM_MOTION_FLAGS’ and ‘BBM_MOTION_ALERT_MASK’ are defined in ‘bbmagic_lib.h’ header file.
9. Calculating and displaying the information from BBMagic MOTION
- Display module name:
printf("\n BBM_MOTION addr: ") ;
- And then six bytes of module address
for(i=0; i<BBM_BT_ADDR_SIZE; i++)
printf("%0.2X", bbm_buf[BBMAGIC_DEVICE_ADDR_5 + i]) ; - Calculate and display modules supply voltage
vcc_f = (float)bbm_buf[BBM_MOTION_V_SUP] ; vcc_f/=BBMAGIC_VCC_DIVIDER ;
printf(" < %0.2fV", vcc_f) ; - Display radio signal strength
printf(" | %ddBm", (signed char)bbm_buf[BBMAGIC_DEVICE_RSSI]) ;
- BBMagic MOTION firmware version:
printf(" | firm.%0.2X.%0.2X",bbm_buf[BBM_MOTION_FIRM_1], bbm_buf[BBM_MOTION_FIRM_0]) ;
printf(" >\n") ; - Show working time from power up (in seconds)
i =bbm_buf[BBM_MOTION_WORKTIME_3] ; i<<=8 ;
i |=bbm_buf[BBM_MOTION_WORKTIME_2] ; i<<=8 ;
i |=bbm_buf[BBM_MOTION_WORKTIME_1] ; i<<=8 ;
i |=bbm_buf[BBM_MOTION_WORKTIME_0] ;
printf(" %4ds",i) ;
- Light level, chip temperature and flags
printf(" L=%d chT=%d*C F=%0.2X", bbm_buf[BBM_MOTION_LIGHT], (signed char)bbm_buf[BBM_MOTION_CHIP_TEMP], bbm_buf[BBM_MOTION_FLAGS]) ;
- ADC_1 and ADC_2 inputs voltage
adc1 =bbm_buf[BBM_MOTION_ADC_1_MSB] ; adc1 *=256 ; adc1 +=bbm_buf[BBM_MOTION_ADC_1_LSB] ;
adc2 =bbm_buf[BBM_MOTION_ADC_2_MSB] ; adc2 *=256 ; adc2 +=bbm_buf[BBM_MOTION_ADC_2_LSB] ;
printf(" ADC1=%dmV ADC2=%dmV", adc1, adc2) ;
10. Close 'switch' statement
If there is received data from other BBMagic sensor lets show its address and type.
default:
printf("BBM_UNKNOWN_") ;
for(i=0; i<BBM_BT_ADDR_SIZE; i++) printf("%0.2X", bbm_buf[BBMAGIC_DEVICE_ADDR_5 + i]) ;
printf(" BBM_TYPE=%0.2X", bbm_buf[BBMAGIC_DEVICE_TYPE]) ;
break ;
} ;
11. Close 'if(data_length > 0)' statement
printf("\n") ;
}
12. Close 'do{' statement
Wait 100 microseconds and do receive loop again untill 'ctrl+c' is pressed (bbm_bt_read(..) function returns -1).
usleep(100) ;
}while(data_length != -1) ;
13. Close BBMagic Bluetooth communication
If 'ctrl+c' pressed the program ends.
bbm_bt_close() ;
exit(0) ;
}
And its done !!
Press 'ctrl+X' then 'Y' and hit 'Enter' to save bbmagic_motion_sketch.c file.
Compile project using Makefile conf file like this:
PRG =bbmagic_motion_sketch
BBM_LIB =bbmagic_lib_1.2
all: clean
gcc -o $(PRG) $(PRG).c $(BBM_LIB).a libbluetooth.a
clean:
rm -f $(PRG)
