From d34ef1d6e7aa150876066199979d40895bf277de Mon Sep 17 00:00:00 2001 From: schneck2004 Date: Fri, 10 Apr 2026 14:51:57 +0200 Subject: [PATCH 01/10] ftespnow: Initial commit --- .gitignore | 1 + .../ftespnow-client/ftespnow/client.py | 77 +++++++++++++++++++ .../ftespnow/ftespnow-client/manifest.py | 9 +++ .../ftespnow-server/ftespnow/server.py | 45 +++++++++++ .../ftespnow/ftespnow-server/manifest.py | 9 +++ micropython/ftespnow/ftespnow/__init__.py | 9 +++ micropython/ftespnow/manifest.py | 6 ++ 7 files changed, 156 insertions(+) create mode 100644 micropython/ftespnow/ftespnow-client/ftespnow/client.py create mode 100644 micropython/ftespnow/ftespnow-client/manifest.py create mode 100644 micropython/ftespnow/ftespnow-server/ftespnow/server.py create mode 100644 micropython/ftespnow/ftespnow-server/manifest.py create mode 100644 micropython/ftespnow/ftespnow/__init__.py create mode 100644 micropython/ftespnow/manifest.py diff --git a/.gitignore b/.gitignore index 65a81ba62..26e7a8ed0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ MANIFEST __pycache__ +.vscode/ *.egg-info */dist/ *.org diff --git a/micropython/ftespnow/ftespnow-client/ftespnow/client.py b/micropython/ftespnow/ftespnow-client/ftespnow/client.py new file mode 100644 index 000000000..8587b8212 --- /dev/null +++ b/micropython/ftespnow/ftespnow-client/ftespnow/client.py @@ -0,0 +1,77 @@ +import espnow +import json + +class CLIENT: + def __init__(self) -> None: + self.esp = espnow.ESPNow() + + def configure(self, timeout=5) -> None: + self.timeout: int = timeout + + def connect(self, peer: str) -> None: + self.peer: str = peer + self.esp.active(True) + self.esp.add_peer(self.peer) + + def send_message(self, data: str) -> bool: + ack: bool = self.esp.send(self.peer, data) + return ack + + def receive_message(self, recv_timeout :int= 5) -> list | None: + received = self.esp.recv(recv_timeout) + if received[0] == None: return + return received + + def send_txt(self, filename: str) -> bool: + with open(filename, 'r') as f: + data: str = str(f.readlines()) + sent: bool = self.send_message(data) + return sent + + def send_json(self, filename: str, *, indent: int=4) -> bool: + with open(filename, 'r') as f: + unparsed = json.load(f) + parsed: str = json.dumps(unparsed, indent=indent) + sent: bool = self.send_message(parsed) + return sent + + def receive_to_txt(self, target_file: str, mode: str='a') -> bool: + if ".txt" not in target_file: raise SyntaxError("File format must be .txt") + try: + data: list | None = self.receive_message() + if data == None: return False + data_list: list[str] = str(data[-1]).split("\n") + if data_list[-1] == "": data_list = data_list[:-1] + with open(target_file, mode) as f: + f.writelines(data_list) + return True + except SyntaxError: + raise + + def receive_to_json(self, target_file: str, mode: str='a') -> bool: + if ".json" not in target_file: raise SyntaxError("File format must be .json") + try: + data: list | None = self.receive_message() + if data == None: return False + mac: str = str(data[0]) + message = json.loads(str(data[-1])) + unparsed: dict = { + "mac": mac, + "message": message + } + with open(target_file, mode) as f: + json.dump(unparsed, f) + return True + except SyntaxError: + raise + + def receive_to_dict(self) -> dict: + data: list | None = self.receive_message() + if data == None: return {} + mac: str = str(data[0]) + message = json.loads(str(data[-1])) + unparsed: dict = { + "mac": mac, + "message": message + } + return unparsed \ No newline at end of file diff --git a/micropython/ftespnow/ftespnow-client/manifest.py b/micropython/ftespnow/ftespnow-client/manifest.py new file mode 100644 index 000000000..228008f22 --- /dev/null +++ b/micropython/ftespnow/ftespnow-client/manifest.py @@ -0,0 +1,9 @@ +metadata( + description="Client-side commands", + version="0.1.0", +) + +require("ftespnow") +require("json") +require("espnow") +package("ftespnow") \ No newline at end of file diff --git a/micropython/ftespnow/ftespnow-server/ftespnow/server.py b/micropython/ftespnow/ftespnow-server/ftespnow/server.py new file mode 100644 index 000000000..c148bed9c --- /dev/null +++ b/micropython/ftespnow/ftespnow-server/ftespnow/server.py @@ -0,0 +1,45 @@ +import espnow +import json + +class SERVER: + def __init__(self, *, timeout: int=5) -> None: + self.esp = espnow.ESPNow() + self.timeout = timeout + + def configure(self, *, timeout: int=5) -> None: + self.timeout = timeout + + def send_message(self, peer: str, data: str) -> bool: + ack: bool = self.esp.send(peer, data) + return ack + + def receive_message(self, recv_timeout :int= 5) -> list | None: + received = self.esp.recv(recv_timeout) + if received[0] == None: return + return received + + def send_txt(self, peer: str, filename: str) -> bool: + with open(filename, 'r') as f: + data: str = str(f.readlines()) + sent: bool = self.send_message(peer, data) + return sent + + def send_json(self, peer: str, filename: str, *, indent: int=4) -> bool: + with open(filename, 'r') as f: + unparsed = json.load(f) + parsed: str = json.dumps(unparsed, indent=indent) + sent: bool = self.send_message(peer, parsed) + return sent + + def receive_to_txt(self, target_file: str, mode: str='a') -> bool: + if ".txt" not in target_file: raise SyntaxError("File format must be .txt") + try: + data: list | None = self.receive_message() + if data == None: return False + data_list: list[str] = str(data[-1]).split("\n") + if data_list[-1] == "": data_list = data_list[:-1] + with open(target_file, mode) as f: + f.writelines(data_list) + return True + except SyntaxError: + raise \ No newline at end of file diff --git a/micropython/ftespnow/ftespnow-server/manifest.py b/micropython/ftespnow/ftespnow-server/manifest.py new file mode 100644 index 000000000..f6522e1c8 --- /dev/null +++ b/micropython/ftespnow/ftespnow-server/manifest.py @@ -0,0 +1,9 @@ +metadata( + description="Server-side commands", + version="0.1.0", +) + +require("ftespnow") +require("json") +require("espnow") +package("ftespnow") \ No newline at end of file diff --git a/micropython/ftespnow/ftespnow/__init__.py b/micropython/ftespnow/ftespnow/__init__.py new file mode 100644 index 000000000..f85254af3 --- /dev/null +++ b/micropython/ftespnow/ftespnow/__init__.py @@ -0,0 +1,9 @@ +try: + from .clinet import * +except ImportError: + pass + +try: + from.server import * +except ImportError: + pass \ No newline at end of file diff --git a/micropython/ftespnow/manifest.py b/micropython/ftespnow/manifest.py new file mode 100644 index 000000000..6591b0600 --- /dev/null +++ b/micropython/ftespnow/manifest.py @@ -0,0 +1,6 @@ +metadata( + description="Extends the micropython espnow module with methods to support file transfers.", + version="0.1.0", +) + +module("ftespnow") \ No newline at end of file From a8288dd67f91d81b45d37f0837d797636923bc3c Mon Sep 17 00:00:00 2001 From: schneck2004 Date: Fri, 10 Apr 2026 17:25:48 +0200 Subject: [PATCH 02/10] ftespnow: Added function docstrings --- .../ftespnow-client/ftespnow/client.py | 135 +++++++++++++++++- .../ftespnow-server/ftespnow/server.py | 88 ++++++++++++ 2 files changed, 219 insertions(+), 4 deletions(-) diff --git a/micropython/ftespnow/ftespnow-client/ftespnow/client.py b/micropython/ftespnow/ftespnow-client/ftespnow/client.py index 8587b8212..6b93a4d3b 100644 --- a/micropython/ftespnow/ftespnow-client/ftespnow/client.py +++ b/micropython/ftespnow/ftespnow-client/ftespnow/client.py @@ -14,21 +14,71 @@ def connect(self, peer: str) -> None: self.esp.add_peer(self.peer) def send_message(self, data: str) -> bool: + """ + Send a string + + Args: + + data (str): Data to be sent + + Returns: + + bool: Confirmation flag (`True` if data was received, `False` otherwise) + """ + ack: bool = self.esp.send(self.peer, data) return ack def receive_message(self, recv_timeout :int= 5) -> list | None: + """ + Receive a string + + Args: + + recv_timeout (int, optional): Reception timeout. Defaults to 5. + + Returns: + + list | None: `[, ]` | `None` if no message is received + """ + received = self.esp.recv(recv_timeout) if received[0] == None: return return received def send_txt(self, filename: str) -> bool: + """ + Parse and send a `.txt` file as a `string` + + Args: + + filename (str): Filepath of the desired file to be sent with file name and extention + + Returns: + + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) + """ + with open(filename, 'r') as f: data: str = str(f.readlines()) sent: bool = self.send_message(data) return sent def send_json(self, filename: str, *, indent: int=4) -> bool: + """ + Parse and send a `.json` file as a `string` + + Args: + + filename (str): Filepath of the desired file to be sent with file name and extention + + indent (int, optional): Desired indent of the resulting parsed `string` (for formatting purposes). Defaults to 4. + + Returns: + + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) + """ + with open(filename, 'r') as f: unparsed = json.load(f) parsed: str = json.dumps(unparsed, indent=indent) @@ -36,23 +86,89 @@ def send_json(self, filename: str, *, indent: int=4) -> bool: return sent def receive_to_txt(self, target_file: str, mode: str='a') -> bool: + """ + Write received `string` into a `.txt` file. + + Args: + + target_file (str): Filepath of the destination file for the received data with file name and extension. + + mode (str, optional): Editing mode + + - `r` - Read only + + - `w` - Write only (truncates file before writing) + + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) + + - `a` - Append to the end of the file (default) + + - `b` - Binary mode + + - `t` - Text mode + + - `+` - Update (read and write) + + Read `open()`_ for more information + + Returns: + + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) + + .. _open(): https://docs.python.org/3/library/functions.html#open + """ + if ".txt" not in target_file: raise SyntaxError("File format must be .txt") try: + received: bool = False data: list | None = self.receive_message() - if data == None: return False + if data == None: return received data_list: list[str] = str(data[-1]).split("\n") if data_list[-1] == "": data_list = data_list[:-1] with open(target_file, mode) as f: f.writelines(data_list) - return True + return not received except SyntaxError: raise def receive_to_json(self, target_file: str, mode: str='a') -> bool: + """ + Write received `string` into a `.json` file. + + Args: + + target_file (str): Filepath of the destination file for the received data with file name and extension. + + mode (str, optional): Editing mode + + - `r` - Read only + + - `w` - Write only (truncates file before writing) + + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) + + - `a` - Append to the end of the file (default) + + - `b` - Binary mode + + - `t` - Text mode + + - `+` - Update (read and write) + + Read `open()`_ for more information + + Returns: + + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) + + .. _open(): https://docs.python.org/3/library/functions.html#open + """ + if ".json" not in target_file: raise SyntaxError("File format must be .json") try: + received: bool = False data: list | None = self.receive_message() - if data == None: return False + if data == None: return received mac: str = str(data[0]) message = json.loads(str(data[-1])) unparsed: dict = { @@ -61,11 +177,22 @@ def receive_to_json(self, target_file: str, mode: str='a') -> bool: } with open(target_file, mode) as f: json.dump(unparsed, f) - return True + return not received except SyntaxError: raise def receive_to_dict(self) -> dict: + """ + Unparses received `string` into a `dict` object + + Args: + + None: + + Returns: + + unparsed (dict): `dictionary` object containing unparsed equivalent of the received `.json` + """ data: list | None = self.receive_message() if data == None: return {} mac: str = str(data[0]) diff --git a/micropython/ftespnow/ftespnow-server/ftespnow/server.py b/micropython/ftespnow/ftespnow-server/ftespnow/server.py index c148bed9c..e50020c2f 100644 --- a/micropython/ftespnow/ftespnow-server/ftespnow/server.py +++ b/micropython/ftespnow/ftespnow-server/ftespnow/server.py @@ -10,21 +10,77 @@ def configure(self, *, timeout: int=5) -> None: self.timeout = timeout def send_message(self, peer: str, data: str) -> bool: + """ + Send a string + + Args: + + peer (str): client's mac address + + data (str): Data to be sent + + Returns: + + bool: Confirmation flag (`True` if data was received, `False` otherwise) + """ + ack: bool = self.esp.send(peer, data) return ack def receive_message(self, recv_timeout :int= 5) -> list | None: + """ + Receive a string + + Args: + + recv_timeout (int, optional): Reception timeout. Defaults to 5. + + Returns: + + list | None: `[, ]` | `None` if no message is received + """ + received = self.esp.recv(recv_timeout) if received[0] == None: return return received def send_txt(self, peer: str, filename: str) -> bool: + """ + Parse and send a `.txt` file as a `string` + + Args: + + peer (str): client's mac address + + filename (str): Filepath of the desired file to be sent with file name and extention + + Returns: + + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) + """ + with open(filename, 'r') as f: data: str = str(f.readlines()) sent: bool = self.send_message(peer, data) return sent def send_json(self, peer: str, filename: str, *, indent: int=4) -> bool: + """ + Parse and send a `.json` file as a `string` + + Args: + + peer (str): client's mac address + + filename (str): Filepath of the desired file to be sent with file name and extention + + indent (int, optional): Desired indent of the resulting parsed `string` (for formatting purposes). Defaults to 4. + + Returns: + + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) + """ + with open(filename, 'r') as f: unparsed = json.load(f) parsed: str = json.dumps(unparsed, indent=indent) @@ -32,6 +88,38 @@ def send_json(self, peer: str, filename: str, *, indent: int=4) -> bool: return sent def receive_to_txt(self, target_file: str, mode: str='a') -> bool: + """ + Write received `string` into a `.txt` file. + + Args: + + target_file (str): Filepath of the destination file for the received data with file name and extension. + + mode (str, optional): Editing mode + + - `r` - Read only + + - `w` - Write only (truncates file before writing) + + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) + + - `a` - Append to the end of the file (default) + + - `b` - Binary mode + + - `t` - Text mode + + - `+` - Update (read and write) + + Read `open()`_ for more information + + Returns: + + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) + + .. _open(): https://docs.python.org/3/library/functions.html#open + """ + if ".txt" not in target_file: raise SyntaxError("File format must be .txt") try: data: list | None = self.receive_message() From b8012ed50964e4a08ef10849f8b57c15de9d99a0 Mon Sep 17 00:00:00 2001 From: schneck2004 Date: Fri, 10 Apr 2026 17:39:44 +0200 Subject: [PATCH 03/10] ftespnow-server: Added JSON support, and write to dictionary functionality Added the receive_to_json() function, that supports exporting received data to a JSON file, and the receive_to_dict() function, that returns the received data as a dictionary --- .../ftespnow-server/ftespnow/server.py | 74 ++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/micropython/ftespnow/ftespnow-server/ftespnow/server.py b/micropython/ftespnow/ftespnow-server/ftespnow/server.py index e50020c2f..5294b968b 100644 --- a/micropython/ftespnow/ftespnow-server/ftespnow/server.py +++ b/micropython/ftespnow/ftespnow-server/ftespnow/server.py @@ -130,4 +130,76 @@ def receive_to_txt(self, target_file: str, mode: str='a') -> bool: f.writelines(data_list) return True except SyntaxError: - raise \ No newline at end of file + raise + + def receive_to_json(self, target_file: str, mode: str='a') -> bool: + """ + Write received `string` into a `.json` file. + + Args: + + target_file (str): Filepath of the destination file for the received data with file name and extension. + + mode (str, optional): Editing mode + + - `r` - Read only + + - `w` - Write only (truncates file before writing) + + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) + + - `a` - Append to the end of the file (default) + + - `b` - Binary mode + + - `t` - Text mode + + - `+` - Update (read and write) + + Read `open()`_ for more information + + Returns: + + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) + + .. _open(): https://docs.python.org/3/library/functions.html#open + """ + + if ".json" not in target_file: raise SyntaxError("File format must be .json") + try: + received: bool = False + data: list | None = self.receive_message() + if data == None: return received + mac: str = str(data[0]) + message = json.loads(str(data[-1])) + unparsed: dict = { + "mac": mac, + "message": message + } + with open(target_file, mode) as f: + json.dump(unparsed, f) + return not received + except SyntaxError: + raise + + def receive_to_dict(self) -> dict: + """ + Unparses received `string` into a `dict` object + + Args: + + None: + + Returns: + + unparsed (dict): `dictionary` object containing unparsed equivalent of the received `.json` + """ + data: list | None = self.receive_message() + if data == None: return {} + mac: str = str(data[0]) + message = json.loads(str(data[-1])) + unparsed: dict = { + "mac": mac, + "message": message + } + return unparsed \ No newline at end of file From f80a21d4bef36d9ace5f5ec02b4966806662458c Mon Sep 17 00:00:00 2001 From: schneck2004 Date: Fri, 10 Apr 2026 17:43:25 +0200 Subject: [PATCH 04/10] ftespnow: Fixed typo when importing ".client" extension in __init__.py --- micropython/ftespnow/ftespnow/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micropython/ftespnow/ftespnow/__init__.py b/micropython/ftespnow/ftespnow/__init__.py index f85254af3..a490d14ed 100644 --- a/micropython/ftespnow/ftespnow/__init__.py +++ b/micropython/ftespnow/ftespnow/__init__.py @@ -1,5 +1,5 @@ try: - from .clinet import * + from .client import * except ImportError: pass From 3a8c6d13eee808e002d4c971207d25c825213ae8 Mon Sep 17 00:00:00 2001 From: schneck2004 Date: Fri, 10 Apr 2026 18:22:26 +0200 Subject: [PATCH 05/10] ftespnow: Added examples and updated docstrings --- .../ftespnow/examples/client_side_example.py | 54 +++++++++++++++++++ .../ftespnow/examples/server_side_example.py | 52 ++++++++++++++++++ .../ftespnow-client/ftespnow/client.py | 13 +++-- .../ftespnow-server/ftespnow/server.py | 4 ++ 4 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 micropython/ftespnow/examples/client_side_example.py create mode 100644 micropython/ftespnow/examples/server_side_example.py diff --git a/micropython/ftespnow/examples/client_side_example.py b/micropython/ftespnow/examples/client_side_example.py new file mode 100644 index 000000000..7928c5ba8 --- /dev/null +++ b/micropython/ftespnow/examples/client_side_example.py @@ -0,0 +1,54 @@ +import ftespnow + +# Initialize ESP-NOW client +esp = ftespnow.CLIENT() + +# Connect to ESP-NOW server +esp.connect("a4f00f772d15") # Change to actual server mac address + +# Send a message +message = "Hello" +sent = esp.send_message(message) +if sent: # Check if server received the data + print("Message received by server") +else: + print("Message not received by server") + +# Receive a message +received_data = esp.receive_message() +if received_data == None: # Check if any data was received + print("No message was received (timed out)") +else: + print(f"Here is the received data: {received_data}") + +# Send a .txt file +txt_sent = esp.send_txt("filepath/filename.txt") +if txt_sent: # Check if server received the data + print("File received by server") +else: + print("File not received by server") + +# Send a .json file +json_sent = esp.send_json("filepath/filename.json") +if json_sent: # Check if server received the data + print("File received by server") +else: + print("File not received by server") + +# Write received data to .txt file +txt_received = esp.receive_to_txt("filepath/filename.txt", mode='w') # Set mode to 'w' so file is truncated before writing +if txt_received: + print("File received successfully") +else: + print("No file received. Destination file was not created/modified") + +# Write received data to .json file +json_received = esp.receive_to_json("filepath/filename.json", mode='w') # Set mode to 'w' so file is truncated before writing +if json_received: + print("File received successfully") +else: + print("No file received. Destination file was not created/modified") + +# Write received data to a python dictionary +data_dict = esp.receive_to_dict() +print(data_dict) \ No newline at end of file diff --git a/micropython/ftespnow/examples/server_side_example.py b/micropython/ftespnow/examples/server_side_example.py new file mode 100644 index 000000000..17a8bb70b --- /dev/null +++ b/micropython/ftespnow/examples/server_side_example.py @@ -0,0 +1,52 @@ +import ftespnow + +# Initialize ESP-NOW client +esp = ftespnow.SERVER() + +# Send a message +message = "Hello" +peer = "a4f00f772d15" # Mac address of the client that you want to send data to +sent = esp.send_message(peer, message) +if sent: # Check if client received the data + print("Message received by clientclient") +else: + print("Message not received by client") + +# Receive a message +received_data = esp.receive_message() +if received_data == None: # Check if any data was received + print("No message was received (timed out)") +else: + print(f"Here is the received data: {received_data}") + +# Send a .txt file +txt_sent = esp.send_txt(peer, "filepath/filename.txt") +if txt_sent: # Check if client received the data + print("File received by client") +else: + print("File not received by client") + +# Send a .json file +json_sent = esp.send_json(peer, "filepath/filename.json") +if json_sent: # Check if client received the data + print("File received by client") +else: + print("File not received by client") + +# Write received data to .txt file +txt_received = esp.receive_to_txt("filepath/filename.txt", mode='w') # Set mode to 'w' so file is truncated before writing +if txt_received: + print("File received successfully") +else: + print("No file received. Destination file was not created/modified") + +# Write received data to .json file +json_received = esp.receive_to_json("filepath/filename.json", mode='w') # Set mode to 'w' so file is truncated before writing +if json_received: # Check if any data was received + print("File received successfully") +else: + print("No file received. Destination file was not created/modified") + +# Write received data to a python dictionary +data_dict = esp.receive_to_dict() # Will return {} if no data was received +print(data_dict) \ No newline at end of file diff --git a/micropython/ftespnow/ftespnow-client/ftespnow/client.py b/micropython/ftespnow/ftespnow-client/ftespnow/client.py index 6b93a4d3b..f67d052a8 100644 --- a/micropython/ftespnow/ftespnow-client/ftespnow/client.py +++ b/micropython/ftespnow/ftespnow-client/ftespnow/client.py @@ -2,10 +2,11 @@ import json class CLIENT: - def __init__(self) -> None: + def __init__(self, *, timeout: int=5) -> None: self.esp = espnow.ESPNow() + self.timeout = timeout - def configure(self, timeout=5) -> None: + def configure(self, *, timeout: int=5) -> None: self.timeout: int = timeout def connect(self, peer: str) -> None: @@ -87,7 +88,9 @@ def send_json(self, filename: str, *, indent: int=4) -> bool: def receive_to_txt(self, target_file: str, mode: str='a') -> bool: """ - Write received `string` into a `.txt` file. + Write received `string` into a `.txt` file. + + **Will not write or create file if no data is received** Args: @@ -112,7 +115,7 @@ def receive_to_txt(self, target_file: str, mode: str='a') -> bool: Read `open()`_ for more information Returns: - + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) .. _open(): https://docs.python.org/3/library/functions.html#open @@ -135,6 +138,8 @@ def receive_to_json(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.json` file. + **Will not write or create file if no data is received** + Args: target_file (str): Filepath of the destination file for the received data with file name and extension. diff --git a/micropython/ftespnow/ftespnow-server/ftespnow/server.py b/micropython/ftespnow/ftespnow-server/ftespnow/server.py index 5294b968b..6623501be 100644 --- a/micropython/ftespnow/ftespnow-server/ftespnow/server.py +++ b/micropython/ftespnow/ftespnow-server/ftespnow/server.py @@ -91,6 +91,8 @@ def receive_to_txt(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.txt` file. + **Will not write or create file if no data is received** + Args: target_file (str): Filepath of the destination file for the received data with file name and extension. @@ -136,6 +138,8 @@ def receive_to_json(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.json` file. + **Will not write or create file if no data is received** + Args: target_file (str): Filepath of the destination file for the received data with file name and extension. From a8baf0c1c0688d61d6f4838df1066322e762cccc Mon Sep 17 00:00:00 2001 From: schneck2004 Date: Fri, 10 Apr 2026 18:32:50 +0200 Subject: [PATCH 06/10] ftespnow: Moved manifests.py to the correct folder --- micropython/ftespnow/{ => ftespnow}/manifest.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename micropython/ftespnow/{ => ftespnow}/manifest.py (100%) diff --git a/micropython/ftespnow/manifest.py b/micropython/ftespnow/ftespnow/manifest.py similarity index 100% rename from micropython/ftespnow/manifest.py rename to micropython/ftespnow/ftespnow/manifest.py From 9b50264200eb22546c07cf476b8e6bcbb927a3bf Mon Sep 17 00:00:00 2001 From: schneck2004 Date: Fri, 10 Apr 2026 18:37:40 +0200 Subject: [PATCH 07/10] ftespnow: Moved __init__.py to the correct folder --- micropython/ftespnow/ftespnow/{ => ftespnow}/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename micropython/ftespnow/ftespnow/{ => ftespnow}/__init__.py (100%) diff --git a/micropython/ftespnow/ftespnow/__init__.py b/micropython/ftespnow/ftespnow/ftespnow/__init__.py similarity index 100% rename from micropython/ftespnow/ftespnow/__init__.py rename to micropython/ftespnow/ftespnow/ftespnow/__init__.py From 5a64472f94f4745a3d705338738dc92cb6d32c14 Mon Sep 17 00:00:00 2001 From: schneck2004 Date: Fri, 10 Apr 2026 18:45:01 +0200 Subject: [PATCH 08/10] ftespnow: Fix .ftespnow/manifest.py. --- micropython/ftespnow/ftespnow/manifest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micropython/ftespnow/ftespnow/manifest.py b/micropython/ftespnow/ftespnow/manifest.py index 6591b0600..4596bc490 100644 --- a/micropython/ftespnow/ftespnow/manifest.py +++ b/micropython/ftespnow/ftespnow/manifest.py @@ -3,4 +3,4 @@ version="0.1.0", ) -module("ftespnow") \ No newline at end of file +package("ftespnow") \ No newline at end of file From f7a4d334c6ff5dbb1aefbb3cc1ae8a86f1a6c1ac Mon Sep 17 00:00:00 2001 From: schneck2004 Date: Fri, 10 Apr 2026 19:41:28 +0200 Subject: [PATCH 09/10] ftespnow: Fix .ftespnow-client/manifest.py and .ftespnow-server/manifest.py, code formatting, and fixed spelling mistakes. ... Signed-off-by: Guilherme Schneck --- .../ftespnow/examples/client_side_example.py | 4 +- .../ftespnow/examples/server_side_example.py | 4 +- .../ftespnow-client/ftespnow/client.py | 109 ++++++++-------- .../ftespnow/ftespnow-client/manifest.py | 2 - .../ftespnow-server/ftespnow/server.py | 117 +++++++++--------- .../ftespnow/ftespnow-server/manifest.py | 2 - 6 files changed, 118 insertions(+), 120 deletions(-) diff --git a/micropython/ftespnow/examples/client_side_example.py b/micropython/ftespnow/examples/client_side_example.py index 7928c5ba8..ab72b3a1d 100644 --- a/micropython/ftespnow/examples/client_side_example.py +++ b/micropython/ftespnow/examples/client_side_example.py @@ -16,7 +16,7 @@ # Receive a message received_data = esp.receive_message() -if received_data == None: # Check if any data was received +if received_data is None: # Check if any data was received print("No message was received (timed out)") else: print(f"Here is the received data: {received_data}") @@ -51,4 +51,4 @@ # Write received data to a python dictionary data_dict = esp.receive_to_dict() -print(data_dict) \ No newline at end of file +print(data_dict) diff --git a/micropython/ftespnow/examples/server_side_example.py b/micropython/ftespnow/examples/server_side_example.py index 17a8bb70b..c07d9b9d3 100644 --- a/micropython/ftespnow/examples/server_side_example.py +++ b/micropython/ftespnow/examples/server_side_example.py @@ -14,7 +14,7 @@ # Receive a message received_data = esp.receive_message() -if received_data == None: # Check if any data was received +if received_data is None: # Check if any data was received print("No message was received (timed out)") else: print(f"Here is the received data: {received_data}") @@ -49,4 +49,4 @@ # Write received data to a python dictionary data_dict = esp.receive_to_dict() # Will return {} if no data was received -print(data_dict) \ No newline at end of file +print(data_dict) diff --git a/micropython/ftespnow/ftespnow-client/ftespnow/client.py b/micropython/ftespnow/ftespnow-client/ftespnow/client.py index f67d052a8..983cbaa88 100644 --- a/micropython/ftespnow/ftespnow-client/ftespnow/client.py +++ b/micropython/ftespnow/ftespnow-client/ftespnow/client.py @@ -5,7 +5,7 @@ class CLIENT: def __init__(self, *, timeout: int=5) -> None: self.esp = espnow.ESPNow() self.timeout = timeout - + def configure(self, *, timeout: int=5) -> None: self.timeout: int = timeout @@ -19,108 +19,108 @@ def send_message(self, data: str) -> bool: Send a string Args: - + data (str): Data to be sent Returns: - + bool: Confirmation flag (`True` if data was received, `False` otherwise) """ - + ack: bool = self.esp.send(self.peer, data) return ack - + def receive_message(self, recv_timeout :int= 5) -> list | None: """ Receive a string Args: - + recv_timeout (int, optional): Reception timeout. Defaults to 5. Returns: - + list | None: `[, ]` | `None` if no message is received """ - + received = self.esp.recv(recv_timeout) if received[0] == None: return return received - + def send_txt(self, filename: str) -> bool: """ Parse and send a `.txt` file as a `string` Args: - filename (str): Filepath of the desired file to be sent with file name and extention + filename (str): Filepath of the desired file to be sent with file name and extension Returns: - + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) """ - + with open(filename, 'r') as f: data: str = str(f.readlines()) sent: bool = self.send_message(data) return sent - + def send_json(self, filename: str, *, indent: int=4) -> bool: """ Parse and send a `.json` file as a `string` Args: - - filename (str): Filepath of the desired file to be sent with file name and extention - + + filename (str): Filepath of the desired file to be sent with file name and extension + indent (int, optional): Desired indent of the resulting parsed `string` (for formatting purposes). Defaults to 4. Returns: sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) """ - + with open(filename, 'r') as f: unparsed = json.load(f) parsed: str = json.dumps(unparsed, indent=indent) sent: bool = self.send_message(parsed) return sent - + def receive_to_txt(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.txt` file. - + **Will not write or create file if no data is received** - + Args: - + target_file (str): Filepath of the destination file for the received data with file name and extension. - + mode (str, optional): Editing mode - + - `r` - Read only - + - `w` - Write only (truncates file before writing) - + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) - + - `a` - Append to the end of the file (default) - + - `b` - Binary mode - + - `t` - Text mode - + - `+` - Update (read and write) - + Read `open()`_ for more information - + Returns: received (bool): Confirmation flag (`True` if data was received, `False` otherwise) - + .. _open(): https://docs.python.org/3/library/functions.html#open """ - + if ".txt" not in target_file: raise SyntaxError("File format must be .txt") try: received: bool = False @@ -133,42 +133,42 @@ def receive_to_txt(self, target_file: str, mode: str='a') -> bool: return not received except SyntaxError: raise - + def receive_to_json(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.json` file. - + **Will not write or create file if no data is received** - + Args: - + target_file (str): Filepath of the destination file for the received data with file name and extension. - + mode (str, optional): Editing mode - + - `r` - Read only - + - `w` - Write only (truncates file before writing) - + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) - + - `a` - Append to the end of the file (default) - + - `b` - Binary mode - + - `t` - Text mode - + - `+` - Update (read and write) - + Read `open()`_ for more information - + Returns: - + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) - + .. _open(): https://docs.python.org/3/library/functions.html#open """ - + if ".json" not in target_file: raise SyntaxError("File format must be .json") try: received: bool = False @@ -185,19 +185,20 @@ def receive_to_json(self, target_file: str, mode: str='a') -> bool: return not received except SyntaxError: raise - + def receive_to_dict(self) -> dict: """ Unparses received `string` into a `dict` object - + Args: None: - + Returns: unparsed (dict): `dictionary` object containing unparsed equivalent of the received `.json` """ + data: list | None = self.receive_message() if data == None: return {} mac: str = str(data[0]) @@ -206,4 +207,4 @@ def receive_to_dict(self) -> dict: "mac": mac, "message": message } - return unparsed \ No newline at end of file + return unparsed diff --git a/micropython/ftespnow/ftespnow-client/manifest.py b/micropython/ftespnow/ftespnow-client/manifest.py index 228008f22..459051869 100644 --- a/micropython/ftespnow/ftespnow-client/manifest.py +++ b/micropython/ftespnow/ftespnow-client/manifest.py @@ -4,6 +4,4 @@ ) require("ftespnow") -require("json") -require("espnow") package("ftespnow") \ No newline at end of file diff --git a/micropython/ftespnow/ftespnow-server/ftespnow/server.py b/micropython/ftespnow/ftespnow-server/ftespnow/server.py index 6623501be..b02e98c03 100644 --- a/micropython/ftespnow/ftespnow-server/ftespnow/server.py +++ b/micropython/ftespnow/ftespnow-server/ftespnow/server.py @@ -5,10 +5,10 @@ class SERVER: def __init__(self, *, timeout: int=5) -> None: self.esp = espnow.ESPNow() self.timeout = timeout - + def configure(self, *, timeout: int=5) -> None: self.timeout = timeout - + def send_message(self, peer: str, data: str) -> bool: """ Send a string @@ -16,14 +16,14 @@ def send_message(self, peer: str, data: str) -> bool: Args: peer (str): client's mac address - + data (str): Data to be sent Returns: - + bool: Confirmation flag (`True` if data was received, `False` otherwise) """ - + ack: bool = self.esp.send(peer, data) return ack @@ -32,96 +32,96 @@ def receive_message(self, recv_timeout :int= 5) -> list | None: Receive a string Args: - + recv_timeout (int, optional): Reception timeout. Defaults to 5. Returns: - + list | None: `[, ]` | `None` if no message is received """ - + received = self.esp.recv(recv_timeout) if received[0] == None: return return received - + def send_txt(self, peer: str, filename: str) -> bool: """ Parse and send a `.txt` file as a `string` Args: - + peer (str): client's mac address - filename (str): Filepath of the desired file to be sent with file name and extention + filename (str): Filepath of the desired file to be sent with file name and extension Returns: - + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) """ - + with open(filename, 'r') as f: data: str = str(f.readlines()) sent: bool = self.send_message(peer, data) return sent - + def send_json(self, peer: str, filename: str, *, indent: int=4) -> bool: """ Parse and send a `.json` file as a `string` Args: - + peer (str): client's mac address - - filename (str): Filepath of the desired file to be sent with file name and extention - + + filename (str): Filepath of the desired file to be sent with file name and extension + indent (int, optional): Desired indent of the resulting parsed `string` (for formatting purposes). Defaults to 4. Returns: - + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) """ - + with open(filename, 'r') as f: unparsed = json.load(f) parsed: str = json.dumps(unparsed, indent=indent) sent: bool = self.send_message(peer, parsed) return sent - + def receive_to_txt(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.txt` file. - + **Will not write or create file if no data is received** - + Args: - + target_file (str): Filepath of the destination file for the received data with file name and extension. - + mode (str, optional): Editing mode - + - `r` - Read only - + - `w` - Write only (truncates file before writing) - + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) - + - `a` - Append to the end of the file (default) - + - `b` - Binary mode - + - `t` - Text mode - + - `+` - Update (read and write) - + Read `open()`_ for more information - + Returns: - + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) - + .. _open(): https://docs.python.org/3/library/functions.html#open """ - + if ".txt" not in target_file: raise SyntaxError("File format must be .txt") try: data: list | None = self.receive_message() @@ -133,42 +133,42 @@ def receive_to_txt(self, target_file: str, mode: str='a') -> bool: return True except SyntaxError: raise - + def receive_to_json(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.json` file. - + **Will not write or create file if no data is received** - + Args: - + target_file (str): Filepath of the destination file for the received data with file name and extension. - + mode (str, optional): Editing mode - + - `r` - Read only - + - `w` - Write only (truncates file before writing) - + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) - + - `a` - Append to the end of the file (default) - + - `b` - Binary mode - + - `t` - Text mode - + - `+` - Update (read and write) - + Read `open()`_ for more information - + Returns: - + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) - + .. _open(): https://docs.python.org/3/library/functions.html#open """ - + if ".json" not in target_file: raise SyntaxError("File format must be .json") try: received: bool = False @@ -185,19 +185,20 @@ def receive_to_json(self, target_file: str, mode: str='a') -> bool: return not received except SyntaxError: raise - + def receive_to_dict(self) -> dict: """ Unparses received `string` into a `dict` object - + Args: None: - + Returns: unparsed (dict): `dictionary` object containing unparsed equivalent of the received `.json` """ + data: list | None = self.receive_message() if data == None: return {} mac: str = str(data[0]) @@ -206,4 +207,4 @@ def receive_to_dict(self) -> dict: "mac": mac, "message": message } - return unparsed \ No newline at end of file + return unparsed diff --git a/micropython/ftespnow/ftespnow-server/manifest.py b/micropython/ftespnow/ftespnow-server/manifest.py index f6522e1c8..3597500d8 100644 --- a/micropython/ftespnow/ftespnow-server/manifest.py +++ b/micropython/ftespnow/ftespnow-server/manifest.py @@ -4,6 +4,4 @@ ) require("ftespnow") -require("json") -require("espnow") package("ftespnow") \ No newline at end of file From 2bb2242181fa3b6a8b5bf63c2557b18876432bc3 Mon Sep 17 00:00:00 2001 From: Guilherme Laurindo Schneck Date: Fri, 10 Apr 2026 21:20:09 +0200 Subject: [PATCH 10/10] ftespnow: Fix .ftespnow-client/manifest.py and .ftespnow-server/manifest.py, code formatting, and fixed spelling mistakes. --- .../ftespnow/examples/client_side_example.py | 4 +- .../ftespnow/examples/server_side_example.py | 4 +- .../ftespnow-client/ftespnow/client.py | 109 ++++++++-------- .../ftespnow/ftespnow-client/manifest.py | 2 - .../ftespnow-server/ftespnow/server.py | 117 +++++++++--------- .../ftespnow/ftespnow-server/manifest.py | 2 - 6 files changed, 118 insertions(+), 120 deletions(-) diff --git a/micropython/ftespnow/examples/client_side_example.py b/micropython/ftespnow/examples/client_side_example.py index 7928c5ba8..ab72b3a1d 100644 --- a/micropython/ftespnow/examples/client_side_example.py +++ b/micropython/ftespnow/examples/client_side_example.py @@ -16,7 +16,7 @@ # Receive a message received_data = esp.receive_message() -if received_data == None: # Check if any data was received +if received_data is None: # Check if any data was received print("No message was received (timed out)") else: print(f"Here is the received data: {received_data}") @@ -51,4 +51,4 @@ # Write received data to a python dictionary data_dict = esp.receive_to_dict() -print(data_dict) \ No newline at end of file +print(data_dict) diff --git a/micropython/ftespnow/examples/server_side_example.py b/micropython/ftespnow/examples/server_side_example.py index 17a8bb70b..c07d9b9d3 100644 --- a/micropython/ftespnow/examples/server_side_example.py +++ b/micropython/ftespnow/examples/server_side_example.py @@ -14,7 +14,7 @@ # Receive a message received_data = esp.receive_message() -if received_data == None: # Check if any data was received +if received_data is None: # Check if any data was received print("No message was received (timed out)") else: print(f"Here is the received data: {received_data}") @@ -49,4 +49,4 @@ # Write received data to a python dictionary data_dict = esp.receive_to_dict() # Will return {} if no data was received -print(data_dict) \ No newline at end of file +print(data_dict) diff --git a/micropython/ftespnow/ftespnow-client/ftespnow/client.py b/micropython/ftespnow/ftespnow-client/ftespnow/client.py index f67d052a8..983cbaa88 100644 --- a/micropython/ftespnow/ftespnow-client/ftespnow/client.py +++ b/micropython/ftespnow/ftespnow-client/ftespnow/client.py @@ -5,7 +5,7 @@ class CLIENT: def __init__(self, *, timeout: int=5) -> None: self.esp = espnow.ESPNow() self.timeout = timeout - + def configure(self, *, timeout: int=5) -> None: self.timeout: int = timeout @@ -19,108 +19,108 @@ def send_message(self, data: str) -> bool: Send a string Args: - + data (str): Data to be sent Returns: - + bool: Confirmation flag (`True` if data was received, `False` otherwise) """ - + ack: bool = self.esp.send(self.peer, data) return ack - + def receive_message(self, recv_timeout :int= 5) -> list | None: """ Receive a string Args: - + recv_timeout (int, optional): Reception timeout. Defaults to 5. Returns: - + list | None: `[, ]` | `None` if no message is received """ - + received = self.esp.recv(recv_timeout) if received[0] == None: return return received - + def send_txt(self, filename: str) -> bool: """ Parse and send a `.txt` file as a `string` Args: - filename (str): Filepath of the desired file to be sent with file name and extention + filename (str): Filepath of the desired file to be sent with file name and extension Returns: - + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) """ - + with open(filename, 'r') as f: data: str = str(f.readlines()) sent: bool = self.send_message(data) return sent - + def send_json(self, filename: str, *, indent: int=4) -> bool: """ Parse and send a `.json` file as a `string` Args: - - filename (str): Filepath of the desired file to be sent with file name and extention - + + filename (str): Filepath of the desired file to be sent with file name and extension + indent (int, optional): Desired indent of the resulting parsed `string` (for formatting purposes). Defaults to 4. Returns: sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) """ - + with open(filename, 'r') as f: unparsed = json.load(f) parsed: str = json.dumps(unparsed, indent=indent) sent: bool = self.send_message(parsed) return sent - + def receive_to_txt(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.txt` file. - + **Will not write or create file if no data is received** - + Args: - + target_file (str): Filepath of the destination file for the received data with file name and extension. - + mode (str, optional): Editing mode - + - `r` - Read only - + - `w` - Write only (truncates file before writing) - + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) - + - `a` - Append to the end of the file (default) - + - `b` - Binary mode - + - `t` - Text mode - + - `+` - Update (read and write) - + Read `open()`_ for more information - + Returns: received (bool): Confirmation flag (`True` if data was received, `False` otherwise) - + .. _open(): https://docs.python.org/3/library/functions.html#open """ - + if ".txt" not in target_file: raise SyntaxError("File format must be .txt") try: received: bool = False @@ -133,42 +133,42 @@ def receive_to_txt(self, target_file: str, mode: str='a') -> bool: return not received except SyntaxError: raise - + def receive_to_json(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.json` file. - + **Will not write or create file if no data is received** - + Args: - + target_file (str): Filepath of the destination file for the received data with file name and extension. - + mode (str, optional): Editing mode - + - `r` - Read only - + - `w` - Write only (truncates file before writing) - + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) - + - `a` - Append to the end of the file (default) - + - `b` - Binary mode - + - `t` - Text mode - + - `+` - Update (read and write) - + Read `open()`_ for more information - + Returns: - + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) - + .. _open(): https://docs.python.org/3/library/functions.html#open """ - + if ".json" not in target_file: raise SyntaxError("File format must be .json") try: received: bool = False @@ -185,19 +185,20 @@ def receive_to_json(self, target_file: str, mode: str='a') -> bool: return not received except SyntaxError: raise - + def receive_to_dict(self) -> dict: """ Unparses received `string` into a `dict` object - + Args: None: - + Returns: unparsed (dict): `dictionary` object containing unparsed equivalent of the received `.json` """ + data: list | None = self.receive_message() if data == None: return {} mac: str = str(data[0]) @@ -206,4 +207,4 @@ def receive_to_dict(self) -> dict: "mac": mac, "message": message } - return unparsed \ No newline at end of file + return unparsed diff --git a/micropython/ftespnow/ftespnow-client/manifest.py b/micropython/ftespnow/ftespnow-client/manifest.py index 228008f22..459051869 100644 --- a/micropython/ftespnow/ftespnow-client/manifest.py +++ b/micropython/ftespnow/ftespnow-client/manifest.py @@ -4,6 +4,4 @@ ) require("ftespnow") -require("json") -require("espnow") package("ftespnow") \ No newline at end of file diff --git a/micropython/ftespnow/ftespnow-server/ftespnow/server.py b/micropython/ftespnow/ftespnow-server/ftespnow/server.py index 6623501be..b02e98c03 100644 --- a/micropython/ftespnow/ftespnow-server/ftespnow/server.py +++ b/micropython/ftespnow/ftespnow-server/ftespnow/server.py @@ -5,10 +5,10 @@ class SERVER: def __init__(self, *, timeout: int=5) -> None: self.esp = espnow.ESPNow() self.timeout = timeout - + def configure(self, *, timeout: int=5) -> None: self.timeout = timeout - + def send_message(self, peer: str, data: str) -> bool: """ Send a string @@ -16,14 +16,14 @@ def send_message(self, peer: str, data: str) -> bool: Args: peer (str): client's mac address - + data (str): Data to be sent Returns: - + bool: Confirmation flag (`True` if data was received, `False` otherwise) """ - + ack: bool = self.esp.send(peer, data) return ack @@ -32,96 +32,96 @@ def receive_message(self, recv_timeout :int= 5) -> list | None: Receive a string Args: - + recv_timeout (int, optional): Reception timeout. Defaults to 5. Returns: - + list | None: `[, ]` | `None` if no message is received """ - + received = self.esp.recv(recv_timeout) if received[0] == None: return return received - + def send_txt(self, peer: str, filename: str) -> bool: """ Parse and send a `.txt` file as a `string` Args: - + peer (str): client's mac address - filename (str): Filepath of the desired file to be sent with file name and extention + filename (str): Filepath of the desired file to be sent with file name and extension Returns: - + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) """ - + with open(filename, 'r') as f: data: str = str(f.readlines()) sent: bool = self.send_message(peer, data) return sent - + def send_json(self, peer: str, filename: str, *, indent: int=4) -> bool: """ Parse and send a `.json` file as a `string` Args: - + peer (str): client's mac address - - filename (str): Filepath of the desired file to be sent with file name and extention - + + filename (str): Filepath of the desired file to be sent with file name and extension + indent (int, optional): Desired indent of the resulting parsed `string` (for formatting purposes). Defaults to 4. Returns: - + sent (bool): Confirmation flag (`True` if data was received, `False` otherwise) """ - + with open(filename, 'r') as f: unparsed = json.load(f) parsed: str = json.dumps(unparsed, indent=indent) sent: bool = self.send_message(peer, parsed) return sent - + def receive_to_txt(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.txt` file. - + **Will not write or create file if no data is received** - + Args: - + target_file (str): Filepath of the destination file for the received data with file name and extension. - + mode (str, optional): Editing mode - + - `r` - Read only - + - `w` - Write only (truncates file before writing) - + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) - + - `a` - Append to the end of the file (default) - + - `b` - Binary mode - + - `t` - Text mode - + - `+` - Update (read and write) - + Read `open()`_ for more information - + Returns: - + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) - + .. _open(): https://docs.python.org/3/library/functions.html#open """ - + if ".txt" not in target_file: raise SyntaxError("File format must be .txt") try: data: list | None = self.receive_message() @@ -133,42 +133,42 @@ def receive_to_txt(self, target_file: str, mode: str='a') -> bool: return True except SyntaxError: raise - + def receive_to_json(self, target_file: str, mode: str='a') -> bool: """ Write received `string` into a `.json` file. - + **Will not write or create file if no data is received** - + Args: - + target_file (str): Filepath of the destination file for the received data with file name and extension. - + mode (str, optional): Editing mode - + - `r` - Read only - + - `w` - Write only (truncates file before writing) - + - `x` - Create a new file and open it for writing (raises `FileExistsError` if file already exists) - + - `a` - Append to the end of the file (default) - + - `b` - Binary mode - + - `t` - Text mode - + - `+` - Update (read and write) - + Read `open()`_ for more information - + Returns: - + received (bool): Confirmation flag (`True` if data was received, `False` otherwise) - + .. _open(): https://docs.python.org/3/library/functions.html#open """ - + if ".json" not in target_file: raise SyntaxError("File format must be .json") try: received: bool = False @@ -185,19 +185,20 @@ def receive_to_json(self, target_file: str, mode: str='a') -> bool: return not received except SyntaxError: raise - + def receive_to_dict(self) -> dict: """ Unparses received `string` into a `dict` object - + Args: None: - + Returns: unparsed (dict): `dictionary` object containing unparsed equivalent of the received `.json` """ + data: list | None = self.receive_message() if data == None: return {} mac: str = str(data[0]) @@ -206,4 +207,4 @@ def receive_to_dict(self) -> dict: "mac": mac, "message": message } - return unparsed \ No newline at end of file + return unparsed diff --git a/micropython/ftespnow/ftespnow-server/manifest.py b/micropython/ftespnow/ftespnow-server/manifest.py index f6522e1c8..3597500d8 100644 --- a/micropython/ftespnow/ftespnow-server/manifest.py +++ b/micropython/ftespnow/ftespnow-server/manifest.py @@ -4,6 +4,4 @@ ) require("ftespnow") -require("json") -require("espnow") package("ftespnow") \ No newline at end of file