Converting 4 bytes to integer

yet another post , this time for a different problem .
I am connecting my phone app with my Arduino which works as a server and transmits sensor values to the phone.
I want to get the integers from arduino to phone and upload them from phone to ioT platform.
Everything is fine so far and i can upload values but they are all 0 .
I checked that BLE reads these values from arduino as bytes , and sends them to phone as 4 bytes.
My problem is i need to convert these 4 bytes to an integer and then send it to iot platform.
in c++ you would need something like

int buffToInteger(char * buffer)
int a = int((unsigned char)(buffer[0]) << 24 |
(unsigned char)(buffer[1]) << 16 |
(unsigned char)(buffer[2]) << 8 |
(unsigned char)(buffer[3]));
return a;

but i have no clue how to convert it Here

Ps. unsigned because i know there are positive values

1 Like
import java.util.Arrays;

public String GetBytesA(String input) {
    byte[] byt = input.getBytes();
    return  Arrays.toString(byt);

 @SimpleFunction(description = "Convert string to bytes")
 public String GetBytes(String input) {
    return GetBytesA(input);

Can be done through extension


I need to convert Bytes to integer ot string to bytes .
Also i cant find the extension you are talking , could you provide a link ?

1 Like

Send the values as strings from Arduino, App Inventor can collect them as strings and process them as integers.


I converted the int to a char array and i send it to app via BLE , then i use this block to read it and upload it .

My test label on my phone shows i receive this value

Really i dont know whats going on :stuck_out_tongue:

1 Like

Looks like the data packet is not being assembled correctly in the Arduino Script.

1 Like

or does it get transformed wrong in BLE server ?

1 Like

There is no transforming :upside_down_face:

1 Like

Transforming in a way like , Firstly i was sending int to BLE and BLe was giving me Hex values .
Could char array be converted to something in ble ? thats what i meant :stuck_out_tongue:

1 Like

Post your Arduino Sketch as a .txt file for me to take a quick look.

Looking at your 'StringsReceived' Block, if the Thingspeak_List is initialized as an actual Blocks List, the data packet values need to be split into this List. You also will not see a List correctly in a Label by just plugging it in, you have to feed or assemble the data in value-by-value.

1 Like

I pm'd The sketch .
Do i need a text with a comma to seperate a block lists value ?

1 Like

Only if that is the separator used by the Sketch.

Is it a top secret military document or something? We prefer everything to be in the open so that others can learn too. That's one of the expediences when the Software and the Tech Support are free.

1 Like

u are right .
sketch.txt (2.7 KB)

heres the sketch , i tried with dtostrf but i think it needs a lib i dont have in order to happen , cuz i get compiler error so i tried itoa for convertsion

1 Like

Don't be shy ...



#include <ArduinoBLE.h>
#define SERVICE_UUID        
#define SENSOR_PIN A0
#define LO_PLUS_PIN 10
#define LO_PLUS_MIN 11

BLEService ECGservice(SERVICE_UUID );

BLEIntCharacteristic ECGchar( CHARACTERISTIC_UUID_TX, BLERead | BLENotify );

int EcgValue = 0;
static unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);    // initialize serial communication
  pinMode(LO_PLUS_PIN, INPUT); // Setup for leads off detection LO +
  pinMode(LO_PLUS_MIN, INPUT); // Setup for leads off detection LO -
  while (!Serial);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);

  /* Set a local name for the BLE device
     This name will appear in advertising packets
     and can be used by remote devices to identify this BLE device
     The name can be changed but maybe be truncated based on space left in advertisement packet
  BLE.setLocalName("Ecg Rose");
  BLE.setAdvertisedService(ECGservice); // add the service UUID
  ECGservice.addCharacteristic(ECGchar); // add the ecg  characteristic
  BLE.addService(ECGservice); // Add the battery service
  ECGchar.writeValue( 0 ); // set initial value for this characteristic
  /* Start advertising BLE.  It will start continuously transmitting BLE
     advertising packets and will be visible to remote BLE central devices
     until it receives a new connection */

  // start advertising

  Serial.println("Bluetooth device active, waiting for connections...");

void loop() {
  // wait for a BLE central
  BLEDevice central = BLE.central();

  // if a central is connected to the peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's BT address:
    // turn on the LED to indicate the connection:

    // while the central is connected:
    while (central.connected()) {

      EcgValue = analogRead(SENSOR_PIN);
      long currentMillis = millis();
      // Let's convert the value to a char array:
      char txString[4]; // make sure this is big enuffz
     // dtostrf(EcgValue, 1, 2, txString); // int_val, min_width, digits_after_decimal, char_buffer      it needs Library , doesnt read it 
      itoa(EcgValue, txString, 10);

      if (currentMillis - previousMillis >= 200) {
        previousMillis = currentMillis;

    // when the central disconnects, turn off the LED:
    Serial.print("Disconnected from central: ");
1 Like

I really dont know whats up with me today :stuck_out_tongue:

1 Like

Where did you find this Sketch? It's a bit of a mess. Apart from the Phone (App), is there another device receiving data?

What is the expected range of the EcgValue?

1 Like

Its the ble example and i added characteristic for Read notify and the Ecg value to read A0 and the conversion
it doesnt go higher than 1200 when i tested and the loop needs an auto format and delete the extra spaces i did to make it more clear when i was writing

1 Like

... and you are in fact only sending the EcgValue, every 200 millis, so we do not need a value separator.....

1 Like

Yes i did that to give time for BLe to read,
Sketch is to sent ECG value only , everything else should be on the app

1 Like

I notice for example that UUIDs are there - but are they being used?

What is the Bluetooth module on your Arduino, an HC-10?