Pages: 1 ... 3 4 [5]   Go Down
Author Topic: Problem in receiving the data from Bluetooth module  (Read 3828 times)
0 Members and 2 Guests are viewing this topic.
Chennai, TN, India
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The if tests are right, but for only one value of i
I thought that the inData1 array stores character by character. So in order to compare each letter of "DONE" I used the for loop. Can I do it with a single "i" value?
Logged

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

Quote
I thought that the inData1 array stores character by character.
It does.

Quote
So in order to compare each letter of "DONE" I used the for loop.
I don't understand why.

Quote
Can I do it with a single "i" value?
Each time you add a character, you need to check whether it is an E, with N in the previous spot, O in the one before that, and and D in the one before that. So, the value for i would be index, and you don't need a for loop.
Logged

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

I have tweaked the program to display all the MAC id's properly and also used the strtok() function with \r as the delimiter to split the result into tokens.  I get this output


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

00:1C:A4:14:7D:AA,"Sony Ericsson"

00:25:48:7B:55:2F,"Nokia 5130c-2"

DONE

OK

From this I need to strip the INQ , DONE , OK and also i guess the \n.

How do I do that?


Logged

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

You could use "\r\n" as the delimiters in the strtok() call. Then, determine the length of the token. Discard it if the length is 1. That will take care of the \n.

You can use strcmp() to see if the token is "DONE" or "OK". If so, discard the token.

You can use strncmp() to see if the first 4 letters of the token are "INQ:", if so, simply increment the token (pointer) by 4 to point to the first character after the INQ: as the start of the string.
Logged

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

My program is
Code:
#include <avr/wdt.h>
#include <SoftwareSerial.h>
#include <SD.h>
#define rxPin 2
#define txPin 3
#define SOP1 '\r\n'
#define EOP1 '\r\n'
#define SOP '+'
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
char * pch;
char inData[200];
char inData1[200];
byte index;
int CS_pin=10;
int count=0;
int i;
bool started = false;
bool ended = false;

void setup()
{  // define pin modes for tx, rx, led pins:
  pinMode(CS_pin, OUTPUT);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(9600);
  Serial.begin(9600);
  if (!SD.begin(CS_pin))
   {
     Serial.println("Card Failure");
     return;
   }
 Serial.println("Card Ready");
 // File header
   File logFile = SD.open("LOG.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(", , , ,"); //Just a leading blank line, incase there was previous data
    String header = "MAC ID, Name";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
}

void readprocess()
{
  while (mySerial.available() > 0)
  {
    char inChar = mySerial.read();
    if(inChar == SOP1)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP1)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 199)
      {
        inData[index] = inChar;
        Serial.print(inData[index]);
        index++;
        inData[index] = '\0';
      }
    }
  }
 
 if(started && ended)
  {
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
   }
}

void readatinq()
{
  while(ended==false)
  {
  while(mySerial.available() > 0 )
  {
    char inChar = mySerial.read();
    if(inChar == SOP)
    {
       Serial.println("SOP MARKER READ");
       index = 0;
       inData1[index] = '\0';
       started = true;
    }
   else
    {
     if((index < 199))
      {
        inData1[index] = inChar;
        i=index;
          if(inData1[i]=='\n')
          {
            if(inData1[i-1]=='\r')
            {
              if(inData1[i-2]=='K')
              {
                if(inData1[i-3]=='O')
                {
                   if(inData1[i-4]=='\n')
                 {
                    if(inData1[i-5]=='\r')
                   {
                     if(inData1[i-6]=='\n')
                     {
                       if(inData1[i-7]=='\r')
                       {
                         if(inData1[i-8]=='E')
                         {
                         if(inData1[i-9]=='N')
                          {
                         if(inData1[i-10]=='O')
                           {
                         if(inData1[i-11]=='D')
                             {
                              Serial.println("EOP MARKER READ");
                              ended=true;
                             }
                           }
                          }
                         }
                        
                       }
                     }
                    
                   }
                 }
               }
              }
            }
          }
        }
        index++;
        inData1[index] = '\0';
      }
if(started && ended)

  {     // The end of packet marker arrived. Process the packet
     Serial.println(inData1);
     Serial.println("TOKENS");
     pch = strtok (inData1,"\r\n");
     while (pch != NULL)
  {
    if((strcmp(pch,"DONE")!=0)&&(strcmp(pch,"OK")!=0))
    {
    Serial.println(pch);
    store();
    pch = strtok (NULL, "\r\n");
    }
    else
    {
   pch=NULL;
  }
 }
    started = false;
    index = 0;
    count=0;
    inData[index] = '\0';
   }
 }
}
ended = false;
}

void store()
{
  File dataFile = SD.open("log.csv", FILE_WRITE);
  if (dataFile)
  {
    dataFile.println(pch);
    dataFile.close();
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
}

void loop()
{
  Serial.println("Initializing");
  Serial.println("===");
  mySerial.print("===");
  delay(100);
  readprocess();
  delay(500);  
  Serial.println("LLL");
  mySerial.print("LLL");
  delay(100);
  readprocess();
  delay(500);
  Serial.print("\r\nAT+INQ\r\n");
  mySerial.print("\r\nAT+INQ\r\n");
  delay(100);
  readprocess();
  readatinq();
  delay(1000);
}


and I get the correct output on the serial monitor. The problem is, on the SD card, in the .csv file I get the header line twice!. I have attached the file too..How to rectify this?

* LOG.CSV (0.83 KB - downloaded 6 times.)
* LOG.CSV (0.82 KB - downloaded 5 times.)
« Last Edit: March 22, 2012, 12:17:13 pm by elan_arr » Logged

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

I'd suggest that you look at the strncmp() function. That mess in readatinq() is silly looking.

I'd also suggest that you add Serial.print() and/or Serial.println() statements to store() to verify that it is getting called and that it is called with the right arguments.
Logged

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

How should I use the strcmp() function to compare four consecutive characters in a character array?
Logged

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

Quote
How should I use the strcmp() function to compare four consecutive characters in a character array?
You have data in inData1. At any given time, you want to know if the last 4 characters in inData1 are "DONE", when there are more than 4 characters. So, after the inData1[index] = NULL; statement, add:
Code:
if(index] >= 4)
{
   char *donePtr = inData[index-4]; // point to the r4th character from the end
   if(strcmp(donePtr, "DONE" == 0)
   {
     // Found "DONE"
   }
}
Logged

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

Quote
char *donePtr = inData1[index-4];

I get a compilation error, "invalid conversion from char to char" on this line.
inData1 was declared as char inData1[200];
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char *donePtr = &inData[index-4];
Logged

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

Everything is working perfectly. The only prob is the device goes to a non responsive state every two minutes. Only the reset button brings it back to life. Can this be a problem because of the coding? or the hardware? Is the only solution,to reset? if yes how can I reset it automatically every 2 minutes. I also need to reset without affecting the timer that is running in the program.
Logged

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