Driving BBMagic actors


Here are BBMagic actors:

  • BBMagic RELAY – has four ON/OFF outputs (BBM_RELAY_0..BBM_RELAY_3). It can switch on and off many devices.
  • BBMagic DIMMER – drives 230VAC devices by phase regulation.
  • BBMagic PWM – has three independent PWM (Pulse-Width Modulation) channels.

Here is the code of function that sets BBMagic DIMMER module. Every ten seconds changes device power from 25% to 100% and back to 25% and so on.

void dimmer_test(void)
{
    unsigned char bbm_buf[100] ;
    int i, bbm_id, counter ;

    printf("\n*** bbmagic_actors_test start : testing BBMagic DIMMER ***\n\n") ;
    i =bbm_bt_open(0, 0, 0, LIB_SHOW_CONFIG | LIB_SHOW_ACTORS) ;
    if(i) exit(1) ;
    bbm_dimmer_add("my_dimmer", "CBFD786BAA68", "2D8E75070812849C07ABF7C4382AEB3B") ;
    counter =80 ;
    do
    {
        counter++ ;
        bbm_id = bbm_bt_read(bbm_buf) ;
        switch( bbm_id )
        {
            case BBMAGIC_M_DIMMER:
                printf("REC_DATA_FROM_BBMAGIC_DIMMER\n") ;
            break ;
            default:
            break ;
        } ;
        if(counter ==100) bbm_dimmer_set("my_dimmer", 100) ;
        if(counter ==200)
        {
            counter =0 ;
            bbm_dimmer_set("my_dimmer", 25) ;
        }
        bbm_sleep_ms(100) ;
    }while(bbm_id != -1) ;
    bbm_bt_close() ;
    exit(0) ;
}

unsigned char bbm_buf[100]
Its a little buffer for data received from BBMagic actors.

int bbm_id
Its BBMagic modules ID. See more in ‘bbmagic_lib.h’ file.

int counter
Used for time measuring.

bbm_bt_open(0, 0, 0, LIB_SHOW_CONFIG | LIB_SHOW_ACTORS)
This function opens BBMagic Bluetooth Smart communication. For more see ‘bbmagic_lib.h’ file.

bbm_dimmer_add(..)
It adds BBMagic DIMMER to application database.

  • my_dimmer – device name. Its your choice.
  • CBFD786BAA68 – device address
  • 2D8E75070812849C07ABF7C4382AEB3B – device security key

Check below how to get device address and security key.

bbm_bt_read(bbm_buf)
Function for reading modules data. For more see ‘bbmagic_lib.h’ file.

bbm_dimmer_set(“my_dimmer”, 100)
It sets DIMMERs output power to 100%.

bbm_dimmer_set(“my_dimmer”, 25)
It sets DIMMERs output power to 25%.

bbm_sleep_ms(100)
It stops mains application loop for 100 milliseconds. Use it instead of usleep(), sleep(), etc.

bbm_bt_close()
This function closes BBMagic Bluetooth Smart communication.

How to read BBMagic actors address and sec key

To drive BBMagic actors you need to add them first to application database. Use functions:

  • bbm_relay_add(“name”, “address” , “key”) – adds BBMagic RELAY module
  • bbm_dimmer_add(“name”, “address” , “key”) – adds BBMagic DIMMER module
  • bbm_pwm_add(“name”, “address” , “key”) – adds BBMagic PWM module

All those functions get the same arguments: “name”, “address” , “key”. Device “name” is your choice but “address” and “key” you need to read from module. Do this:

1. run bbm_scanner_2 app
It is in tools directory in every downloaded project. You can get it from Download page too. So lets change permission if needed and launch it:

chmod +x ./tools/bbm_scanner_2
sudo ./tools/bbm_scanner_2

2. turn on BBMagic actors power
After switching the power on red LED blinks for a few seconds. In this period of time you must push modules button. Be careful if your device is connected to high voltage !! Push the button with some isolator stick.
BBMagic RELAY addr read

3. holding modules button
If you hold the button pressed for 1 second module sends address and sec key to bbm_scanner_2 app. Remember: BBMagic module must be near the Raspberry Pi SBC (1 or 2 meters). To stay confidential this information is sending with very low radio power.
bbm_scanner_2 application prints received data and now we can copy it respectively to bbm_pwm_add, bbm_dimmer_add or bbm_relay_add function.
BBMagic sec key

Only BBMagic actors: RELAY, DIMMER and PWM send addresses and sec keys.
There is no need to add BBMagic sensors to application database.

Do It Yourself

An example application ‘bbmagic_actors_test’ can communicate with all three types of BBMagic actors. It has relay_test(), dimmer_test(), pwm_test() functions for this purpose. To download it simply type:

wget http://bbmagic.net/download/src_2/bbmagic_actors_test.tar.gz
tar -zxvf bbmagic_actors_test.tar.gz
cd bbmagic_actors_test

We want to play with BBMagic DIMMER so comment other functions to disable them.

int main(void)
{
    //relay_test() ;
    dimmer_test() ;
    //pwm_test() ;
}

Read address and security key of BBMagic DIMMER and copy it like below:
BBMagic addres and key get
Edit ‘bbmagic_actors_test.c’ app file:

nano bbmagic_actors_test.c

… then paste address and sec key to ‘bbm_dimmer_add’ function: Close nano editor (Ctrl+x) saving file (Y).
BBMagic addres and key paste
Compile and run your app:

make
sudo ./bbmagic_actors_test

BBMagic DIMMER run
And its finished.
Raspberry Pi SBC comunicates with BBMagic DIMMER module and it drives the lamp.

BBMagic DIMMER lamp

Tagged , , , . Bookmark the permalink.

Comments are closed.