• Enter email to sit in the first row

Raspberry Pi writing BBMagic FLOOD application

Lets write application which:

  • checks out bbmagic_lib library compatibility using bbm_bt_lib_version() function
  • turns on BBMagic Bluetooth communication using bbm_bt_open(..) function
  • receives data from BBMagic module using bbm_bt_read(..) function
  • displays information from BBMagic FLOOD module on the screen
  • ends when ‘ctrl+c’ is pressed and closes BBMagic Bluetooth communication using bbm_bt_close() function

Application files

app for BBMagic FLOOD

  • 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_flood_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_flood_sketch
and go to it:cd bbmagic_flood_sketch
Open your favorite file editor and if you have not one try nano – its simple and intuitive:nano bbmagic_flood_sketch.c
BBMagic_FLOOD_sketch nano start

Writing app for BBMagic FLOOD

1. Include header files required and put main function#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "bbmagic_lib.h"
int main(void)

2. Variables declarationsunsigned char bbm_buf[BBLIB_FRAME_SIZE] ;
int i, data_length ;
float vcc_f ;

  • bbm_buf – buffer for BBMagic FLOOD sensor data. Its size ‘BBLIB_FRAME_SIZE’ is defined in ‘bbmagic_lib.h’ file
  • vcc_f – contains BBMagic FLOOD power supply voltage
  • i, data_length – needs for … you will see below

3. Checking out bbmagic_lib library compatibility
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") ;
printf("nok - stop\n\n") ;
exit(1) ;

4. Turn on BBMagic Bluetooth communication
i = bbm_bt_open(17) ;
if(i) exit(2) ;

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:
BBMagic RPI LED connection

5. Waiting for BBMagic data
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 FLOOD?

Constants ‘BBMAGIC_DEVICE_TYPE’ and ‘BBMAGIC_M_FLOOD’ are defined in ‘bbmagic_lib.h’ file.

7. Calculating and displaying basic information from BBMagic FLOOD sensor

  • Display module name:printf("BBM_FLOOD_") ;
  • And then six bytes of module Bluetooth addressfor(i=0; i<BBM_BT_ADDR_SIZE; i++)
    printf("%0.2X", bbm_buf[BBMAGIC_DEVICE_ADDR_5 + i]) ;
  • Calculate and display BBMagic FLOOD power supply voltagevcc_f = bbm_buf[BBM_FLOOD_V_SUP] ; vcc_f/=BBMAGIC_VCC_DIVIDER ;
    printf(" | %0.2fV", vcc_f) ;
  • Show device working time from power up (in seconds)i =bbm_buf[BBM_FLOOD_WORKTIME_3] ; i<<=8 ;
    i |=bbm_buf[BBM_FLOOD_WORKTIME_2] ; i<<=8 ;
    i |=bbm_buf[BBM_FLOOD_WORKTIME_1] ; i<<=8 ;
    i |=bbm_buf[BBM_FLOOD_WORKTIME_0] ;
    printf(" | %4d",i) ;
  • Display radio signal strength printf(" | %ddBm | ", (signed char)bbm_buf[BBMAGIC_DEVICE_RSSI]) ;

8. Check out flood alert
If the alert flag is active we display ‘FLOOD ALERT !!’ otherwise ‘NO FLOOD :-)’
else printf(" NO FLOOD :-)") ;
break ;

‘BBM_FLOOD_ALERT_FLAG’ and ‘BBM_FLOOD_ALERT_MASK’ constants are defined in ‘bbmagic_lib.h’ file of course.

9. Close ‘switch’ statement
If there is received data from other BBMagic sensor lets show its address and type.
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 ;
} ;

10. Close ‘if(data_length > 0)’ statement
printf("\n") ;

11. 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) ;

12. Close BBMagic Bluetooth communication
If ‘ctrl+c’ pressed the program ends.
bbm_bt_close() ;
exit(0) ;

And done. Great job !!

BBMagic_flood_sketch runs

You can download all project files as ‘bbmagic_flood_sketch.tar.gz’ archive from Download section.
Learn how to get and compile bbmagic_flood_sketch project

Tagged , , , , . Bookmark the permalink.

Comments are closed.