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.
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:
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.
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.
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 ':'
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.