BBMagic RELAY demo C app


Here is how to do hardware preparation: BBMagic RELAY C app-hardware preparation
All project files ‘bbmagic_relay_sketch.tar.gz’ are avilable here: Download.

Let’s write

Little demo application that turns on and off relays channels (from RELAY_0 to RELAY_3).
BBMagic RELAY relays pins

Global declarations and definitions

    void relays(void) ;
    void proc_data(unsigned char *buf) ;

    int relay_time ;
    unsigned char rel_bd_addr[BBM_BT_ADDR_SIZE] = {0xE7, 0xBA, 0x08, 0x93, 0xC9, 0xD2} ;

relays(void)
Functions that turns on and off RELAY_0 to RELAY_3 channels.
proc_data(unsigned char *buf)
It process and displays data from BBMagic RELAY modules.
relay_time
Counts time for relays() function.
rel_bd_addr[BBM_BT_ADDR_SIZE]
Bluetooth MAC address of the module.

Main function

int main(void)
{
    unsigned char bbm_buf[BBLIB_FRAME_SIZE] ;
    int i, data_length ;

    i = bbm_bt_open(26, 19, 13, 1) ;
    if(i) exit(2) ;

    relay_time =0 ;
    do
    {
        relays() ;

        data_length = bbm_bt_read(bbm_buf) ;
        if(data_length > 0) proc_data(bbm_buf) ;

        usleep(100) ;
    }while(data_length != -1) ;

    bbm_bt_close() ;

    exit(0) ;
}
Learn more about bbmagic_lib_1.4 here: Here is the new bbmagic_lib_1.4 for Raspberry Pi.
relays() function

    int i ;
    relay_time++ ;
    if(relay_time == 10000)
    {
        //-turn relay off
        i = bbm_bt_relay_off(rel_bd_addr, BBM_RELAY_0 | BBM_RELAY_1 | BBM_RELAY_2 | BBM_RELAY_3) ;
        printf("\nsend: ALL RELAYS OFF\n") ;
    }
    if(relay_time == 20000)
    {
        //-turn relay on
        i = bbm_bt_relay_on(rel_bd_addr, BBM_RELAY_0) ;
        printf("\nsend: BBM_RELAY_0 ON\n") ;
    }
    if(relay_time == 30000)
    {
        //-turn relay on
        i = bbm_bt_relay_on(rel_bd_addr, BBM_RELAY_1) ;
        printf("\nsend: BBM_RELAY_1 ON\n") ;
    }
    if(relay_time == 40000)
    {
        //-turn relay on
        i = bbm_bt_relay_on(rel_bd_addr, BBM_RELAY_2) ;
        printf("\nsend: BBM_RELAY_2 ON\n") ;
    }
    if(relay_time == 50000)
    {
        relay_time =0 ;
        //-turn relay on
        i = bbm_bt_relay_on(rel_bd_addr, BBM_RELAY_3) ;
        printf("\nsend: BBM_RELAY_3 ON\n") ;
    }

Lets run it

Get with command: wget http://bbmagic.net/download/src/bbmagic_relay_sketch.tar.gz
BBMagic RELAY app pobierz
Unpack: tar -zxvf bbmagic_relay_sketch.tar.gz
BBMagic RELAY app rozpakuj
Go to the project directory: cd bbmagic_relay_sketch
Change permissions if needed: chmod 755 bbmagic_relay_sketch
BBMagic RELAY app chmod
Launch app to read address of your BBMagic RELAY module: sudo ./bbmagic_relay_sketch
BBMagic RELAY app uruchomienie
BBMagic RELAY app odczyt mac
Turn on BBMagic RELAY modules power and wait a second:
BBMagic RELAY mamy adres
Edit ‘bbmagic_relay_sketch.c’ file. You can use ‘nano editor’
nano bbmagic_relay_sketch.c
BBMagic RELAY app nano
Write your BBMagic modules address (I have module with address: E7BA0893C9D2):
unsigned char rel_bd_addr[BBM_BT_ADDR_SIZE] = {0xE7,0xBA,0x08,0x93,0xC9,0xD2};
BBMagic RELAY edycja adresu
Hit ‘ctrl+x’ then ‘y’ and ENTER to save changes and exit editor.
BBMagic RELAY app save
Compile project: make
BBMagic RELAY app kompilacja
And launch it: sudo ./bbmagic_relay_sketch
BBMagic RELAY app działa
BBMagic RELAY ledy

How does it work?

Here is hardware preparation for this app: BBMagic RELAY C app-hardware preparation
Tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *