Blue Tooth Bee + Shield not supposed to send Ascii

Hey Guys,

This is my first time posting to the forums. Currently I am working on a project that involves sending data from a BlueTooth Bee Standalone(Master) to an uno with a Bluetooth Shield. Whenever i send a single character i always get the ascii numerical representation. For example when i want to send 1 i would get a 34 on the other side.

If i was only sending single characters i would just subtract 48, but im sending a number with decimals and i get other numbers.

Attached below is my code for both the receiver and the transmitter

Any help would be nice , if i need to provide any information ill be more than willing to do so.
BlueTooth Bee Standalone(Master)

#include <SoftwareSerial.h>                         // Software Serial Port

#define RxD         2
#define TxD         3

#define DEBUG_ENABLED  1

const int sensorPin = 3; // should be an analog in to read voltage

float newSensorVal;
float sensorVal = 0;

int sensorRate=200;//hz
int btRate=10;

long sensorInterval = 1000000.0/sensorRate;
long btInterval = 1000000.0/btRate;

double filterCoef = .95;

unsigned long prevSensorMicros = 0;
unsigned long prevBtMicros = 0;
unsigned long currentSensorMicros;
unsigned long currentBtMicros;


String retSymb   = "+RTINQ=";                       // start symble when there's any return
String slaveName = ";SeeedBTSlave";                 // caution that ';'must be included, and make sure the slave name is right.

int nameIndex = 0;
int addrIndex = 0;

String recvBuf;
String slaveAddr;
 int counter = 0;

String connectCmd = "\r\n+CONN=";
SoftwareSerial blueToothSerial(RxD,TxD);

void setup()
{
    Serial.begin(9600);
    pinMode(RxD, INPUT);
    pinMode(TxD, OUTPUT);
    
    setupBlueToothConnection();
    //wait 1s and flush the serial buffer
    delay(1000);
    Serial.flush();
    blueToothSerial.flush();
     
       
}
//code to send over
void loop()
{ 
//I commented this out to try and debug my code
/*  currentSensorMicros = micros();
  currentBtMicros = micros();
  if (currentSensorMicros - prevSensorMicros > sensorInterval) {
    prevSensorMicros = currentSensorMicros;
    newSensorVal = analogRead(sensorPin);
    //newSensorVal =newSensorVal*(3.3/1024.0);//what im currently reading
    //sensorVal = (filterCoef * sensorVal) + ((1.0 - filterCoef) * newSensorVal);
  }
  if (currentBtMicros - prevBtMicros > btInterval) {
    prevBtMicros = currentBtMicros;
    newSensorVal=newSensorVal*(3.3/1024.0);
    Serial.println(newSensorVal);
    blueToothSerial.print(newSensorVal);
  }  */
  //Reading Voltages through Blue Tooth bee and sending data to shield.
newSensorVal = analogRead(sensorPin);
// newSensorVal = newSensorVal*(3.3/1024.0);
Serial.print(newSensorVal);//multiplying 5/1024 to scale the number to voltage
 Serial.print(" * (3.3/1024.0) = ");

sensorVal = newSensorVal*(3.3/1024.0);
 Serial.println(sensorVal);
 blueToothSerial.print(newSensorVal);   

}


void setupBlueToothConnection()
{
    blueToothSerial.begin(38400);                               // Set BluetoothBee BaudRate to default baud rate 38400
    blueToothSerial.print("\r\n+STWMOD=1\r\n");                 // set the bluetooth work in master mode
    blueToothSerial.print("\r\n+STNA=SeeedBTMaster\r\n");       // set the bluetooth name as "SeeedBTMaster"
    blueToothSerial.print("\r\n+STAUTO=0\r\n");                 // Auto-connection is forbidden here
    delay(2000);                                                // This delay is required.
    blueToothSerial.flush();
    blueToothSerial.print("\r\n+INQ=1\r\n");                    //make the master inquire
    Serial.println("Master is inquiring!");
    delay(2000); // This delay is required.

    //find the target slave
    char recvChar;
    while(1)
    {
        if(blueToothSerial.available())
        {
            recvChar = blueToothSerial.read();
            recvBuf += recvChar;
            nameIndex = recvBuf.indexOf(slaveName);             //get the position of slave name
            
                                                                //nameIndex -= 1;
                                                                //decrease the ';' in front of the slave name, to get the position of the end of the slave address
            if ( nameIndex != -1 )
            {
                //Serial.print(recvBuf);
                addrIndex = (recvBuf.indexOf(retSymb,(nameIndex - retSymb.length()- 18) ) + retSymb.length());//get the start position of slave address
                slaveAddr = recvBuf.substring(addrIndex, nameIndex);//get the string of slave address
                break;
            }
        }
    }
    
    //form the full connection command
    connectCmd += slaveAddr;
    connectCmd += "\r\n";
    int connectOK = 0;
    Serial.print("Connecting to slave:");
    Serial.print(slaveAddr);
    Serial.println(slaveName);
    //connecting the slave till they are connected
    
    do
    {
        blueToothSerial.print(connectCmd);//send connection command
        recvBuf = "";
        while(1)
        {
            if(blueToothSerial.available()){
                recvChar = blueToothSerial.read();
                recvBuf += recvChar;
                if(recvBuf.indexOf("CONNECT:OK") != -1)
                {
                    connectOK = 1;
                    Serial.println("Connected!");
                    blueToothSerial.print("Connected!");
                    break;
                }
                else if(recvBuf.indexOf("CONNECT:FAIL") != -1)
                {
                    Serial.println("Connect again!");
                    break;
                }
            }
        }
    }while(0 == connectOK);
}

Blue Tooth Shield({Reciever/Slave)

#include <SoftwareSerial.h>   //Software Serial Port

#define RxD         6
#define TxD         7

#define DEBUG_ENABLED  1
 double newRecvChar;
SoftwareSerial blueToothSerial(RxD,TxD);
double recvChar;
void setup()
{
    Serial.begin(9600);
    pinMode(RxD, INPUT);
    pinMode(TxD, OUTPUT);
    
    setupBlueToothConnection();
}

void loop()
{
    while(1)
    {
        if(blueToothSerial.available() )
        {//check if there's any data sent from the remote bluetooth shield and print them
            //taking in the character
            recvChar = blueToothSerial.read();
            newRecvChar = recvChar* 3.3/1024.0;
            Serial.print(recvChar );//multiplying 5/1024 to scale the number to voltage
            Serial.print(" * (3.3/1024.0) = ");
            Serial.println(newRecvChar);
        }
    }
}

void setupBlueToothConnection()
{
    blueToothSerial.begin(38400);                           // Set BluetoothBee BaudRate to default baud rate 38400
    blueToothSerial.print("\r\n+STWMOD=0\r\n");             // set the bluetooth work in slave mode
    blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n");    // set the bluetooth name as "SeeedBTSlave"
    blueToothSerial.print("\r\n+STOAUT=1\r\n");             // Permit Paired device to connect me
    blueToothSerial.print("\r\n+STAUTO=0\r\n");             // Auto-connection should be forbidden here
    delay(2000);                                            // This delay is required.
    blueToothSerial.print("\r\n+INQ=1\r\n");                // make the slave bluetooth inquirable
    Serial.println("The slave bluetooth is inquirable!");
    delay(2000);                                            // This delay is required.
    blueToothSerial.flush();
}

Kyle:

Thanks for including your sample code. Although I don’t have a bluetooth pair to test this out, I believe the issue may be with the lines like this one:

blueToothSerial.print(newSensorVal);

In this case, blueToothSerial is an instance of SofwareSerial, so it inherits the methods associated with SoftwareSerial. I went to the reference for SoftwareSerial on the Arduino site ( arduino.cc/en/Reference/SoftwareSerialPrint ) and found the following example code, which I think may explain the problem you’re seeing:

// read the analog input on pin 0:
analogValue = analogRead(A0);

// print it out in many formats:
serial.print(analogValue); // print as an ASCII-encoded decimal
serial.print("\t"); // print a tab character

serial.print(analogValue/4, BYTE); // print as a raw byte value (divide the
// value by 4 because analogRead() returns numbers
// from 0 to 1023, but a byte can only hold values
// up to 255)

As you can see, the default serial.print() function prints characters in ASCII-encoded values. So, integer 0 becomes 0x30, integer 1 becomes 0x31, and so forth, just as you are seeing.

You have at least a few options:

  • break your sensor values into two BYTE values and recombine them on the receiving end
  • continue to send ASCII encoded values, and reinterpret them on the receiving end
  • Use the example code above, divide the analog value by 4, and send a single raw byte value

I suspect the easiest is to continue sending the sensor values in ASCII encoded form, and use the atoi or atol (ASCII to INT, ASCII to LONG) conversion functions on the receiving end. You can find some sample code on how to do that here:
inkling.com/read/arduino-co … recipe-2-9

You might also use the toInt method available from the String library. An example is shown in the article I mentioned above. Here’s a quick code snippet:

String aNumber = “1234”;
int value = aNumber.toInt();

And lastly, there is the parseInt() and parsefloat(). Those are documented here: arduino.cc/en/Reference/Stream

I hope this helps.

Best wishes.