Dears, i’m working on a LiFi image transmission project using ESP32 microcontrollers. I then use 2 microcontrollers, one as transmitter through laser and another as receiver through an LDR. So far, the Arduino codes and Python script i’ve used for the receiption aren’t working so far.
PYTHON CODE TO CONVERT IMAGE TO STRING:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import base64
defimage_to_base64(image_path):
with open(image_path, "rb") as img_file:
# Read the image file in binary mode
img_data = img_file.read()
# Encode the binary data as base64
base64_data = base64.b64encode(img_data)
# Decode bytes to string
base64_string = base64_data.decode("utf-8")
return base64_string
# Example usage
image_path = "blue.png"# Replace with your image file path
base64_string = image_to_base64(image_path)
print(base64_string)
</code>
<code>import base64
def image_to_base64(image_path):
with open(image_path, "rb") as img_file:
# Read the image file in binary mode
img_data = img_file.read()
# Encode the binary data as base64
base64_data = base64.b64encode(img_data)
# Decode bytes to string
base64_string = base64_data.decode("utf-8")
return base64_string
# Example usage
image_path = "blue.png" # Replace with your image file path
base64_string = image_to_base64(image_path)
print(base64_string)
</code>
import base64
def image_to_base64(image_path):
with open(image_path, "rb") as img_file:
# Read the image file in binary mode
img_data = img_file.read()
# Encode the binary data as base64
base64_data = base64.b64encode(img_data)
# Decode bytes to string
base64_string = base64_data.decode("utf-8")
return base64_string
# Example usage
image_path = "blue.png" # Replace with your image file path
base64_string = image_to_base64(image_path)
print(base64_string)
<code>import serial
import base64
from io import BytesIO
from PIL import Image
import time
import binascii
from serial import Serial
import re # Ajout de l'importation de la bibliothèque re
long_string = ""
fixed_base64_str = ""
complete_base64_string = ""
ser = serial.Serial('COM5', 9600, timeout=15)
ser.reset_input_buffer()
#time.sleep(2)
while True:
if ser.in_waiting > 0:
data = ser.read()
decoded_data = data.decode('latin-1')
long_string += decoded_data
complete_base64_string += decoded_data
# Filtrer les caractères non valides
complete_base64_string = re.sub(r'[^A-Za-z0-9+/=]', '', complete_base64_string)
print(decoded_data)
if " " in long_string:
break
ser.close()
long_string = long_string[:-1]
print(f"nn{long_string}nn")
# Decode the image from the fixed base64 string
image_bytes = base64.b64decode(complete_base64_string)
image = Image.open(BytesIO(image_bytes))
image.show()
</code>
import serial
import base64
from io import BytesIO
from PIL import Image
import time
import binascii
from serial import Serial
import re # Ajout de l'importation de la bibliothèque re
long_string = ""
fixed_base64_str = ""
complete_base64_string = ""
ser = serial.Serial('COM5', 9600, timeout=15)
ser.reset_input_buffer()
#time.sleep(2)
while True:
if ser.in_waiting > 0:
data = ser.read()
decoded_data = data.decode('latin-1')
long_string += decoded_data
complete_base64_string += decoded_data
# Filtrer les caractères non valides
complete_base64_string = re.sub(r'[^A-Za-z0-9+/=]', '', complete_base64_string)
print(decoded_data)
if " " in long_string:
break
ser.close()
long_string = long_string[:-1]
print(f"nn{long_string}nn")
# Decode the image from the fixed base64 string
image_bytes = base64.b64decode(complete_base64_string)
image = Image.open(BytesIO(image_bytes))
image.show()
RECEIPTION CIRCUIT
When i run python receiver code, i got this error:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Traceback(most recent call last):
File "C:UsersYasmineAppDataLocalProgramsPythonPython312coderecep.py", line 15, in<module>
data = ser.read().decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3in position 0: unexpected endof data
</code>
<code>Traceback (most recent call last):
File "C:UsersYasmineAppDataLocalProgramsPythonPython312coderecep.py", line 15, in <module>
data = ser.read().decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 0: unexpected end of data
</code>
Traceback (most recent call last):
File "C:UsersYasmineAppDataLocalProgramsPythonPython312coderecep.py", line 15, in <module>
data = ser.read().decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 0: unexpected end of data
I tried to adjust the synchronisation threshold in receiption code between 210 and 800, but it did not work.
Do you think this project will work?
I have been working on it for almost 5 months.
python
arduino
decode
esp32
encode
New contributor
Fifali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.