I am transmitting a JSON payload from an Arduino microcontroller and attempting to receive it using a Python script:
import bluetooth #pybluez
import json
sensor_address = "18:D9:31:YY:C7:4A"
socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
socket.connect((sensor_address, 1))
buffer = ""
print("Listening...")
while True:
data = socket.recv(1024)
buffer += str(data, encoding='ascii')
print(buffer) # used to check json payload
try:
data = json.loads(buffer)
print("Received:", data)
buffer = ""
except json.JSONDecodeError as e:
print(e)
continue
Examining the value stored in buffer before entering the try statement, I see what appears to be perfectly valid JSON:
{"a_x":957.5195,"a_y":-0.488281,"a_z":315.918,"g_x":-0.625954,"g_y":-1.305344}{"a_x":961.914,"a_y":-1.953125,"a_z":297.8516,"g_x":-2.816794,"g_y":2.572519}{"a_x":964.8437,"a_y":3.417969,"a_z":303.2227,"g_x":-1,"g_y":0.374046}
However the result of my script is only Expecting value: line 1 column 1 (char 0) repeatedly.
Why doesn't the code inside the try statement execute once a complete payload has been received?
My hunch is that at no time does a valid JSON payload appear in buffer, but instead valid payloads appear along with incomplete payloads.
Is it possible to use a regular expression to extract a valid payload from a string?
The data in
bufferis not valid JSON so that is why you are seeing the error.The buffer seems to have the information in the format of a Python dictionary so you could use Python
remodule to extract the dictionary and then use ast.literal_eval to turn the string in to a Python dictionary.In the example below I've mocked the reading of the socket as I don't have your device.
This gave the following output: