Pages: 1 2 [3] 4 5   Go Down
Author Topic: Problem in receiving the data from Bluetooth module  (Read 4886 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If some data is available at the receiving terminal, the very first character that is read is stored in "inchar" (this is capable of storing only a single character right?). if the first character is the SOP, it sets index as 0 and clears the first element of array "inData" and sets the started and ended stuffs as true and false.
All true. Except that the reading happens in a while loop, so "the very first character" is a bit incorrect. It should be 'the next character".

Just to be pedantic, first can not be modified. Something can be very hot, where hot can take on shades of meaning from warm to hotter than the surface of the sun. Something is either first or it isn't. there are no degrees of first.

Quote
If it is an EOP, ended is made as true and loop is exited.
The while loop is exited, yes.

Quote
After detecting the SOP, and if data is still available, it again stores the value in "inchar" this time it is not an SOP and hence it checks for the "index" value. The index value was initialized wen the SOP was detected and hence now it'll be <199(in my case) and it stores the value in inData[0], then index is incremented and inData[1] is cleared to store the next new character.
This is true.

Quote
Finally if started and ended flags are true, it resets all the values.
After the while loop ends, either because the end of packet marker arrived or because there was no more data to read, the flag are tested, and, if both are true, then everything is reset.

You are expected, of course, to have used the data before you reset things. In your case, apparently, this means storing MAC IDs and dates on an SD card.

Quote
dtafile.print(inData) will store the data that was read initially right?
That will store the whole packet on the SD card. It would make more sense, to me, to parse the data, and store each device's information as a separate record.

But, first things first.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fine, I get the idea, Sir. And one more thing that I'd like to clarify is this one.
Quote
Quote
Having the bluetooth device connected to the same serial port that you use for debugging is clearly not working for you.
So I should use another way of viewing the incoming data other than the Serial monitor on arduino? I'm currently have  software named Flash magic and Realterm that helps in monitoring the serial port. So I should connect the Rxd of my device to the Txd of the arduino and the Txd of the device to the Rxd of the arduino and use another USB to UART converter and connect the txd, rxd pins of the module to it and
view the data separately on the software aforementioned.correct?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would think that you could use NewSoftSerial/SoftwareSerial to create a software serial port to talk to the bluetooth module, and use the hardware serial port to talk to the PC far more easily.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would think that you could use NewSoftSerial/SoftwareSerial to create a software serial port to talk to the bluetooth module, and use the hardware serial port to talk to the PC far more easily.

So should I be using  SoftwareSerial to create Rxd and Txd ports on pins(2 and 3) for example and connect the them to the bluetooth module. At the same time connect the original rxd and txd pins on the arduino uno (0 and 1) to the bluetooth module just to monitor the communication using arduino's serial monitor??
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So should I be using  SoftwareSerial to create Rxd and Txd ports on pins(2 and 3) for example and connect the them to the bluetooth module.
Yes.

Quote
At the same time connect the original rxd and txd pins on the arduino uno (0 and 1) to the bluetooth module just to monitor the communication using arduino's serial monitor??
No. There should be NO connection between the Arduino's pin 0 and 1 and the bluetooth module.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There should be NO connection between the Arduino's pin 0 and 1 and the bluetooth module.
In that case I'm not able to view anything on Ardunio's serial monitor. So I should  used a separate usb to uart converter and viewed the data on a serial monitor software like realterm?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In that case I'm not able to view anything on Ardunio's serial monitor.
Why the hell not? Plug the USB cable in, just like you program the thing.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is exactly what i did...But the serial monitor is just blank. This is my current code...
Code:
#define rxPin 2
#define txPin 3
#define SOP '+'
#define EOP '\r'
#include <SoftwareSerial.h>
#include <avr/wdt.h>
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
int count=0;
int i;

bool started = false;
bool ended = false;

char inData[200];
byte index;

void setup()
{  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
   mySerial.begin(9600);
   // Other stuff...
}

void readatinq()
{
  while(mySerial.available() > 0)
  {
    char inChar = mySerial.read();
    if(inChar == SOP)
    {
       mySerial.print("SOP detected");
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    { 
       count=count+1;
       if(count>=2)
       {mySerial.print("EOP detected");
       ended = true;
       break;
       }
    }
    else
    {
      if(index < 199)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

   mySerial.println('\n'); 

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
mySerial.print(inData);
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    count=0;
    inData[index] = '\0';
   }
}

void loop()
{
mySerial.print("Initializing");
  mySerial.print("===");
  delay(2000);
  mySerial.print("LLL");
   delay(2000);
  mySerial.print("\r\nAT+INQ\r\n");
  delay(1000);
   delay(25000);
  readatinq();
  delay(2000);
  wdt_enable(WDTO_15MS);
  wdt_reset();
 
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That is exactly what i did...But the serial monitor is just blank. This is my current code...
I'm not sure what the problem is. I tell you you need to do one thing, and you do something completely different.

You need two serial ports. One is used to talk to the bluetooth device. The other is used to talk to the PC.

Since the one that can talk to the PC is the one hardwired to the pins 0 and 1, and those pins are the ones utilized by the Serial instance, you need to use Serial to talk to the PC/Serial Monitor.

Since the hardware serial port is in use talking to the PC, it can not be used to talk to the bluetooth module. For that, you need a software serial port.

Look at your code. Some print() statements are intended for the serial monitor. Some are intended for the bluetooth module. You must use the correct instance in each case.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My bad! I forgot to include the "serial.begin" line in the code. So the corrected code is
Code:
#define rxPin 2
#define txPin 3
#define SOP '+'
#define EOP '\r'
#include <SoftwareSerial.h>
#include <avr/wdt.h>
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
int count=0;
int i;

bool started = false;
bool ended = false;

char inData[200];
byte index;

void setup()
{  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
   mySerial.begin(9600);
Serial.begin(9600);
   // Other stuff...
}
void readatinq()
{
  while(mySerial.available() > 0)
  {
    char inChar = mySerial.read();
    if(inChar == SOP)
    {
       Serial.print("SOP detected");
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    { 
       count=count+1;
       if(count>=3)
       {Serial.print("EOP detected");
       ended = true;
       break;
       }
    }
    else
    {
      if(index < 199)
      {
        inData[index] = inChar;
       
        index++;
        inData[index] = '\0';
      }
    }
  }

   Serial.println('\n'); 

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
Serial.print(inData);
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    count=0;
    inData[index] = '\0';
   }
}

void loop()
{
Serial.print("Initializing");
 Serial.print("==="); //just for us to view which statement is being executed
  mySerial.print("===");
  delay(100);
 
  delay(2000); 
 Serial.print("LLL");
  mySerial.print("LLL");
  delay(100);
 
   delay(2000);
   Serial.print("\r\nAT+INQ\r\n");
  mySerial.print("\r\nAT+INQ\r\n");
  delay(1000);
 
  delay(25000);
  readatinq();
  delay(2000);
  wdt_enable(WDTO_15MS);
  wdt_reset();
 
}

If I do not include the "count" part in the code the output I get is
Quote
Initializing
===

LLL

AT+INQ

EOP detected


If I include
Code:
if(count>=2)
       {Serial.print("EOP detected");
       ended = true;
       break;
       }
I get this output
Quote
Initializing
===

LLL

AT+INQ


SOP detectedEOP detected

INQ:D4:C1:FC:3A:56:7C,"C1-01"

If I use >=3 all I get is

Quote
===

LLL

AT+INQ


SOP detected

But I noticed something weird. During the search time after the AT+INQ command (25 seconds delay), I close and reopen the Serial monitor, The code restarts as always but in between the LLL and === commands I unexpectedly get the output of all the detected devices!! but from the next iteration I get the same old erroneous output..
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

    else if(inChar == EOP)
    { 
       Serial.println("EOP marker read");
       count=count+1;
       Serial.print("count: ");
       Serial.println(count);

       if(count>=3)
       {
          Serial.print("EOP detected");
          ended = true;
          break;
       }
    }

Try adding the highlighted stuff.
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I get this output:

Quote
Initializing===

LLL

AT+INQ


EOP marker read
count: 1
SOP detectedEOP marker read
count: 2


Initializing===Initializing===

LLL

AT+INQ


EOP marker read
count: 1
SOP detectedEOP marker read
count: 2


Initializing===Initializing===ÿK


LLLD4:C1:FC:3A:56:7C,"C1-01"
DONE

OK


AT+INQ


EOP marker read
count: 1
SOP detectedEOP marker read
count: 2


Initializing===Initializing===úK


LLLD4:C1:FC:3A:56:7C,"C1-01"
DONE

OK


AT+INQ


EOP marker read
count: 1
SOP detectedEOP marker read
count: 2
EOP marker read
count: 3
EOP detected

INQ:70:05:14:91:2C:C9,"LG-P500"
D4:C1:FC:3A:56:7C,"C1-01"Initializing===Initializing===

LLL

AT+INQ


EOP marker read
count: 1
SOP detectedEOP marker read
count: 2


Initializing===Initializing===


LLLD4:C1:FC:3A:56:7C,"C1-01"
DONE

OK


AT+INQ


EOP marker read
count: 1
SOP detectedEOP marker read
count: 2


Initializing===Initializing===

LLL

AT+INQ


EOP marker read
count: 1
SOP detectedEOP marker read
count: 2

Only The result of the 4th iteration is correct.
Is it like there occurs a delay in printing the values that were received? because there are no outputs for the first two iterations and as soon as the third iteration begins, one device's MAC ID is printed tat too abruptly after the Initialization command...the "DONE OK" are the statements that usually occur after printing the entire output. Everything seems so jumbled up! smiley-sad
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to learn the difference between Serial.print() and Serial.println().
Quote
EOP marker read
count: 1
SOP detectedEOP marker read
count: 2
You should not be counting anything until the SOP arrives!
Logged

Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Quote
EOP marker read
count: 1
SOP detectedEOP marker read
count: 2

But this code doesn't work that way right?
Code:
void readatinq()
{
  while(mySerial.available() > 0)
  {
    char inChar = mySerial.read();
    if(inChar == SOP)
    {
       Serial.print("SOP detected");
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    { 
       count=count+1;
       if(count>=3)
       {Serial.print("EOP detected");
       ended = true;
       break;
       }

When available,  if the character is the SOP it enters that if loop. In case it is an EOP it enters the else if loop and hence the "EOP marker read" line gets printed.
Am i interpreting the code correctly? I guess that it is counting the \r that is present before the SOP! I do understand that you once told that the stuffs that come before the SOP's doesn't matter
Quote
he stream of data may well contain a \r before the +, but that doesn't matter. Nothing happens with the bytes in the stream until the + arrives. 47 \r could arrive before the + does, and they would all be ignored.
But it doesn't seems to happen that way here i guess.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But this code doesn't work that way right?
True, but a slight modification is all that is required.
    else if(inChar == EOP && started)
Logged

Pages: 1 2 [3] 4 5   Go Up
Jump to: