Go Down

Topic: How to receive a string on BLE using CurieBLE (Read 2918 times) previous topic - next topic

pat-ase30

Hello everyone,

I'm new here, but use Arduino products since a few month for both personal and professional applications.

Today, I encounter a problem on an Arduino 101 communicating with a smartphone application, over BLE with CurieBLE library. I'm using several Services and typed Characteristics sucessfully, but for one of them, I need to exchange a data string (array of char) in both direction.

I could find how to write (from Arduino side) using the "BLECharacteristic()" method without type declaration, and adding a lenght. Then I write using "setValue()" specifiyng the lenght. That is Ok.

But I tried several methods to perform the opposite operation : Reading a string from the Characteristic.... I can't find any solution to be able to compile without any error. I tried severai type declarations, pointer, ... nothing is right !

Do someone already experiment this and have the solution ??

Some code extracts :
unsigned char MyString[16] ;
BLECharacteristic MyChar("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", BLERead | BLEWrite,16);
MyChar.setValue(MyString,16) ;

Thank you in advance for your help.

drmpf

Try downloading the free android app pfodDesignerV2
  

and generate a small menu for the Arduino101 it will give complete code for reading and write BLE characteristic.
http://www.forward.com.au/pfod/pfodDesigner/index.html  has some tutorials on using pfodDesignerV2

pat-ase30

Hello again,

Thank you for this idea, but my problem is not to write the characteristic from Android. The application I use let me write character arrays, but I can't read this characteristic on Arduino 101 side. I'm using the CurieBLE library as described in my previous message, but there is always errors avoiding me to upload the program.

Other ideas are welcome !

Patrick

pat-ase30

Hello again,

I'm please to close this topic with some explaination about this problem and ho I solved it.

In fact, since string manipulating functions and characteristic methods doesn't use the same variables types, I simply added some casting fnuctions to convert each variable types from char to/from unsigned char.

As an example :

//-------------------------------------------- Includes for BLE
#include <BLEAttribute.h>
#include <BLECentral.h>
#include <BLECharacteristic.h>
#include <BLECommon.h>
//#include <BLEDescriptor.h>
#include <BLEPeripheral.h>
#include <BLEService.h>
#include <BLETypedCharacteristic.h>
#include <BLETypedCharacteristics.h>
#include <BLEUuid.h>
#include <CurieBLE.h>

//----------------------------------------------------------- BLE declaring
BLEPeripheral blePeripheral; 
BLEService InitAdmin("41300000-EB90-09D7-0001-413031360000");     
BLECharacteristic Ssid("41300001-EB90-09D7-0001-413031360000", BLERead | BLEWrite,16);

//---------------------------------------------------------------------- Global Variables
int i;
char SsidLocal[32] ;

//----------------------------------------------------------------- Initialisation

void setup()
{
  Serial.begin(9600);
 
  // Set advertised local name and service UUID:
  blePeripheral.setLocalName("ASE_A016");
  blePeripheral.setDeviceName("ASE_A016");
  blePeripheral.setAppearance(true);
 
  // No visible service ?
  //blePeripheral.setAdvertisedServiceUuid(InitAdmin.uuid());   
 
  // Add Service & Caracteristic
  blePeripheral.addAttribute(InitAdmin);
  blePeripheral.addAttribute(Ssid);

  // Write into the SSID characteristic
  sprintf(SsidLocal,"AZERTYUIOPQSDFGH");
  Ssid.setValue((unsigned char *)SsidLocal,16) ;

  // begin advertising BLE service:
  blePeripheral.begin();

  Serial.println("A016 peripheral running..."); 
  Serial.println("");

}

//----------------------------------------------------------------------- Main loop
void loop()
{
    // listen for BLE peripherals to connect:
    BLECentral central = blePeripheral.central();
 
    // if a central is connected to peripheral:
    if (central)
    {
        // print the central's MAC address:
        Serial.print("Connected to central: ");
        Serial.println(central.address());
   
        // while the central is still connected to peripheral:
        while (central.connected())
        {
            if (Ssid.written())  // le SSID
            {
                Serial.print("Admin : SSID recu ");
                Serial.println(Ssid.valueLength());
                sprintf(SsidLocal,"%16c",NULL);
                strncpy(SsidLocal,(char*)Ssid.value(),Ssid.valueLength());
                Serial.println(SsidLocal);
            }
            delay(250);
        }
   
        // when the central disconnects, print it out:
        Serial.print("Disconnected from central: ");
        Serial.println(central.address());
        Serial.println();
   
    }
}


Have a nice day !
PAtrick

kosh09

this is good resource for me
it looks like u are trying to send string from arduino101

then which device receive data?

SEBASTIAN23

Buenas yo tengo el mismo problema.
Como hago que lea una cadena de caracteres (string) mandado por medio del BLE de la arduino 101.
Uso una apk hecha en appinventor.
mi codigo es asi.

#include <BLEAttribute.h>
#include <BLECentral.h>
#include <BLECharacteristic.h>
#include <BLECommon.h>
#include <BLEPeripheral.h>
#include < BLEService.h>
#include <BLETypedCharacteristic.h>
#include <BLETypedCharacteristics.h>
#include <BLEUuid.h>


#include <Servo.h>
#include <CurieBLE.h>

BLEPeripheral blePeripheral;
BLEService lightService("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);


const int lightPin = 13;
Servo myservo;  
Servo myservo1;  
Servo myservo2;  
Servo myservo3;  
char a;
String readString;




void setup()
{
 Serial.begin(9600);
 //Servo N° 0
 
 myservo.attach(2);  

 //Servo N° 1 (A)
 
 myservo1.attach(3);
 
 //Servo N° 2 (B)
 
 myservo2.attach(4);
 
 //Servo N° 3 (C)

 myservo3.attach(5);

myservo.write(8);
myservo1.write(100);
myservo2.write(164);
myservo3.write(90);
delay(10);

 blePeripheral.setLocalName("GENUINO 101");
 blePeripheral.setAdvertisedServiceUuid(lightService.uuid());
 blePeripheral.addAttribute(lightService);
 blePeripheral.addAttribute(switchCharacteristic);
 switchCharacteristic.setValue(0);
 blePeripheral.begin();
 
 Serial.println("BLE GENUINO 101.");
 
pinMode (lightPin,OUTPUT);
 
}

void loop() {
 BLECentral central = blePeripheral.central(); // Aguarda ser conectado por BLuetooth
if (central){ //Si se produce la conexión
 digitalWrite(lightPin,HIGH);
Serial.print("Conectado a la central: "); //muestra en el monitor serial
Serial.println(central.address());
while (central.connected()) { // se ejecuta mientras el dispositivo está conectado
if (switchCharacteristic.written()) { //Característica del interruptor ESCRITO
  a = switchCharacteristic.value(); //(Característica del interruptor VALOR)guardado en la variable estado

  if (a=='A'){
   motor1();
  }

 if(a=='B'){
   motor2();
 }

 if(a=='C'){
   motor3();
 }

 if(a=='D'){
   motor4();
 }
}
}
}else{
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());// Cuando se produce desconexión, muestra el mensaje
digitalWrite(lightPin,LOW);

}
}

void motor1(){
 delay(10);
 while (if (switchCharacteristic.written()) { //Característica del interruptor ESCRITO
  char c = switchCharacteristic.value(); //(Característica del interruptor VALOR)guardado en la variable estado
readString += c;
}
if(readString.length() >0){
 Serial.println(readString.toInt());
 myservo.write(readString.toInt());
 readString="";
}


void motor2(){
 delay(10);
 while (if (switchCharacteristic.written()) { //Característica del interruptor ESCRITO
  char c = switchCharacteristic.value(); //(Característica del interruptor VALOR)guardado en la variable estado
readString += c;
}
if(readString.length() >0){
 Serial.println(readString.toInt());
 myservo1.write(readString.toInt());
 readString="";
}



void motor3(){
 delay(10);
 while (if (switchCharacteristic.written()) { //Característica del interruptor ESCRITO
  char c = switchCharacteristic.value(); //(Característica del interruptor VALOR)guardado en la variable estado
readString += c;
}
if(readString.length() >0){
 Serial.println(readString.toInt());
 myservo2.write(readString.toInt());
 readString="";
}



void motor4(){
 delay(10);
 while (if (switchCharacteristic.written()) { //Característica del interruptor ESCRITO
  char c = switchCharacteristic.value(); //(Característica del interruptor VALOR)guardado en la variable estado
readString += c;
}
if(readString.length() >0){
 Serial.println(readString.toInt());
 myservo3.write(readString.toInt());
 readString="";
}

jepeto

Hello Patrick,

I thought your code would resolve my issue but I am encountering a very silly problem when reading a BLEcharacteristic string including a 0x00 byte (Null character) at a certain position since all the following bytes are set to 0x00 as well.

Do you know where this could come from?

jepeto

Hello,

I finally resolved it by using memcopy instead of strncopy. In such case it is possible to work with unsigned char and get the full string including 0's.

I hope this will help !

ballscrewbob

Could you guys posing sketches please use CODE TAGS to avoid the forum wrecking your sketches with emoticons etc.

They look like this " </> "
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Go Up