Convert ASCII value in binary value in Arduino IDE

Hi.
I've this piece of code that reads value from a BLE service CHARACTERISTIC_UUID (trasmitted from a BLE server to a BLE client, both running on two ESP32 boards) and write it on serial monitor:

 > #define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
    #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"


class MyCallbacks: public BLECharacteristicCallbacks 
{
    void onWrite(BLECharacteristic *pCharacteristic) 
    {
     std::string value = pCharacteristic->getValue();
  
      if (value.length() > 0) {
        Serial.println("*********");
        Serial.print("New value: ");
        for (int i = 0; i < value.length(); i++)
          Serial.print(value[i]);
         
        Serial.println();
        Serial.println("*********");
      }

If I look at the serial monitor I see , depending on the value trasmitted form the BLE client device, two ASCII character : null and what it represents the 1 (ON) value.

The question is:

how should I modify the code so that I may have in the serial monitor not the ASCII value but the binary (or decimal) value and can I treat it as such and use in the rest of the code (i.e. : in an if ... than statement)?
Any idea?
Thanks in advance.

Read each character into an array of chars as you receive it which, because of the null termination, will turn it into a C style string (lowercase s) that you can convert to a decimal value using the atoi() function for use in later comparisons or calculations

what do you really see ? the string "null" and the string "ON" or do you see "0" and "1" ? or something else?

Hi Jackson.
Thanks for your reply.
I'd like to have 0 and 1 as numbers not as alphanumerics characters.

Hi UKHeliBob.
Thanks for you reply.
May you provide an example on how to do this?
Thanks in advance.

Far from a complete example but it will show the principle that I was suggesting

byte charIndex = 0;         //index to the array
char buffer[10];            //buffer for characters read
bool inputComplete = false; //flag to indicate that input is complete

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

void loop()
{
  while (Serial.available())      //read characters if available
  {
    char inChar = Serial.read();  //read a character
    buffer[charIndex++] = inChar; //put it in the buffer
    if (inChar == '\0')           //if end of input
    {
      inputComplete = true;       //flag complete
      break;                      //exit the while loop
    }
  }
  if (inputComplete)              //if input is complete
  {
    int theNumber = atoi(buffer); //convert string to integer
    charIndex = 0;                //reset the array index
    //do whatever you need with theNumber value here
    inputComplete = false;        //ready for the next one
  }
}

This is based on the code in this tutorial Serial input basics - updated

See example 2 in the linked topic

I don't think it's applicable here as the library offers a generic call to get the value that returns a string. (if I'm not mistaken)

I understand what you would want to see, but what do you get now when you do

     std::string value = pCharacteristic->getValue();

what's in value??

on-off

change your function to be

void onWrite(BLECharacteristic *pCharacteristic)
{
  std::string value = pCharacteristic->getValue();

  if (value.length() > 0) {
    Serial.println("*********");
    Serial.print("New value: ");
    for (int i = 0; i < value.length(); i++)
     { Serial.print(value[i], DEC); Serial.write(' ');}
    Serial.println();
    Serial.println("*********");
  }
}

and share what you see in the Serial monitor

Now works better.
The issue is that the if statment don't recognize 0 e 1 and don't works as expected lightning ON and OFF the LED:

OK so you are getting binary data, not ASCII.

Try this

void onWrite(BLECharacteristic *pCharacteristic)
{
  std::string value = pCharacteristic->getValue();

  if (value.length() > 0) {
    if (value[0] == 0) {
      Serial.println("I GOT 0");
    } else {
      Serial.println("I GOT 1");
    }
  } else Serial.println("empty value");
}

Hi Jackson.
It works !

class MyCallbacks: public BLECharacteristicCallbacks 
{
    void onWrite(BLECharacteristic *pCharacteristic) 
    {
     std::string value = pCharacteristic->getValue();
     
      if (value.length() > 0) {
        if (value[0] == 0) {
          Serial.println("I GOT 0");
          digitalWrite(25,LOW);
        } else {
          Serial.println("I GOT 1");
          digitalWrite(25,HIGH);
        }
      } else Serial.println("empty value");   
      
      
    }
      
    };

The LED light ON and OFF like a charm.
Thanks

:slight_smile: have fun

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.