Extract number from char data coming from bluetooth connexion

Hello,

I would like to retrieve the "1" in this what I receive via bluetooth, I get this: "Open: 1".
I would like to recover the 1 in an integer. This figure can vary ...

If necessary, I can modify what I receive in another form.

I inquired but I can't, can you help me please?

Thank you for your help.

Please consider changing the topic title to English

Take a look at the strtok() function

I would suggest to study Serial Input Basics to handle this

consider


#define S_LEN   80
char  s [S_LEN];

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

}

void loop() {
    char cmd [20];
    int  val;

    if (Serial.available ())  {
        Serial.readBytesUntil ('\n', s, S_LEN);

        Serial.println (s);

        sscanf (s, "%s %d", cmd, &val);

        if (! strcmp (cmd, "Open:"))
            Serial.println (val);
    }
}

Thank you but I do not understand how it all works ...

reads characters into a c-string until a newline char

sscanf() extracts values from the specified string, "s", depending on the format string, "%s %d" into the specified variables. it copies a "string" into a c-string and translates numeric data into the variable pointed to by the argument, &val

compares the c-string, "cmd" to the specified string, "Open:". strcmp() returns 0 if there is a match, hence the "!"

1. While sending the said string "Open: 1" from the Bluetooth, please send LF (Line Feed = 0x0A) as the terminating charcater.

2. Upload the following sketch in UNO and check that you have seen 1 on the OutputBox of the Serial Monitor.

char myArray[12];
char *temPtr;

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

void loop()
{
  while (Serial.available() > 0)
  {
    byte m = Serial.readBytesUntil('\n', myArray, 12);
    unsigned int x1 = strtoul(myArray, &temPtr, 10);
    byte x2 = strtoul(temPtr+5, &temPtr, 10);
    Serial.println(x2, DEC);  //shows: 1 or (0 to 255)
    memset(myArray, 0x00, 12);  //reset the array
  }
}

Working Principle of strtoul() Function:
The function parses (read a character from a character type array and check if it is a digit) a string (array of characters) and stops at the non-digit character. The ASCII codes of the digits which have been parsed are converted into a numerical number which is saved in a variable. For example:

char myArray = “1234, 56, 78”;
char *temPtr;r
unsigned int x1 = strtoul(myArray, &temPtr, 10); // x1 = 0x04D2
Serial.println(x1, DEC); //shows: 1234

The arg3 (= 10) says that the function looks for decimal digits in the array. The parsing stops at the first comma (,); there remains the sub-array/sub-string (, 56, 78) which will be parsed next to extract 56. Now, we need a pointer variable (*temPtr) to point the beginning of the sub-array. The arg2 (= &temPtr) passes to the pointer variable (temPtr) the address of the character where parsing has stopped and this mechanism allows the strtoul() function to locate the beginning of the sub-array.

fast learner ! :wink:

I can't find the old karma!

Thank you, I understand well but it does not work ...

I send "Open:10x0A"

My code :

#include <SoftwareSerial.h>

SoftwareSerial HC06(3,4);
char myArray[12];
char *temPtr;

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

void loop()
{
  while (HC06.available() > 0)
  {
    byte m = HC06.readBytesUntil('\n', myArray, 12);
    unsigned int x1 = strtoul(myArray, &temPtr, 10);
    byte x2 = strtoul(temPtr+5, &temPtr, 10);//shows: 1 or (0 to 255)
    Serial.println(x2, DEC);
    memset(myArray, 0x00, 12);  //reset the array
  }
}

What do you want to see on the Serial Monitor? Your operand is: 10x0A - to what numerical will it be evaluated? Remember that the PC/Program is a dump entity and it does exactly what you tell it to do unless it is an AI Machine.

I would like to see 1 but I see absolutely nothing ...
You said to put 0x0A at the end of the command right?

try this (typed here, untested)

void parseMessage(const char* message) {
  Serial.print(F("\nParsing -> [")); Serial.print(message); Serial.println(F("]"));
  char *colonPtr = strchr(message, ':'); // find the first ':' in the message
  if (colonPtr) { // if we found one
    char *ptr;
    long result = strtol(colonPtr + 1, &ptr, 10); // try to decode a signed long integer starting 1 character after the ':'
    if (ptr != colonPtr + 1) {  // if we managed to parse something
      Serial.print(F("Value = ")); Serial.println(result);
      if (*ptr != '\0') { // if we are not at the end of the cString after the number
        Serial.print(F("Careful, there was more data after the number -> "));
        Serial.println(ptr);
      }
    } else Serial.println(F("Error Could not find a number after ':'"));
  } else Serial.println(F("Error missing ':'."));
}

void setup() {
  Serial.begin(115200);
  parseMessage("open: 1");
  parseMessage("open: -245");
  parseMessage("open:               123456");
  parseMessage("open: 42XYZ");
  parseMessage("open 1");
  parseMessage("open: abcd");
}

void loop() {}

open the Serial monitor at 115200 bauds and look at the results


Parsing -> [open: 1]
Value = 1

Parsing -> [open: -245]
Value = -245

Parsing -> [open:               123456]
Value = 123456

Parsing -> [open: 42XYZ]
Value = 42
Careful, there was more data after the number -> XYZ

Parsing -> [open 1]
Error missing ':'.

Parsing -> [open: abcd]
Error Could not find a number after ':'

so is that useful?

Yes I understand better, so I can keep this function and just get the result?

I suggest you improve it to return an error code and the value if successful

if your numbers can't be negative, the function could return a negative value (-1, -2, -3) indicating the error code otherwise it would return the value it read.

Let us learn how to insert LF (ASCII code is: 0x0A) or sometimes called NL (Newline) at the end of string you are sending from BT Terminal of the Android Phone.
1. Open the BT Terminal in the Android Phone.
2. Pair the BT Module with your phone.
3. Press and hold you finger for a second on the Send ASCII button. As a result, a menu will apeear with \r-CR and \n-LF option. Check the \n-LF option and save.

4. Click on the Enter ASCII Command button. As a result, a blinking cursor will appear. Enter Open: 123 and then click on the Send ASCII button.

5. Check that 123 has appeared on the Serial Monitor of your Arduino.