ESP32 BLE comparison with pointer

Hey guys!

Would be nice if sb could help me here.

if((char*)turnsafe==“A”)

I have problems comparing the variable turnsafe, which is a pointer i guess, with this “A”, an integer or anything. At the moment the compiler doesnt say anything but its still not working…
Im receiving turnsafe from another ESP32 over BLE using the notify and client example sketch. This part works, but I need to figure out how to work with my transmitted variable in this if loop…

Any advice?
Thx in advance!

BLE_client.ino (5.92 KB)

try strcmp()

-jim lee

rubm: Hey guys!

Would be nice if sb could help me here.

if((char*)turnsafe=="A")

I have problems comparing the variable turnsafe, which is a pointer i guess, with this "A", an integer or anything. At the moment the compiler doesnt say anything but its still not working... Im receiving turnsafe from another ESP32 over BLE using the notify and client example sketch. This part works, but I need to figure out how to work with my transmitted variable in this if loop...

Any advice? Thx in advance!

As the previous poster said use strcmp() if indeed turnsafe is a pointer to char.

FYI: What if((char*)turnsafe=="A") is doing is casting turnsafe to a pointer to char and then comparing it to the pointer to wherever the constant string "A" resides in memory, which is who knows where.

thx, that was a quick response!

if(strcmp(turnsafe)=="A")

Im sorry, never used strcmp. How do you code this? In the upper case the compiler says: invalid conversion from 'uint8_t* {aka unsigned char*}' to 'const char*' [-fpermissive]

or this way if(strcmp((char*)turnsafe)=="A") too few arguments to function 'int strcmp(const char*, const char*)'

Proper use of strcmp:

if(strcmp((char*)turnsafe, "A") == 0)
{
   // Code for match
}
else
{
   // Code for does not match
}

Keep in mind that turnsafe must point to a valid C string! (must end with a NULL '/0' character)

now the device keeps rebooting:

Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled. Core 1 register dump: PC : 0x40001277 PS : 0x00060f30 A0 : 0x800d1ea0 A1 : 0x3ffc86c0 A2 : 0x00000000 A3 : 0x3f4015fd A4 : 0xdff0504c A5 : 0x3ffc5280 A6 : 0x3ffdfd58 A7 : 0x00a80010 A8 : 0x800d6f7c A9 : 0x3ffc8690 A10 : 0x00000002 A11 : 0x3f40158b A12 : 0x00000001 A13 : 0x3ffc5280 A14 : 0x3f40194a A15 : 0x3ffdee3c SAR : 0x00000010 EXCCAUSE: 0x0000001c EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xffffffff

Backtrace: 0x40001277:0x3ffc86c0 0x400d1e9d:0x3ffc86d0 0x400d7411:0x3ffc86f0 0x4008e089:0x3ffc8710

Rebooting...

I think my problem is what you mentioned above, cause turnsafe is declared as follows: uint8_t* turnsafe=0;

sorry, basically dont know anything about these pointers...

rubm: I think my problem is what you mentioned above, cause turnsafe is declared as follows: uint8_t* turnsafe=0;

sorry, basically dont know anything about these pointers...

That's called a null pointer. You pretty much never want to point to 0.

do you think you can have a look at the sketch I uploaded earlier?
all I need to do is comparing the transmitted variable pData which I equaded with turnsafe…

You could change it to this:

if (turnsafe != 0)
{
   if(strcmp((char*)turnsafe, "A") == 0)
  {
     Serial.println("brakeLight");
  }
  else
  {
     Serial.println("blaaaaaaaaaa");
  }
}

This way no comparison will be made unless notifyCallback() function has been executed. However, in the notifyCallback() function the pData pointer is assigned to turnsafe. I'm not convinced that pointer remains valid after exiting notifyCallback. You may want to copy the data.

no way, now it works just fine... you safed my projekt!!!

highly appreciate it!