Go Down

Topic: faulty transmission/retrieval of bluetooth data  (Read 619 times) previous topic - next topic

Robin2

which gives me

And is that you are expecting?

That simple program in Reply #14 makes no attempt to identify the start or end of the messages. Without knowing when a message starts and ends how can you parse it properly?

Have you carefully studied the examples in the link I gave you in Reply #2?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Code: [Select]
    Serial.print(mySerial.read());
The read() method returns an int, not a char. Store the value in a char, and print the char.
The art of getting good answers lies in asking good questions.

Blue Eyes

I was recently working with an Arduino and HM-10 with a phone app.  Characters would intermittently get dropped when sending to the Arduino.  I could see that my app was (trying) to write all the characters but every now and then there would be one missing on the receiving end.

I fixed it by having the Arduino echo the characters back to the phone, with the app waiting until it receives characters before writing to the characteristic again.

If your problem is with transmission and not with parsing you might give that a shot.  

Ziayakens

#18
Nov 21, 2018, 03:42 am Last Edit: Nov 21, 2018, 03:45 am by Ziayakens
oh thats unfortunate.. I see to be parsing it incorrectly.

with

Code: [Select]
void loop() {
  while (mySerial.available () > 0) {
    char d = mySerial.read();
    Serial.print(d);
    if (d == ';') {
      Serial.println("");     
    }
  }
}
It shows

Code: [Select]
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;
c:255,191,0;




Robin2, yes I read that,  In the main code I was trying to use ; as the indicator of the end of the data. But it seems im not parsing correctly

Robin2

#19
Nov 21, 2018, 09:54 am Last Edit: Nov 21, 2018, 09:55 am by Robin2
Robin2, yes I read that,  In the main code I was trying to use ; as the indicator of the end of the data. But it seems im not parsing correctly
I reckon if you use the second example in my Serial Input Basics and change the end-marker to a semi-colon it will receive the data correctly.

Once you have received a message you can parse it with strtok() using the comma as the separator. The first group will include the c: and it looks like you can just ignore those first 2 characters - just replace them with the 0 character (ASCII 48).

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Quote
just replace them with the 0 character (ASCII 48).
Or the space character (ASCII 32).
The art of getting good answers lies in asking good questions.

Ziayakens

when I use your sketch like

Code: [Select]
void loop() {
recvWithEndMarker();
showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = ';';
    char rc;
   
    while (mySerial.available() > 0 && newData == false) {
        rc = mySerial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}


I get
Code: [Select]
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c,0
This just in ... c:255,191,0
This just in ... c:255,1c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0
This just in ... c:255,191,0

Robin2

You have not posted the complete program so I have no idea what pins you are using or what baud rate etc etc

I am tempted to say there is a problem with the incoming data, but I would like to see the whole program.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Ziayakens

#23
Nov 22, 2018, 06:02 pm Last Edit: Nov 22, 2018, 06:04 pm by Ziayakens
I actually eventually got it to work with this

Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial mySerial(7,8); // RX, TX

void setup() {
  Serial.begin(115200);
  mySerial.begin(57600); //required when using the hm-10 with an arduino nano
  mySerial.listen();
}
char data[20];
byte indx = 0;
void loop() {
  while (mySerial.available () > 0) {
    char d = mySerial.read();
    data[indx] = d;
    indx ++;
    if (d == ';') {
      Serial.println(data);
      data[0] = (char)0;
      indx = (byte)0;     
    }
  }
}


I still nee to implement strtok() and more but your link sent me in the right direction. Thank you so much.
I apologize if I was difficult to assist, but I greatly appreciate your help.

Robin2

I actually eventually got it to work with this
That does not seem to be functionally different from my tutorial code.

There is a parse example in the tutorial.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up