Page 1 of 1

Reading/converting - Integers from Bluetooth instead of string

Posted: Fri Nov 01, 2019 8:07 am
by sigrun
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.
Problem
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);

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

Posted: Mon Nov 04, 2019 4:40 am
by Aussie Susan
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;
		break;
		
	case BatteryService:
		values->batteryVoltage = *(uint16_t*)rxCharacteristic.value;
		break;
		
	case LocalName:
		memcpy(values->deviceName, rxCharacteristic.value, rxCharacteristic.value_len);
		break;
	}
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.
Susan