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#if defined(ARDUINO_ARCH_ESP8266)
88 bool
89 addPeer(const uint8_t mac[WIFIESPNOW_ALEN], int channel = 0, const uint8_t key[WIFIESPNOW_KEYLEN] = nullptr);
90#elif defined(ARDUINO_ARCH_ESP32)
91 bool
92 addPeer(const uint8_t mac[WIFIESPNOW_ALEN], int channel = 0, const uint8_t key[WIFIESPNOW_KEYLEN] = nullptr, int netif = ESP_IF_WIFI_AP);
93#endif
94
100 bool
101 removePeer(const uint8_t mac[WIFIESPNOW_ALEN]);
102
103 using RxCallback = void (*)(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count, void* arg);
104
111 void
112 onReceive(RxCallback cb, void* arg);
113
121 bool
122 send(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count);
123
127 {
128 return m_txRes;
129 }
130
131private:
132 static void
133 rx(const uint8_t* mac, const uint8_t* data, uint8_t len);
134
135 static void
136 tx(const uint8_t* mac, uint8_t status);
137
138private:
139 RxCallback m_rxCb = nullptr;
140 void* m_rxArg = nullptr;
142 bool m_ready = false;
143};
144
147
148#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:126
void end()
Stop ESP-NOW.
Definition WifiEspNow.cpp:30
int listPeers(WifiEspNowPeerInfo *peers, int maxPeers) const
List current peers.
Definition WifiEspNow.cpp:49
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:101
void onReceive(RxCallback cb, void *arg)
Set receive callback.
Definition WifiEspNow.cpp:131
bool setPrimaryKey(const uint8_t key[WIFIESPNOW_KEYLEN])
Set primary key, also known as KOK or PMK.
Definition WifiEspNow.cpp:39
void(*)(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t *buf, size_t count, void *arg) RxCallback
Definition WifiEspNow.h:103
bool begin()
Initialize ESP-NOW.
Definition WifiEspNow.cpp:17
bool hasPeer(const uint8_t mac[WIFIESPNOW_ALEN]) const
Test whether peer exists.
Definition WifiEspNow.cpp:74
bool send(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t *buf, size_t count)
Send a message.
Definition WifiEspNow.cpp:137
WifiEspNowSendStatus getSendStatus() const
Retrieve status of last sent message.
Definition WifiEspNow.h:126
Definition WifiEspNow.h:28
uint8_t channel
Definition WifiEspNow.h:30
uint8_t mac[WIFIESPNOW_ALEN]
Definition WifiEspNow.h:29