Skip to content

Commit 3a8c6d1

Browse files
committed
ftespnow: Added examples and updated docstrings
1 parent f80a21d commit 3a8c6d1

4 files changed

Lines changed: 119 additions & 4 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import ftespnow
2+
3+
# Initialize ESP-NOW client
4+
esp = ftespnow.CLIENT()
5+
6+
# Connect to ESP-NOW server
7+
esp.connect("a4f00f772d15") # Change to actual server mac address
8+
9+
# Send a message
10+
message = "Hello"
11+
sent = esp.send_message(message)
12+
if sent: # Check if server received the data
13+
print("Message received by server")
14+
else:
15+
print("Message not received by server")
16+
17+
# Receive a message
18+
received_data = esp.receive_message()
19+
if received_data == None: # Check if any data was received
20+
print("No message was received (timed out)")
21+
else:
22+
print(f"Here is the received data: {received_data}")
23+
24+
# Send a .txt file
25+
txt_sent = esp.send_txt("filepath/filename.txt")
26+
if txt_sent: # Check if server received the data
27+
print("File received by server")
28+
else:
29+
print("File not received by server")
30+
31+
# Send a .json file
32+
json_sent = esp.send_json("filepath/filename.json")
33+
if json_sent: # Check if server received the data
34+
print("File received by server")
35+
else:
36+
print("File not received by server")
37+
38+
# Write received data to .txt file
39+
txt_received = esp.receive_to_txt("filepath/filename.txt", mode='w') # Set mode to 'w' so file is truncated before writing
40+
if txt_received:
41+
print("File received successfully")
42+
else:
43+
print("No file received. Destination file was not created/modified")
44+
45+
# Write received data to .json file
46+
json_received = esp.receive_to_json("filepath/filename.json", mode='w') # Set mode to 'w' so file is truncated before writing
47+
if json_received:
48+
print("File received successfully")
49+
else:
50+
print("No file received. Destination file was not created/modified")
51+
52+
# Write received data to a python dictionary
53+
data_dict = esp.receive_to_dict()
54+
print(data_dict)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import ftespnow
2+
3+
# Initialize ESP-NOW client
4+
esp = ftespnow.SERVER()
5+
6+
# Send a message
7+
message = "Hello"
8+
peer = "a4f00f772d15" # Mac address of the client that you want to send data to
9+
sent = esp.send_message(peer, message)
10+
if sent: # Check if client received the data
11+
print("Message received by clientclient")
12+
else:
13+
print("Message not received by client")
14+
15+
# Receive a message
16+
received_data = esp.receive_message()
17+
if received_data == None: # Check if any data was received
18+
print("No message was received (timed out)")
19+
else:
20+
print(f"Here is the received data: {received_data}")
21+
22+
# Send a .txt file
23+
txt_sent = esp.send_txt(peer, "filepath/filename.txt")
24+
if txt_sent: # Check if client received the data
25+
print("File received by client")
26+
else:
27+
print("File not received by client")
28+
29+
# Send a .json file
30+
json_sent = esp.send_json(peer, "filepath/filename.json")
31+
if json_sent: # Check if client received the data
32+
print("File received by client")
33+
else:
34+
print("File not received by client")
35+
36+
# Write received data to .txt file
37+
txt_received = esp.receive_to_txt("filepath/filename.txt", mode='w') # Set mode to 'w' so file is truncated before writing
38+
if txt_received:
39+
print("File received successfully")
40+
else:
41+
print("No file received. Destination file was not created/modified")
42+
43+
# Write received data to .json file
44+
json_received = esp.receive_to_json("filepath/filename.json", mode='w') # Set mode to 'w' so file is truncated before writing
45+
if json_received: # Check if any data was received
46+
print("File received successfully")
47+
else:
48+
print("No file received. Destination file was not created/modified")
49+
50+
# Write received data to a python dictionary
51+
data_dict = esp.receive_to_dict() # Will return {} if no data was received
52+
print(data_dict)

micropython/ftespnow/ftespnow-client/ftespnow/client.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import json
33

44
class CLIENT:
5-
def __init__(self) -> None:
5+
def __init__(self, *, timeout: int=5) -> None:
66
self.esp = espnow.ESPNow()
7+
self.timeout = timeout
78

8-
def configure(self, timeout=5) -> None:
9+
def configure(self, *, timeout: int=5) -> None:
910
self.timeout: int = timeout
1011

1112
def connect(self, peer: str) -> None:
@@ -87,7 +88,9 @@ def send_json(self, filename: str, *, indent: int=4) -> bool:
8788

8889
def receive_to_txt(self, target_file: str, mode: str='a') -> bool:
8990
"""
90-
Write received `string` into a `.txt` file.
91+
Write received `string` into a `.txt` file.
92+
93+
**Will not write or create file if no data is received**
9194
9295
Args:
9396
@@ -112,7 +115,7 @@ def receive_to_txt(self, target_file: str, mode: str='a') -> bool:
112115
Read `open()`_ for more information
113116
114117
Returns:
115-
118+
116119
received (bool): Confirmation flag (`True` if data was received, `False` otherwise)
117120
118121
.. _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:
135138
"""
136139
Write received `string` into a `.json` file.
137140
141+
**Will not write or create file if no data is received**
142+
138143
Args:
139144
140145
target_file (str): Filepath of the destination file for the received data with file name and extension.

micropython/ftespnow/ftespnow-server/ftespnow/server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def receive_to_txt(self, target_file: str, mode: str='a') -> bool:
9191
"""
9292
Write received `string` into a `.txt` file.
9393
94+
**Will not write or create file if no data is received**
95+
9496
Args:
9597
9698
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:
136138
"""
137139
Write received `string` into a `.json` file.
138140
141+
**Will not write or create file if no data is received**
142+
139143
Args:
140144
141145
target_file (str): Filepath of the destination file for the received data with file name and extension.

0 commit comments

Comments
 (0)