Hi,
trying to implement the Python B4XSerializator (b4x_serializer.py) in Home Assistant with ESP32 BLE (B4R) bi-directional communication.
B4R Type
Python DataClass
Any help appreciated.
Issues
HA receives serialized data, but can not parse
HA sends data, but B4R receives wrong format
B4R Code Snippet Advertise to HA
Data advertised:
HA Code Snippets
const.py
ble_client.py
trying to implement the Python B4XSerializator (b4x_serializer.py) in Home Assistant with ESP32 BLE (B4R) bi-directional communication.
B4R Type
B4X:
Type TServerData (Temperature As Int, Humidity As Int, Command As Int, CommandData As Int)
B4X:
class ServerData: temperature: int humidity: int command: int commanddata: int
Issues
HA receives serialized data, but can not parse
B4X:
[BLE_SERVER_DHT][process_data] Raw data received: 7E04028606020D1A04FF04FF7F
[BLE_SERVER_DHT][process_data] Error deserializing B4X data: Error -3 while decompressing data: incorrect header check
HA sends data, but B4R receives wrong format
B4X:
[BLEServer_NewData] buffer=789C9360E4666060882F4E2D2A4B2D4A492C49640172C16225A9B905A9458925A545A9CC0C20410E2091519A9B999259520911610712C9F9B9B9897929CC4C307D50019059605500099A1463, len=76 bytes
B4R Code Snippet Advertise to HA
B4X:
Type TServerData (Temperature As Int, Humidity As Int, Command As Int, CommandData As Int)
Private ServerData As TServerData
Advertise to HA
ServerData.Temperature = DHT.GetTemperature * 100
ServerData.Humidity = DHT.GetHumidity * 100
ServerData.Command = -1
ServerData.CommandData = -1
'Advertise as serialized objects
Dim data() As Byte = ser.ConvertArrayToBytes(Array(ServerData.Temperature, ServerData.Humidity, ServerData.Command, ServerData.CommandData))
BLEServer_Write(data)
B4X:
[TimerDataAdvertising_Tick] data=7E04028606020D1A04FF04FF7F
const.py
B4X:
@dataclass
class ServerData:
temperature: int
humidity: int
command: int
commanddata: int
B4X:
# Get the global B4X serializer instance
serializer = B4XSerializator(types=[ServerData])
if serializer is None:
_LOGGER.error(f"{LOGGING_PREFIX}[process_data] B4XSerializer not initialized!")
return
# Deserialize Objects Received from B4R
try:
sensor_data = serializer.convert_bytes_to_object(raw_data)
except Exception as e:
_LOGGER.error(f"{LOGGING_PREFIX}[process_data] Error deserializing B4X data: {e}")
return
# Assign the objects
temperature = sensor_data.temperature / 100
humidity = sensor_data.humidity / 100
_LOGGER.info(f"{LOGGING_PREFIX}[process_data] Received Temperature: {temperature}°C, Humidity: {humidity}%")
# Serialize Objects send to B4R
# Get the global B4X serializer instance
serializer = B4XSerializator(types=[ServerData])
if serializer is None:
_LOGGER.error(f"{LOGGING_PREFIX}[write_led_state] B4XSerializer not initialized!")
return
try:
serobj = ServerData(temperature=0, humidity=0, command=led, commanddata=state)
data = serializer.convert_object_to_bytes(obj=serobj)
await asyncio.sleep(0.1)
await self._client.write_gatt_char(CHARACTERISTICS_UUID, data)
_LOGGER.info(f"{LOGGING_PREFIX}[write_led_state] LED state written: " + ", ".join(f"0x{b:02X}" for b in data))
except Exception as e:
_LOGGER.error(f"{LOGGING_PREFIX}[process_data] Error deserializing B4X data: {e}")
return
[CODE=b4x]