Reading/converting - Integers from Bluetooth instead of string

Posts: 5
Joined: Sat Oct 05, 2019 1:47 pm

Reading/converting - Integers from Bluetooth instead of string

Postby sigrun » Fri Nov 01, 2019 8:07 am

Hi Forum
I am working on a Generel Bluetooth Low Energy program, for use in the varius control systems my students build. I have now a system which collect data from 3 potentiometers and transmit those via Bluetooth. I am able to read the data in the serial monitor and in nRF-Connect on the phone. On the Bluetooth recever I can see the characters change when I turn the potentiometers.
I would like to receive pure numerical data.
When I look at my data in the serial monitor I send 05 0B 00 00 --- 32bit
When I receive the Characters 05 0b on “nRF Connect”, it is representative for 0x0B05 = decimal 2821
How do I change my code to get the original integers back, can I get all 32 bits in use?
Is it possible to receive the send data as two integers instead of a string?

Reading the variable from bluetooth

Code: Select all

//When the BLE Server sends position reading with the notify property
static void AvarCall(BLERemoteCharacteristic* pBLERemoteCharacteristic, 
                                        uint8_t* pData, size_t length, bool isNotify) {
  //store position 
  AvarC = (char*)pData;
  AvarB = true;                                                                       }
writing the variable to screen

Code: Select all

void printDHTReadings(){
  Serial.print    ("Avar");
  Serial.print    (AvarC);

Aussie Susan
Posts: 33
Joined: Thu Aug 22, 2019 3:48 am

Re: Reading/converting - Integers from Bluetooth instead of string

Postby Aussie Susan » Mon Nov 04, 2019 4:40 am

If you know the value is 32-bits long, then cast the pData pointer to a 'uint32_t' (or int32_t if the value could be signed) and use that.
In the code snippet I have actually passed the packet data back to my own data struct re but it is an exact copy of what you get from the API:

Code: Select all

	switch( rxService.serviceType)
	case PanelService:
		values->panelVoltage = *(uint16_t*)rxCharacteristic.value;
	case BatteryService:
		values->batteryVoltage = *(uint16_t*)rxCharacteristic.value;
	case LocalName:
		memcpy(values->deviceName, rxCharacteristic.value, rxCharacteristic.value_len);
Here my PanelService and BatteryService pass back a 16-bit unsigned value while the LocalName passes an (up to) 30 character string. The pointer is cast to the appropriate type and the value just read off.

Who is online

Users browsing this forum: No registered users and 10 guests