WifiEspNow
ESP-NOW Arduino library for ESP8266 and ESP32
Loading...
Searching...
No Matches
WifiEspNow.h
Go to the documentation of this file.
1
7#ifndef WIFIESPNOW_H
8#define WIFIESPNOW_H
9
10#if defined(ARDUINO_ARCH_ESP8266)
11#include <ESP8266WiFi.h>
12#elif defined(ARDUINO_ARCH_ESP32)
13#include <WiFi.h>
14#endif
15
16#include <cstddef>
17#include <cstdint>
18
20static const int WIFIESPNOW_ALEN = 6;
21
23static const int WIFIESPNOW_KEYLEN = 16;
24
26static const int WIFIESPNOW_MAXMSGLEN = 250;
27
29 uint8_t mac[WIFIESPNOW_ALEN];
30 uint8_t channel;
31};
32
34enum class WifiEspNowSendStatus : uint8_t {
35 NONE = 0,
36 OK = 1,
37 FAIL = 2,
38};
39
41{
42public:
47 bool
48 begin();
49
51 void
52 end();
53
59 bool
60 setPrimaryKey(const uint8_t key[WIFIESPNOW_KEYLEN]);
61
68 int
69 listPeers(WifiEspNowPeerInfo* peers, int maxPeers) const;
70
76 bool
77 hasPeer(const uint8_t mac[WIFIESPNOW_ALEN]) const;
78
87 bool
88 addPeer(const uint8_t mac[WIFIESPNOW_ALEN], int channel = 0, const uint8_t key[WIFIESPNOW_KEYLEN] = nullptr,
89 #if defined(ARDUINO_ARCH_ESP8266)
90 int netif = 0
91#elif defined(ARDUINO_ARCH_ESP32)
92 int netif = ESP_IF_WIFI_AP
93#endif
94 );
95
101 bool
102 removePeer(const uint8_t mac[WIFIESPNOW_ALEN]);
103
104 using RxCallback = void (*)(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count, void* arg);
105
112 void
113 onReceive(RxCallback cb, void* arg);
114
122 bool
123 send(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count);
124
128 {
129 return m_txRes;
130 }
131
132private:
133 RxCallback m_rxCb = nullptr;
134 void* m_rxArg = nullptr;
136 bool m_ready = false;
137 friend class WifiEspNowInternal;
138};
139
142
143#endif // WIFIESPNOW_H
WifiEspNowSendStatus
Result of send operation.
Definition WifiEspNow.h:34
@ NONE
result unknown, send in progress
@ FAIL
sending failed
@ OK
unicast message acknowledged by peer; multicast message transmitted
WifiEspNowClass WifiEspNow
ESP-NOW API.
Definition WifiEspNow.cpp:14
Definition WifiEspNow.h:41
bool removePeer(const uint8_t mac[WIFIESPNOW_ALEN])
Remove a peer.
Definition WifiEspNow.cpp:142
void end()
Stop ESP-NOW.
Definition WifiEspNow.cpp:55
int listPeers(WifiEspNowPeerInfo *peers, int maxPeers) const
List current peers.
Definition WifiEspNow.cpp:74
bool addPeer(const uint8_t mac[WIFIESPNOW_ALEN], int channel=0, const uint8_t key[WIFIESPNOW_KEYLEN]=nullptr, int netif=ESP_IF_WIFI_AP)
Add a peer or change peer channel.
Definition WifiEspNow.cpp:109
void onReceive(RxCallback cb, void *arg)
Set receive callback.
Definition WifiEspNow.cpp:147
bool setPrimaryKey(const uint8_t key[WIFIESPNOW_KEYLEN])
Set primary key, also known as KOK or PMK.
Definition WifiEspNow.cpp:64
void(*)(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t *buf, size_t count, void *arg) RxCallback
Definition WifiEspNow.h:104
bool begin()
Initialize ESP-NOW.
Definition WifiEspNow.cpp:43
bool hasPeer(const uint8_t mac[WIFIESPNOW_ALEN]) const
Test whether peer exists.
Definition WifiEspNow.cpp:99
bool send(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t *buf, size_t count)
Send a message.
Definition WifiEspNow.cpp:153
WifiEspNowSendStatus getSendStatus() const
Retrieve status of last sent message.
Definition WifiEspNow.h:127
Definition WifiEspNow.cpp:16
Definition WifiEspNow.h:28
uint8_t channel
Definition WifiEspNow.h:30
uint8_t mac[WIFIESPNOW_ALEN]
Definition WifiEspNow.h:29