|
| 1 | +--- |
| 2 | +title: "" |
| 3 | +aliases: |
| 4 | + - tutorials/all/ESP-NOW.html |
| 5 | + - tutorials/all/espnow.md |
| 6 | +--- |
| 7 | + |
| 8 | +Detailed information about this class can be found in [espnow](/firmwareapi/pycom/network/espnow). |
| 9 | + |
| 10 | +### Sending and receiving messages to/from 2 remote devices: Peer 1 and Peer 2 |
| 11 | + |
| 12 | +The following example sets up a device which communicates with 2 remote Peers. Message exchange with Peer 1 is encrypted while with Peer 2 it is not. |
| 13 | + |
| 14 | +```python |
| 15 | + |
| 16 | +from network import WLAN |
| 17 | +from network import ESPNOW |
| 18 | +import binascii |
| 19 | +import time |
| 20 | + |
| 21 | +# Modify this variable to change the PMK to be used |
| 22 | +# Note: This must match with the value of ESPNOW_PMK from the next example. |
| 23 | +ESPNOW_PMK = "0123456789abcdef" |
| 24 | +# Modify this variable to change the MAC address of the remote Peer 1 |
| 25 | +# Note: This must match with the MAC address of the Peer 1 from the next example. |
| 26 | +ESPNOW_PEER_1_MAC = "112233445566" |
| 27 | +# Modify this variable to change the LMK to be used when communicating with remote Peer 1 |
| 28 | +# Note: This must match with the value of ESPNOW_PEER_LMK from the next example. |
| 29 | +ESPNOW_PEER_1_LMK = "0123456789123456" |
| 30 | +# Modify this variable to change the MAC address of the remote Peer 2 |
| 31 | +# Note: This must match with the MAC address of the Peer 2 from the next example. |
| 32 | +ESPNOW_PEER_2_MAC = "AABBCCDDEEFF" |
| 33 | + |
| 34 | + |
| 35 | +# The callback to be registered when a message has been sent to a Peer |
| 36 | +def espnow_tx(result): |
| 37 | + # "result" is the parameter in form of 2 element long tuple |
| 38 | + # "peer" is the Peer which the message has been sent to |
| 39 | + # "sent" is a boolean showing whether the message could be sent |
| 40 | + peer, sent = result |
| 41 | + mac = peer.addr() |
| 42 | + if(sent == False): |
| 43 | + print("Sending message to %s failed!" % binascii.hexlify(mac)) |
| 44 | + else: |
| 45 | + print("Message sent to: %s" % (binascii.hexlify(mac))) |
| 46 | + |
| 47 | +# The callback to be registered when a message has been received |
| 48 | +def espnow_rx(result): |
| 49 | + # "result" is the parameter in form of 3 element long tuple |
| 50 | + # "mac" is the MAC address of the sender |
| 51 | + # "peer" is the Peer which the message has been received from. If message has been received from a not registered Peer this parameter is None |
| 52 | + # "msg" is the payload from the received message |
| 53 | + mac, peer, msg = result |
| 54 | + # Accept and handle messages only from the registered Peers |
| 55 | + if(peer is not None): |
| 56 | + # Do something with the received message |
| 57 | + print("Message received from %s with content: %s" % (binascii.hexlify(mac), msg)) |
| 58 | + |
| 59 | +# The ESPNOW module needs that WLAN is initialized |
| 60 | +w = WLAN() |
| 61 | +# Initialize the ESPNOW module |
| 62 | +ESPNOW.init() |
| 63 | +print("ESP-NOW version: %s" % ESPNOW.version()) |
| 64 | + |
| 65 | +# Register the callback which will be called on TX |
| 66 | +ESPNOW.on_send(espnow_tx) |
| 67 | +# Register the callback which will be called on RX |
| 68 | +ESPNOW.on_recv(espnow_rx) |
| 69 | +# Configure the Primary Master Key which is used to encrypt the Local Master Key |
| 70 | +ESPNOW.pmk(ESPNOW_PMK) |
| 71 | + |
| 72 | +# Add Peer 1 |
| 73 | +p1 = ESPNOW.add_peer(ESPNOW_PEER_1_MAC) |
| 74 | +# Add Peer 2 |
| 75 | +p2 = ESPNOW.add_peer(ESPNOW_PEER_2_MAC) |
| 76 | +# Set the Local Master Key of Peer p1 |
| 77 | +p1.lmk(ESPNOW_PEER_1_LMK) |
| 78 | +# Do not set LMK for Peer 2, traffic is not encrypted |
| 79 | + |
| 80 | +# Get the number of registered Peers and how many is encrypted |
| 81 | +count = ESPNOW.peer_count() |
| 82 | +print("Number of Peers: %s, encrypted: %s" % (count[0], count[1])) |
| 83 | + |
| 84 | +# Sending some messages to the Peers individually |
| 85 | +i = 0 |
| 86 | +while i < 10: |
| 87 | + # The Peer 1 will only receive this message if the same PMK and LMK is configured for the Peer object created for this device on the other device also. Check the next examples. |
| 88 | + p1.send("%s" % (i)) |
| 89 | + # Message to Peer 2 is not encrypted, on the Peer 2 device encryption for this current device's Peer object should not be configured. Check the next examples. |
| 90 | + p2.send("%s" % (i)) |
| 91 | + i = i + 1 |
| 92 | + time.sleep(1) |
| 93 | + |
| 94 | +# Sending 1 message to all Peers which are registered |
| 95 | +ESPNOW.send(None, "Hello all Peers!") |
| 96 | + |
| 97 | +# Remove Peer 1 |
| 98 | +ESPNOW.del_peer(p1) |
| 99 | + |
| 100 | +# Deinit the module |
| 101 | +ESPNOW.deinit() |
| 102 | + |
| 103 | +``` |
| 104 | + |
| 105 | +### Example code for Peer 1 |
| 106 | +This example code implements Peer 1 device from the previous example. |
| 107 | + |
| 108 | +```python |
| 109 | +from network import WLAN |
| 110 | +from network import ESPNOW |
| 111 | +import binascii |
| 112 | +import time |
| 113 | + |
| 114 | +# Modify this variable to change the PMK to be used |
| 115 | +# Note: This must match with the value of ESPNOW_PMK from the first example. |
| 116 | +ESPNOW_PMK = "0123456789abcdef" |
| 117 | +# Modify this variable to change the MAC address of the remote Peer. |
| 118 | +# Note: This must match with the MAC address of the Peer from the first example. |
| 119 | +ESPNOW_PEER_MAC = "665544332211" |
| 120 | +# Modify this variable to change the LMK to be used when communicating with the remote Peer |
| 121 | +# Note: This must match with the value of ESPNOW_PEER_1_LMK from the first example. |
| 122 | +ESPNOW_PEER_LMK = "0123456789123456" |
| 123 | + |
| 124 | +def tx(result): |
| 125 | + peer, sent = result |
| 126 | + mac = peer.addr() |
| 127 | + print("Sending to: %s - %r" % (binascii.hexlify(mac),sent)) |
| 128 | + |
| 129 | +def rx(result): |
| 130 | + print("Message received:") |
| 131 | + mac, peer, msg = result |
| 132 | + # Accept messages only from the registered Peer |
| 133 | + if(peer is not None): |
| 134 | + print("Received: %s - %s" % (binascii.hexlify(mac),msg)) |
| 135 | + # Send back a response |
| 136 | + peer.send("Message received: %s" % msg) |
| 137 | + |
| 138 | +w = WLAN() |
| 139 | +ESPNOW.init() |
| 140 | +ESPNOW.on_send(tx) |
| 141 | +ESPNOW.on_recv(rx) |
| 142 | +ESPNOW.pmk(ESPNOW_PMK) |
| 143 | + |
| 144 | +# Add the Peer |
| 145 | +p = ESPNOW.add_peer(ESPNOW_PEER_MAC) |
| 146 | +# Configure the LMK to be used during communication. |
| 147 | +p.lmk(ESPNOW_PEER_LMK) |
| 148 | + |
| 149 | +``` |
| 150 | + |
| 151 | +### Example code for Peer 2 |
| 152 | +This example code implements Peer 2 device from the first example. |
| 153 | + |
| 154 | +```python |
| 155 | +from network import WLAN |
| 156 | +from network import ESPNOW |
| 157 | +import binascii |
| 158 | +import time |
| 159 | + |
| 160 | +# Modify this variable to change the MAC address of the remote Peer. |
| 161 | +# Note: This must match with the MAC address of the Peer from the first example. |
| 162 | +ESPNOW_PEER_MAC = "665544332211" |
| 163 | + |
| 164 | +def tx(result): |
| 165 | + peer, sent = result |
| 166 | + mac = peer.addr() |
| 167 | + print("Sending to: %s - %r" % (binascii.hexlify(mac),sent)) |
| 168 | + |
| 169 | +def rx(result): |
| 170 | + print("Message received:") |
| 171 | + mac, peer, msg = result |
| 172 | + # Accept messages only from the registered Peer |
| 173 | + if(peer is not None): |
| 174 | + print("Received: %s - %s" % (binascii.hexlify(mac),msg)) |
| 175 | + # Send back a response |
| 176 | + peer.send("Message received: %s" % msg) |
| 177 | + |
| 178 | +w = WLAN() |
| 179 | +# Init ESP-NOW, no PMK is configured. |
| 180 | +ESPNOW.init() |
| 181 | +ESPNOW.on_send(tx) |
| 182 | +ESPNOW.on_recv(rx) |
| 183 | + |
| 184 | +# Add the Peer. No LMK will be configured for it. |
| 185 | +p = ESPNOW.add_peer(ESPNOW_PEER_MAC) |
| 186 | + |
| 187 | +``` |
| 188 | + |
| 189 | +### Example snippet for RX callback |
| 190 | +This example snippet shows how it may be handled when the device receives a message from a previously not registered Peer. |
| 191 | + |
| 192 | +```python |
| 193 | + |
| 194 | +# The callback to be registered when a message has been received |
| 195 | +def espnow_rx(result): |
| 196 | + # "result" is the parameter in form of 3 element long tuple |
| 197 | + # "mac" is the MAC address of the sender |
| 198 | + # "peer" is the Peer which the message has been received from. If message has been received from a not registered Peer this parameter is None |
| 199 | + # "msg" is the payload from the received message |
| 200 | + mac, peer, msg = result |
| 201 | + if(peer is None): |
| 202 | + print("Message received from an unknown Peer, registering...") |
| 203 | + peer = ESPNOW.add_peer(mac) |
| 204 | + print("Message received from %s with content: %s" % (binascii.hexlify(mac), msg)) |
| 205 | + peer.send("Sending back an answer...") |
| 206 | + |
| 207 | +``` |
0 commit comments