Receiving signed data larger than 128

Hi everyone, I know similar threads have popped up on this forum a few times but none seem to provide a simple solution to this problem and I was wondering if someone could help me out.

I need to receive data from an Arduino, the data on the Arduino that I'm sending out via BLE is a double. However, when I receive this data In MIT App Inventor through functions register for bytes and when bytes received I am limited to either 128 signed 256 unsigned. I need these values to reach at least 2000 signed.

When I try use functions in MIT App Inventor like doubles received or shorts received I simply get no data coming through. I've seen other users output multiple bits of data with multiple characteristics from the Arduino but surely there is an easier way.

I'm not sure if my Arduino project is necesary but here is the function which outputs the data:

void outputScale()
{
       int t, i, n, T;
       n=7;
       i=0;
  double val, sum, sumsq, mean;
  float stddev;

  float oldVal;

     while (i<n) 
  {
        val = ((scale.read() - scale.get_offset()) / scale.get_scale());
     if (val != oldVal)
     {      
    WeightLevelChar.writeValue(val);  // and update the battery level characteristic
    oldVal = val;           
     }
    sum += val;
    i++;
  }
  mean = sum / n;
  if(i>=n){
    i=0;
  }
     Serial.print(val);
     Serial.print("\n"); 
  lcd.setCursor (0,1);
  lcd.print("                      ");
  lcd.setCursor (0,1);
  lcd.print("weight:  ");
  lcd.print(mean);
  lcd.print("g");
}

Here are screenshots of my bytes received functions. As mentioned when I use the exact same blocks but floats received or shorts received I simply get no data.

What BLE device are you using?
Can you send string instead of bytes?

I am using an Android phone to receive the data. I can try sending string however I think there is a more simple solution. Firstly I realized my problem before was that my Arduino characteristic was defined in the wrong format sending out 8 bit data in the form 0x00. I changed the characteristic to an int and it is now sending out data as 16 bit 0x0000.

My problem is now that I am receiving data in the format 0x2801 which is supposed to equal 296.
Obviously in Hex that should be written as 0x0128. I am now just trying to swap the hex numbers around to convert the incoming data appropriately. I could maybe do this inside of C of the Arduino IDE but I am sure it can be done in MIT App Inventor.

This is current conversion I have in place the the bytes in position two are unfortunately in wrong order for 2's complement.

If someone knows how I can simply flip the bytes in position two my problem will be solved :slight_smile:

The method shown in the attached image works fine for unsigned values but with signed values it is not always correct.

Okay I found the strangest solution to this problem. Instead of declaring the bytes as signed (which they are) I declared them as unsigned in MIT and then added the following if then else block to convert the unsigned bytes to signed bytes. Seems to work flawlessly.

If its stupid and it works: It's not stupid.

1 Like

That is a nifty solution Thomas, but for peace of mind I would send-receive a string.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.