Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data conversion in flutter

I'm currently playing around with BLE and Flutter trying to learn how it all works. I have an esp32 mcu that is sending a temperature value via ble. While there doesn't seem to be any conversion of the float temperature value in the esp code, when it is received on the flutter app, it's in uint8 (or possibly uint32). How would i go about converting that back into a double in Flutter? An example is 23.9 is converted to 1103049523. Below are some code snippets i believe are relevant.

ESP32 code

 float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();


  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }



  Serial.print(F("Humidity: "));
  Serial.println(h);
  Serial.print(F("Temperature: "));
  Serial.println(t);



  pCharacteristic->setValue(t);
    
  pCharacteristic->notify();
  }

From the flutter code:

 final stream = bleManager.subscribeToCharacteristic(characteristic!);
    await for (final data in stream) {
      var dataInt = ByteData.view(Uint8List.fromList(data).buffer)
          .getUint32(0, Endian.little);
      print("GOT DATA: $dataInt");
      setState(() {
        lastRead = dataInt;
        temp = lastRead.toDouble();
      });
    }

As you can see i tried converting the "lastRead" to a double but that didn't work as i suspect there's more to it.

like image 553
Juicy Avatar asked Dec 18 '25 08:12

Juicy


1 Answers

You seem to be explicitly interpreting your data as a 32-bit integer by calling getUint32() on it. How about trying getFloat32() instead?

like image 104
Tarmo Avatar answered Dec 21 '25 01:12

Tarmo