Arduino ble example with Bluegiga 112

Ask, and ye shall receive.
artlover5781
Posts: 9
Joined: 22:19, 28 Jan 2015

Arduino ble example with Bluegiga 112

Postby artlover5781 » 22:37, 28 Jan 2015

Hello All,

I would like to create an application for my ios device, using the Arduino ble example https://github.com/evothings/evothings-examples/tree/master/examples/arduino-ble. From what I have read this example only works with readbear shield, I currently am using the tinyduino ble shield with a tinyduino which is an arduino compatible device and is programmed within the Arduino IDE. This Shield uses a bluegiga 112 chip, is there support for this within the app and does anyone know how I would make it work? the bleshield and its info can be found: https://tiny-circuits.com/tiny-shield-bluetooth-low-energy.html and the code for the ble board will be found: https://tiny-circuits.com/learn/tinyshield-ble2

ardiri
Posts: 58
Joined: 16:13, 28 May 2014

Re: Arduino ble example with Bluegiga 112

Postby ardiri » 14:34, 31 Jan 2015

the following page would be a great start:

https://tiny-circuits.com/learn/tinyshield-ble2

ultimately; once you have the communication handlers written for the arduino - you simply need to know the respective UUID's to connect to from the mobile device; you can use a third party tool to identify what these UUID's are and experiment from that perspective. LightBlue (https://itunes.apple.com/us/app/lightbl ... 4780?mt=12) for Mac OSX works well - should be similar tools for Linux and Windows as well.
// Aaron
Chief Technology Officer
Evothings AB http://www.evothings.com/

artlover5781
Posts: 9
Joined: 22:19, 28 Jan 2015

Re: Arduino ble example with Bluegiga 112

Postby artlover5781 » 16:37, 31 Jan 2015

Hello Ardiri,

Thank you for your reply. I have already completed these steps. My ble shield connects to BLE discovery apllications like bluegiga etc. I have also found the respective service UUID and the TX RX UUID. The issue I am having is making the application work with my peripheral and the transmission of information between my arduino through BLE serial communication and the mobile application.

ardiri
Posts: 58
Joined: 16:13, 28 May 2014

Re: Arduino ble example with Bluegiga 112

Postby ardiri » 17:00, 31 Jan 2015

have you changed the UUID's in the evothings example?

https://github.com/evothings/evothings- ... app/app.js

specifically; lines 229, 233, 242 and 243? if the device supports serial communication over BLE this should be the only thing you need to do.
// Aaron
Chief Technology Officer
Evothings AB http://www.evothings.com/

artlover5781
Posts: 9
Joined: 22:19, 28 Jan 2015

Re: Arduino ble example with Bluegiga 112

Postby artlover5781 » 18:31, 31 Jan 2015

I am getting three sets of UUIDs from the BLE discovery applications:
Services UUID, TX UUID, RX UUID

I have changed the UUIDs in evothings app to my peripheral's UUIDS:
on lines 229, 234, 244 I have changed it to my services UUID what about line 247 what is the descriptor UUID?

Also I believe it might be an issue with my Arduino code, I am also experimenting with the chat example found https://github.com/don/BluetoothSerial/tree/master/examples/Chat. I am able to get this example to connect to my peripheral along with deploying it to my mobile ios device. I am having trouble with this as well with the ardunio code. Along with the standard arduino code to get the BLE shield working I have added 2 sensors and an accelerometer. I would like for the sensor values to be transmitted to the phone. here is my arduino code.



Code: Select all



//include The BLE files
#include <SoftwareSerial.h>
#include "BGLib.h"

#define DEBUG
//END include The BLE files

//include The Accelerometer files
#include <Wire.h>
#include "BMA250.h"

BMA250 accel;
// END include The Accelerometer files


// ================================================================
// BLE STATE TRACKING (UNIVERSAL TO JUST ABOUT ANY BLE PROJECT)
// ================================================================

// BLE state machine definitions
#define BLE_STATE_STANDBY           0
#define BLE_STATE_SCANNING          1
#define BLE_STATE_ADVERTISING       2
#define BLE_STATE_CONNECTING        3
#define BLE_STATE_CONNECTED_MASTER  4
#define BLE_STATE_CONNECTED_SLAVE   5

// BLE state/link status tracker
uint8_t ble_state = BLE_STATE_STANDBY;
uint8_t ble_encrypted = 0;  // 0 = not encrypted, otherwise = encrypted
uint8_t ble_bonding = 0xFF; // 0xFF = no bonding, otherwise = bonding handle

// ================================================================
// HARDWARE CONNECTIONS AND GATT STRUCTURE SETUP
// ================================================================

// NOTE: this assumes you are using one of the following firmwares:
//  - BGLib_U1A1P_38400_noflow
//  - BGLib_U1A1P_38400_noflow_wake16
//  - BGLib_U1A1P_38400_noflow_wake16_hwake15
// If not, then you may need to change the pin assignments and/or
// GATT handles to match your firmware.

#define LED_PIN         13  // Arduino Uno LED pin

#define GATT_HANDLE_C_RX_DATA   17  // 0x11, supports "write" operation
#define GATT_HANDLE_C_TX_DATA   20  // 0x14, supports "read" and "indicate" operations

// use SoftwareSerial on pins D3/D4 for RX/TX (Arduino side)
SoftwareSerial bleSerialPort(3, 4);

// create BGLib object:
//  - use SoftwareSerial por for module comms
//  - use nothing for passthrough comms (0 = null pointer)
//  - enable packet mode on API protocol since flow control is unavailable
BGLib ble112((HardwareSerial *)&bleSerialPort, 0, 1);

#define BGAPI_GET_RESPONSE(v, dType) dType *v = (dType *)ble112.getLastRXPayload()



// ================================================================
// ARDUINO APPLICATION SETUP AND LOOP FUNCTIONS
// ================================================================





void setup()
{
  Serial.begin(115200);
 
 
  Wire.begin();
  accel.begin(BMA250_range_2g, BMA250_update_time_32ms);
 
 
 
 
 
 
   // initialize status LED
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, LOW);
   
     // set up internal status handlers (these are technically optional)
    ble112.onBusy = onBusy;
    ble112.onIdle = onIdle;
    ble112.onTimeout = onTimeout;

    // ONLY enable these if you are using the <wakeup_pin> parameter in your firmware's hardware.xml file
    // BLE module must be woken up before sending any UART data
    //ble112.onBeforeTXCommand = onBeforeTXCommand;
    //ble112.onTXCommandComplete = onTXCommandComplete;

    // set up BGLib event handlers
    ble112.ble_evt_system_boot = my_ble_evt_system_boot;
    ble112.ble_evt_connection_status = my_ble_evt_connection_status;
    ble112.ble_evt_connection_disconnected = my_ble_evt_connection_disconnect;
    ble112.ble_evt_attributes_value = my_ble_evt_attributes_value;

    // open Arduino USB serial (and wait, if we're using Leonardo)
    // use 38400 since it works at 8MHz as well as 16MHz
    Serial.begin(38400);
    while (!Serial);

    // open BLE software serial port
    bleSerialPort.begin(38400);

    my_ble_evt_system_boot( NULL);
 
 
 

 
 
 
 
 
 
}


unsigned char buf[16] = {0};
unsigned char len = 0;

void loop()
{
 
 
 
//   delay(1000);       
//  int sensorValue1 = analogRead(A0);
//  delay(1000);       
//  int sensorValue2 = analogRead(A1);
//
//  // print out the value you read:
//  Serial.print("value1= " );
//  Serial.print(sensorValue1);
// 
//  Serial.print(" value2 = " );
//  Serial.println(sensorValue2);
 
 
 
//  accel.read();
//  //Print out the accelerometer data
//  Serial.print("x: ");
//  Serial.print(accel.X);
//  Serial.print(", y: ");
//  Serial.print(accel.Y);
//  Serial.print(", z:");
//  Serial.print(accel.Z);
//  Serial.print(",  t: ");   
//  Serial.print(accel.rawTemp/2 + 24);
//  Serial.println("degC");   
// 
//  delay(1000);
 
 
 
  if (bleSerialPort.available()) {
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);

 bleSerialPort.write(sensorValue1);
 bleSerialPort.write(sensorValue2);
 }
 
  //Send data to cordova app
 
// 
//  if ( bleSerialPort.available()) {
//    Serial.write("Them: ");
//    while ( bleSerialPort.available()) {
//      Serial.write( bleSerialPort.read());
//    }
//  }
// 
//  while (Serial.available()) {
//    unsigned char c = Serial.read();
//    if (c == 0xA || c == 0xD) { // \n or \r
//      sendData();
//    } else {
//      bufferData(c);
//    }
//  }
//}
//
//void bufferData(char c) {
//  if (len < 16) {
//    buf[len++] = c;
//  } // TODO warn, or send data
//}
//
//void sendData() {
//  Serial.write("Us: ");
//  for (int i = 0; i < len; i++) {
//     bleSerialPort.write(buf[i]);
//    Serial.write(buf[i]);
//  }
//  bleSerialPort.write(0xA);
//  Serial.write(0xA); // TODO test on windows
//  len = 0; 

 
 
 
    // END Send data to cordova app

 
 
 
  // keep polling for new data from BLE
    ble112.checkActivity();
   
    // blink Arduino LED based on state:
    //  - solid = STANDBY
    //  - 1 pulse per second = ADVERTISING
    //  - 2 pulses per second = CONNECTED_SLAVE
    //  - 3 pulses per second = CONNECTED_SLAVE with encryption
    uint16_t slice = millis() % 1000;
    if (ble_state == BLE_STATE_STANDBY) {
        digitalWrite(LED_PIN, HIGH);
    } else if (ble_state == BLE_STATE_ADVERTISING) {
        digitalWrite(LED_PIN, slice < 100);
    } else if (ble_state == BLE_STATE_CONNECTED_SLAVE) {
        if (!ble_encrypted) {
            digitalWrite(LED_PIN, slice < 100 || (slice > 200 && slice < 300));
        } else {
            digitalWrite(LED_PIN, slice < 100 || (slice > 200 && slice < 300) || (slice > 400 && slice < 500));
        }
    }
 
 
 }
 
 
 
 // ================================================================
// INTERNAL BGLIB CLASS CALLBACK FUNCTIONS
// ================================================================

// called when the module begins sending a command
void onBusy() {
    // turn LED on when we're busy
    //digitalWrite(LED_PIN, HIGH);
}

// called when the module receives a complete response or "system_boot" event
void onIdle() {
    // turn LED off when we're no longer busy
    //digitalWrite(LED_PIN, LOW);
}

// called when the parser does not read the expected response in the specified time limit
void onTimeout() {
    // reset module (might be a bit drastic for a timeout condition though)
}

// called immediately before beginning UART TX of a command
void onBeforeTXCommand() {
    // wake module up (assuming here that digital pin 5 is connected to the BLE wake-up pin)
   
    // give a bit of a gap between parsing the wake-up event and allowing the command to go out
    delayMicroseconds(1000);
}

// called immediately after finishing UART TX
void onTXCommandComplete() {
    // allow module to return to sleep (assuming here that digital pin 5 is connected to the BLE wake-up pin)
//    digitalWrite(BLE_WAKEUP_PIN, LOW);
}



// ================================================================
// APPLICATION EVENT HANDLER FUNCTIONS
// ================================================================

void my_ble_evt_system_boot(const ble_msg_system_boot_evt_t *msg) {
    #ifdef DEBUG
        Serial.print("###\tsystem_boot: { ");
        Serial.print("major: "); Serial.print(msg -> major, HEX);
        Serial.print(", minor: "); Serial.print(msg -> minor, HEX);
        Serial.print(", patch: "); Serial.print(msg -> patch, HEX);
        Serial.print(", build: "); Serial.print(msg -> build, HEX);
        Serial.print(", ll_version: "); Serial.print(msg -> ll_version, HEX);
        Serial.print(", protocol_version: "); Serial.print(msg -> protocol_version, HEX);
        Serial.print(", hw: "); Serial.print(msg -> hw, HEX);
        Serial.println(" }");
    #endif

    // system boot means module is in standby state
    //ble_state = BLE_STATE_STANDBY;
    // ^^^ skip above since we're going right back into advertising below

    // set advertisement interval to 200-300ms, use all advertisement channels
    // (note min/max parameters are in units of 625 uSec)
    ble112.ble_cmd_gap_set_adv_parameters(320, 480, 7);
    while (ble112.checkActivity(1000));

    // USE THE FOLLOWING TO LET THE BLE STACK HANDLE YOUR ADVERTISEMENT PACKETS
    // ========================================================================
    // start advertising general discoverable / undirected connectable
   // ble112.ble_cmd_gap_set_mode(BGLIB_GAP_GENERAL_DISCOVERABLE, BGLIB_GAP_UNDIRECTED_CONNECTABLE);
    //while (ble112.checkActivity(1000));

    // USE THE FOLLOWING TO HANDLE YOUR OWN CUSTOM ADVERTISEMENT PACKETS
    // =================================================================
#if 1
    // build custom advertisement data
    // default BLE stack value: 0201061107e4ba94c3c9b7cdb09b487a438ae55a19
    uint8 adv_data[] = {
        0x02, // field length
        BGLIB_GAP_AD_TYPE_FLAGS, // field type (0x01)
        BGLIB_GAP_AD_FLAG_GENERAL_DISCOVERABLE | BGLIB_GAP_AD_FLAG_BREDR_NOT_SUPPORTED, // data (0x02 | 0x04 = 0x06)
        0x11, // field length
        BGLIB_GAP_AD_TYPE_SERVICES_128BIT_ALL, // field type (0x07)
        0xe4, 0xba, 0x94, 0xc3, 0xc9, 0xb7, 0xcd, 0xb0, 0x9b, 0x48, 0x7a, 0x43, 0x8a, 0xe5, 0x5a, 0x19
    };

    // set custom advertisement data
    ble112.ble_cmd_gap_set_adv_data(0, 0x15, adv_data);
    while (ble112.checkActivity(1000));

    // build custom scan response data (i.e. the Device Name value)
    // default BLE stack value: 140942474c69622055314131502033382e344e4657
    uint8 sr_data[] = {
        0x14, // field length
        BGLIB_GAP_AD_TYPE_LOCALNAME_COMPLETE, // field type
        'M', 'y', ' ', 'A', 'r', 'd', 'u', 'i', 'n', 'o', ' ', '0', '0', ':', '0', '0', ':', '0', '0'
    };

    // get BLE MAC address
    ble112.ble_cmd_system_address_get();
    while (ble112.checkActivity(1000));
    BGAPI_GET_RESPONSE(r0, ble_msg_system_address_get_rsp_t);

    // assign last three bytes of MAC address to ad packet friendly name (instead of 00:00:00 above)
    sr_data[13] = (r0 -> address.addr[2] / 0x10) + 48 + ((r0 -> address.addr[2] / 0x10) / 10 * 7); // MAC byte 4 10's digit
    sr_data[14] = (r0 -> address.addr[2] & 0xF)  + 48 + ((r0 -> address.addr[2] & 0xF ) / 10 * 7); // MAC byte 4 1's digit
    sr_data[16] = (r0 -> address.addr[1] / 0x10) + 48 + ((r0 -> address.addr[1] / 0x10) / 10 * 7); // MAC byte 5 10's digit
    sr_data[17] = (r0 -> address.addr[1] & 0xF)  + 48 + ((r0 -> address.addr[1] & 0xF ) / 10 * 7); // MAC byte 5 1's digit
    sr_data[19] = (r0 -> address.addr[0] / 0x10) + 48 + ((r0 -> address.addr[0] / 0x10) / 10 * 7); // MAC byte 6 10's digit
    sr_data[20] = (r0 -> address.addr[0] & 0xF)  + 48 + ((r0 -> address.addr[0] & 0xF ) / 10 * 7); // MAC byte 6 1's digit

    // set custom scan response data (i.e. the Device Name value)
    ble112.ble_cmd_gap_set_adv_data(1, 0x15, sr_data);
    while (ble112.checkActivity(1000));

    // put module into discoverable/connectable mode (with user-defined advertisement data)
    ble112.ble_cmd_gap_set_mode(BGLIB_GAP_USER_DATA, BGLIB_GAP_UNDIRECTED_CONNECTABLE);
    while (ble112.checkActivity(1000));
#endif
    // set state to ADVERTISING
    ble_state = BLE_STATE_ADVERTISING;
}

void my_ble_evt_connection_status(const ble_msg_connection_status_evt_t *msg) {
    #ifdef DEBUG
        Serial.print("###\tconnection_status: { ");
        Serial.print("connection: "); Serial.print(msg -> connection, HEX);
        Serial.print(", flags: "); Serial.print(msg -> flags, HEX);
        Serial.print(", address: ");
        // this is a "bd_addr" data type, which is a 6-byte uint8_t array
        for (uint8_t i = 0; i < 6; i++) {
            if (msg -> address.addr[i] < 16) Serial.write('0');
            Serial.print(msg -> address.addr[i], HEX);
        }
        Serial.print(", address_type: "); Serial.print(msg -> address_type, HEX);
        Serial.print(", conn_interval: "); Serial.print(msg -> conn_interval, HEX);
        Serial.print(", timeout: "); Serial.print(msg -> timeout, HEX);
        Serial.print(", latency: "); Serial.print(msg -> latency, HEX);
        Serial.print(", bonding: "); Serial.print(msg -> bonding, HEX);
        Serial.println(" }");
    #endif

    // "flags" bit description:
    //  - bit 0: connection_connected
    //           Indicates the connection exists to a remote device.
    //  - bit 1: connection_encrypted
    //           Indicates the connection is encrypted.
    //  - bit 2: connection_completed
    //           Indicates that a new connection has been created.
    //  - bit 3; connection_parameters_change
    //           Indicates that connection parameters have changed, and is set
    //           when parameters change due to a link layer operation.

    // check for new connection established
    if ((msg -> flags & 0x05) == 0x05) {
        // track state change based on last known state, since we can connect two ways
        if (ble_state == BLE_STATE_ADVERTISING) {
            ble_state = BLE_STATE_CONNECTED_SLAVE;
        } else {
            ble_state = BLE_STATE_CONNECTED_MASTER;
        }
    }

    // update "encrypted" status
    ble_encrypted = msg -> flags & 0x02;
   
    // update "bonded" status
    ble_bonding = msg -> bonding;
}

void my_ble_evt_connection_disconnect(const struct ble_msg_connection_disconnected_evt_t *msg) {
    #ifdef DEBUG
        Serial.print("###\tconnection_disconnect: { ");
        Serial.print("connection: "); Serial.print(msg -> connection, HEX);
        Serial.print(", reason: "); Serial.print(msg -> reason, HEX);
        Serial.println(" }");
    #endif

    // set state to DISCONNECTED
    //ble_state = BLE_STATE_DISCONNECTED;
    // ^^^ skip above since we're going right back into advertising below

    // after disconnection, resume advertising as discoverable/connectable
    //ble112.ble_cmd_gap_set_mode(BGLIB_GAP_GENERAL_DISCOVERABLE, BGLIB_GAP_UNDIRECTED_CONNECTABLE);
    //while (ble112.checkActivity(1000));

    // after disconnection, resume advertising as discoverable/connectable (with user-defined advertisement data)
    ble112.ble_cmd_gap_set_mode(BGLIB_GAP_USER_DATA, BGLIB_GAP_UNDIRECTED_CONNECTABLE);
    while (ble112.checkActivity(1000));

    // set state to ADVERTISING
    ble_state = BLE_STATE_ADVERTISING;

    // clear "encrypted" and "bonding" info
    ble_encrypted = 0;
    ble_bonding = 0xFF;
}

void my_ble_evt_attributes_value(const struct ble_msg_attributes_value_evt_t *msg) {
    #ifdef DEBUG
        Serial.print("###\tattributes_value: { ");
        Serial.print("connection: "); Serial.print(msg -> connection, HEX);
        Serial.print(", reason: "); Serial.print(msg -> reason, HEX);
        Serial.print(", handle: "); Serial.print(msg -> handle, HEX);
        Serial.print(", offset: "); Serial.print(msg -> offset, HEX);
        Serial.print(", value_len: "); Serial.print(msg -> value.len, HEX);
        Serial.print(", value_data: ");
        // this is a "uint8array" data type, which is a length byte and a uint8_t* pointer
        for (uint8_t i = 0; i < msg -> value.len; i++) {
            if (msg -> value.data[i] < 16) Serial.write('0');
            Serial.print(msg -> value.data[i], HEX);
        }
        Serial.println(" }");
    #endif

    // check for data written to "c_rx_data" handle
    if (msg -> handle == GATT_HANDLE_C_RX_DATA && msg -> value.len > 0) {
        // set ping 8, 9, and 10 to three lower-most bits of first byte of RX data
        // (nice for controlling RGB LED or something)
        digitalWrite(8, msg -> value.data[0] & 0x01);
        digitalWrite(9, msg -> value.data[0] & 0x02);
        digitalWrite(10, msg -> value.data[0] & 0x04);
    }
}

ardiri
Posts: 58
Joined: 16:13, 28 May 2014

Re: Arduino ble example with Bluegiga 112

Postby ardiri » 10:33, 02 Feb 2015

i've got contact with the tinyduino guys and have requested a BLE module myself so we can look into this - i participated in their tinyscreen kickstarter and received some hardware via that.
// Aaron
Chief Technology Officer
Evothings AB http://www.evothings.com/

artlover5781
Posts: 9
Joined: 22:19, 28 Jan 2015

Re: Arduino ble example with Bluegiga 112

Postby artlover5781 » 17:51, 02 Feb 2015

YOU GUYS ARE AMAZING thank you!

Thea
Posts: 3
Joined: 10:19, 17 May 2015

Re: Arduino ble example with Bluegiga 112

Postby Thea » 10:21, 17 May 2015

Hallo together,

are there any news about this subject. I'm interested to do the same.
An evothings example with the Tinyduino BLE-Board would be Amazing.

Thank you!

Thea
Posts: 3
Joined: 10:19, 17 May 2015

Re: Arduino ble example with Bluegiga 112

Postby Thea » 21:18, 01 Jun 2015

Sorry for asking again.
But it would be so helpful to know if something is already planned, because i m absolutly new to app development.
I have some deadline, so otherwise i need to change my plans.

Please let me know if some Tinyduino Example is coming and if yes, around what time I can hope for it.

Thank you really much !


Return to “Questions and answers”

Who is online

Users browsing this forum: No registered users and 6 guests