Parsing serial data from bluetooth module

Hello everyone,

I'm new to Arduino and I don't know how to parse serial data. I've read that maybe the strtok function would be a solution.

So what I'm trying to do is establish a communication between two Arduino's with HC-05 Bluetooth modules. Both are receiving and sending data to eachother.

Arduino1 is going to send some variables and data. Let's say that it needs to send P=1023 (which ranges from 0 to 1023) and Z=30 (which ranges from 0 to 100).
Maybe sending it as a string? For example P:1023,Z:30
But how do I send it?

Next, Arduino2 will receive that data (P:1023,Z:30) and now needs to parse it into P=1023 and Z=30.
So how do I parse it?

Thank you!

Alright, thanks!

For easiest decoding, usually the internal data is delimited with some character, and the end of the data packet is marked with another character. lots of different ways. Find the easiest to use decoding, then send the data in that format.

Maybe sending it as a string? For example P:1023,Z:30

It might be easier to send the data like below. Bottom is some servo rx code that uses this type of format.

1023P,30Z,

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

And how do I send it as 1023a,203c, ?

I've tried this:

BTserial.write(variable);
BTserial.write("a,");

but it doesn't seem to work

And how do I send it as 1023a,203c, ?

Below is what I use which is pretty much the same as yours. You can trouble shoot using the serial monitor setup per the bottom link where you can communicate directly with the bluetooth using the serial monitor.

    Serial.print(newval1); //print the new value for testing 
    Serial.print("a,");

http://forum.arduino.cc/index.php?topic=371986.msg2566861#msg2566861

Alright, so my project is an electric skateboard. I have the remote and the skateboard that have Arduino's.
The remote sends a value to the skate that it reads from it's analog input.

Code for the remote:

#include <SoftwareSerial.h>
#include <Arduino.h>
#include <Servo.h>

SoftwareSerial BTserial(A4, A3); // RX | TX
const byte BTpin = A5;
// HC05 TX vers Arduino pin A4 RX. 
// HC05 RX vers Arduino pin A3 TX avec diviseur de tension.
// HC05 STATE pin vers Arduino pin A5.

// BTconnected = false lorsque pas connecte
boolean BTconnected = false;
char c = ' ';
int t1,t2,pileskate,rpm,valeurpot;
int potentiometre = A0;                // potentiometre pin A0
String readString;


void setup() {
  
    // BTpin set input
    pinMode(BTpin, INPUT);   
 
    // serial to computer
    Serial.begin(9600);
    Serial.println("Remote is powered on \n");
    Serial.println("Connect remote to skateboard\n");
 
    // att connection bluetooth
    while (!BTconnected)
    {
      if ( digitalRead(BTpin)==HIGH)  { BTconnected = true;};
    }
    Serial.println("Remote is connected to skate\n");
 
    //comm with HC05
    BTserial.begin(57600);  
}

void loop() {
  
  //if remote is not connected to skate
  if (digitalRead(BTpin)==LOW)  {
    BTconnected = false;
    Serial.print("Please connect remote to skate\n");
    while (!BTconnected)
    {
      if ( digitalRead(BTpin)==HIGH)  { BTconnected = true;};
    }
    Serial.println("Remote is connected to skate");
    BTconnected = true;
  }
  
  valeurpot = analogRead(potentiometre);
  valeurpot=map(valeurpot,0,1023,1000,2000);               // convert 0-1023 to 1000-2000
  //Serial.println(valeurpot);
  BTserial.write(valeurpot);
  BTserial.write("a,"); 
  delay(150);
  
  if (BTserial.available())  {
    char c = BTserial.read();  //gets one byte from serial buffer
    if (c == ',') {       //delimited ',' string parse 
      if (readString.length() >1) {
        int n = readString.toInt();  //convert readString into a number 
        if(readString.indexOf('b') >0) {
           rpm=n;
           Serial.print("rpm= ");
           Serial.println(rpm);
        }
        if(readString.indexOf('c') >0) {
           pileskate=n;
           Serial.print("Pile skate = ");
           Serial.println(pileskate);
        }
        if(readString.indexOf('d') >0) {
           t1=n;
           Serial.print("t1= ");
           Serial.println(t1);
        }
        if(readString.indexOf('e') >0) {
           t2=n;
           Serial.print("t2= ");
           Serial.println(t2);
        }
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }     
}

Here's the code for the skate:

#include <SoftwareSerial.h>
#include <Arduino.h>
#include <Servo.h>

SoftwareSerial BTserial(A4, A3); // RX | TX
const byte BTpin = A5;
// HC05 TX vers Arduino pin A4 RX. 
// HC05 RX vers Arduino pin A3 TX avec diviseur de tension.
// HC05 STATE pin vers Arduino pin A5.

// BTconnected = false lorsque pas connecte
boolean BTconnected = false;
char c = ' ';
int period;
Servo ESC;                             //define name regulateur de vitesse
String readString;


void setup() {
  
    // BTpin set input
    pinMode(BTpin, INPUT);   
 
    // serial to  computer 
    Serial.begin(9600);
    Serial.println("Skate is powered on \n");
    Serial.println("Connect remote \n");
 
    // wait BT connect
    while (!BTconnected)
    {
      if ( digitalRead(BTpin)==HIGH)  { BTconnected = true;};
    }
    Serial.println("Remote is connected \n");
 
    //comm with HC05
    BTserial.begin(57600);  
    
    //set ESC
    ESC.attach(9);                       //ESC to pin 9
    ESC.writeMicroseconds(1000); //set speed to 0
}

void loop() {
  
  //if remote is not connected to skate
  if ( digitalRead(BTpin)==LOW)  {
    BTconnected = false;
    ESC.writeMicroseconds(1000);
    Serial.print("Please connect remote \n");
    while (!BTconnected)
    {
      if ( digitalRead(BTpin)==HIGH)  { BTconnected = true;};
    }
    Serial.println("Remote is connected");
    BTconnected = true;
  }
  
  if (BTserial.available())  {
    char c = BTserial.read();  //gets one byte from serial buffer
    if (c == ',') {       //delimited ',' string parse 
      if (readString.length() >1) {
        int n = readString.toInt();  //convert readString into a number 
        if(readString.indexOf('a') >0) { 
          ESC.writeMicroseconds(n);  //ESC.write(n); //angle
          period=n;
          Serial.print("ESC=  ");
          Serial.println(period);  
        }
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

In the serial monitor, all I see is ESC = 0;
So in short, I've used (which didn't work):

BTserial.write(valeurpot);
BTserial.write("a,");

Then I've used only(which still showed ESC = 0):

BTserial.write("120a,");

Then I've added a delay of 150ms (and it showed ESC = 120):

BTserial.write("120a,");
delay(150);

But this still doesnt work:

BTserial.write(valeurpot);
BTserial.write("a,"); 
delay(150);

Update:

This works now:

BTserial.print(valeurpot);
BTserial.print("a,"); 
delay(150);

instead of:

BTserial.write(valeurpot);
BTserial.write("a,"); 
delay(150);

Thank you very much!