In your peripheral code, you define the characteristic as an unsigned long which occupies 4 bytes.
BLEUnsignedLongCharacteristic intyCharacteristic("19B10014-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite );
In your peripheral code, you write the value of the characteristic with a signed int, which occupies 2 bytes.
int inty =1000;
intyCharacteristic.writeValue(inty);
In your central code, you read the value into an array and then pack it back into a variable.
Serial.println(byteArrayToInt(intyCharacteristic.value(), intyCharacteristic.valueLength()));
What happens if you try this:
In your peripheral code...
unsigned int inty =1000;
intyCharacteristic.writeValue(inty);
In your central code...
while (peripheral.connected()) {
// while the peripheral is connected
unsigned int Cval=intyCharacteristic.read();
Serial.print("Value ");
Serial.println(Cval);
Serial.print("rssi ");
Serial.println (peripheral.rssi());
delay(4000);
// read the button pin
}
...and get rid of this
union ArrayToInteger {
byte array[4];
uint32_t integer;
};
int byteArrayToInt(const byte data[], int length) {
byte dataW[length];
for (int i = 0; i < length; i++) {
byte b = data[i];
dataW[i] = data[i];
}
ArrayToInteger converter; //Create a converter
converter.array[0] = dataW[0];
converter.array[1] = dataW[1];
converter.array[2] = dataW[2];
converter.array[3] = dataW[3];
return converter.integer ;
// Serial.println(dataW[1],HEX);
// Serial.println(dataW[2],HEX);
//Serial.println(dataW[3],HEX);
}
What kind of result does that give you? Note that I have not tested this and it may not preserve the "endianness" but it is worth a try.
edited to add: unsigned int Cval=intyCharacteristic.read(); should have been uint32_t Cval; intyCharacteristic.readValue(Cval);