Receiving negative numbers w/ BLE?

cards2019
Posts: 6
Joined: Sat Mar 06, 2021 8:14 am

Receiving negative numbers w/ BLE?

Postby cards2019 » Mon Mar 29, 2021 4:47 am

Hello, I am using an Arduino to send an integer value between -90 and 90 to an esp32. I am able to read everything fine when I only send a value between 0 and 90 but now things are trickier. In the notifyCallback function, pData is of data type uint8_t, so that means between 0 and 255, right? However, if it was just int8_t, then it would be between -128 and 127 which would be great, but I can't get it to work.

Code: Select all

static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData, size_t length, bool isNotify)
Is there a way to for notifyCallback to be able to produce a negative number? Or, should I just ignore that function and focus on putting this this line in my main loop?

Code: Select all

std::string value = pRemoteCharacteristic->readValue();
      Serial.print("The characteristic value was: ");
      Serial.println(value.c_str());
I am having all sorts of problems trying to convert std::string value to an integer that doesn't produce an ascii symbol in my serial monitor. I've tried a few solutions (e.g. atoi(), toInt(), std::stoi(value) ) but I have not had any luck. Does anyone have any insight on how to get BLE_client to read a negative number? Thank you.

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: Receiving negative numbers w/ BLE?

Postby chegewara » Mon Mar 29, 2021 2:55 pm

Code: Select all

int8_t value = *(int8_t*)pData;

cards2019
Posts: 6
Joined: Sat Mar 06, 2021 8:14 am

Re: Receiving negative numbers w/ BLE?

Postby cards2019 » Tue Mar 30, 2021 6:13 am

Thank you so much. That did the trick.

Who is online

Users browsing this forum: No registered users and 34 guests