├── readme.md ├── .gitignore └── esp_now ├── examples ├── Bot │ ├── Bot.ino │ ├── espnow.h │ └── espnow.cpp └── Remoter │ ├── Remoter.ino │ ├── espnow.h │ └── espnow.cpp ├── espnow.h └── espnow.cpp /readme.md: -------------------------------------------------------------------------------- 1 | # M5-espnow 2 | 3 | M5-espnow可以让通信更加方便的使用。 4 | 5 | M5-espnow是基于乐鑫ESPNOW做的二次封装的库,使得在M5产品上使用更容易简单。(注意:ESPNOW规定每次发送最多不超过250个字节) 6 | 7 | ## 1 效果演示(Results demonstrate) 8 | 9 | 点击链接:[https://www.youtube.com/watch?v=mQL8gUF1cYA](https://www.youtube.com/watch?v=mQL8gUF1cYA) 10 | 11 | ## 2 使用方式(demo) 12 | 13 | [点击例程](https://github.com/m5stack/M5-espnow/tree/master/esp_now) 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /esp_now/examples/Bot/Bot.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "espnow.h" 3 | 4 | Espnow espnow; 5 | // callback when data is sent from Master to Slave 6 | void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) 7 | { 8 | 9 | } 10 | 11 | void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) 12 | { 13 | if(espnow.OnBotRecv(mac_addr,data,data_len)) 14 | return; 15 | } 16 | void setup() { 17 | M5.begin(); 18 | espnow.BotInit(); 19 | esp_now_register_recv_cb(OnDataRecv); 20 | esp_now_register_send_cb(OnDataSent); 21 | } 22 | 23 | void loop() { 24 | espnow.BotConnectUpdate(); 25 | } 26 | -------------------------------------------------------------------------------- /esp_now/examples/Remoter/Remoter.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include"espnow.h" 3 | Espnow espnow; 4 | 5 | void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) 6 | { 7 | 8 | } 9 | 10 | void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) 11 | { 12 | if(espnow.OnRemotRecv(mac_addr,data,data_len)){ 13 | return; 14 | } 15 | } 16 | 17 | 18 | void setup() { 19 | // put your setup code here, to run once: 20 | 21 | m5.begin(); 22 | espnow.RemoteInit(); 23 | esp_now_register_recv_cb(OnDataRecv); 24 | esp_now_register_send_cb(OnDataSent); 25 | 26 | } 27 | void loop() { 28 | espnow.RemoteConnectUpdate(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /esp_now/espnow.h: -------------------------------------------------------------------------------- 1 | #ifndef __ESPNOW_H__ 2 | #define __ESPNOW_H__ 3 | #define bot 1 4 | #include 5 | #include 6 | #include "Arduino.h" 7 | #define CHANNEL 1 8 | #define PRINTSCANRESULTS 0 9 | 10 | 11 | class Espnow { 12 | 13 | 14 | public: 15 | Espnow(); 16 | void BotInit(void); 17 | void RemoteInit(void); 18 | 19 | void OnRemotSent(const uint8_t *mac_addr, esp_now_send_status_t status); 20 | int8_t OnRemotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len); 21 | 22 | void OnBotSent(const uint8_t *mac_addr, esp_now_send_status_t status); 23 | int8_t OnBotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len); 24 | 25 | void RemoteConnectUpdate(void); 26 | void BotConnectUpdate(void); 27 | 28 | //uint8_t peer_addr[6]= {0xB4, 0xE6, 0x2D, 0x8B, 0x7D, 0x42}; 29 | public: 30 | uint8_t peer_addr[6];//= {0xB4, 0xE6, 0x2D, 0x8B, 0x7D, 0x42}; 31 | esp_now_peer_info_t slave; 32 | private: 33 | 34 | void ConfigDeviceAP(void); 35 | void InitEspNow(void); 36 | void ScanForSlave(void); 37 | void manageSlave(void); 38 | void sendData(void); 39 | 40 | private: 41 | Preferences preferences; 42 | uint8_t sta_addr[6]; 43 | //esp_now_peer_info_t slave; 44 | esp_now_peer_info_t slaves[20] = {}; 45 | int SlaveCnt = 0; 46 | bool connectflag = false; 47 | uint8_t connect_addr[20][6]; 48 | int connect_num; 49 | //uint8_t peer_addr[6]= {0xB4, 0xE6, 0x2D, 0x8B, 0x7D, 0x42}; 50 | }; 51 | #endif 52 | -------------------------------------------------------------------------------- /esp_now/examples/Bot/espnow.h: -------------------------------------------------------------------------------- 1 | #ifndef __ESPNOW_H__ 2 | #define __ESPNOW_H__ 3 | #define bot 1 4 | #include 5 | #include 6 | #include "Arduino.h" 7 | #define CHANNEL 1 8 | #define PRINTSCANRESULTS 0 9 | 10 | 11 | class Espnow { 12 | 13 | 14 | public: 15 | Espnow(); 16 | void BotInit(void); 17 | void RemoteInit(void); 18 | 19 | void OnRemotSent(const uint8_t *mac_addr, esp_now_send_status_t status); 20 | int8_t OnRemotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len); 21 | 22 | void OnBotSent(const uint8_t *mac_addr, esp_now_send_status_t status); 23 | int8_t OnBotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len); 24 | 25 | void RemoteConnectUpdate(void); 26 | void BotConnectUpdate(void); 27 | 28 | //uint8_t peer_addr[6]= {0xB4, 0xE6, 0x2D, 0x8B, 0x7D, 0x42}; 29 | public: 30 | uint8_t peer_addr[6];//= {0xB4, 0xE6, 0x2D, 0x8B, 0x7D, 0x42}; 31 | esp_now_peer_info_t slave; 32 | private: 33 | 34 | void ConfigDeviceAP(void); 35 | void InitEspNow(void); 36 | void ScanForSlave(void); 37 | void manageSlave(void); 38 | void sendData(void); 39 | 40 | private: 41 | Preferences preferences; 42 | uint8_t sta_addr[6]; 43 | //esp_now_peer_info_t slave; 44 | esp_now_peer_info_t slaves[20] = {}; 45 | int SlaveCnt = 0; 46 | bool connectflag = false; 47 | uint8_t connect_addr[20][6]; 48 | int connect_num; 49 | //uint8_t peer_addr[6]= {0xB4, 0xE6, 0x2D, 0x8B, 0x7D, 0x42}; 50 | }; 51 | #endif 52 | -------------------------------------------------------------------------------- /esp_now/examples/Remoter/espnow.h: -------------------------------------------------------------------------------- 1 | #ifndef __ESPNOW_H__ 2 | #define __ESPNOW_H__ 3 | #define bot 1 4 | #include 5 | #include 6 | #include "Arduino.h" 7 | #define CHANNEL 1 8 | #define PRINTSCANRESULTS 0 9 | 10 | 11 | class Espnow { 12 | 13 | 14 | public: 15 | Espnow(); 16 | void BotInit(void); 17 | void RemoteInit(void); 18 | 19 | void OnRemotSent(const uint8_t *mac_addr, esp_now_send_status_t status); 20 | int8_t OnRemotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len); 21 | 22 | void OnBotSent(const uint8_t *mac_addr, esp_now_send_status_t status); 23 | int8_t OnBotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len); 24 | 25 | void RemoteConnectUpdate(void); 26 | void BotConnectUpdate(void); 27 | 28 | //uint8_t peer_addr[6]= {0xB4, 0xE6, 0x2D, 0x8B, 0x7D, 0x42}; 29 | public: 30 | uint8_t peer_addr[6];//= {0xB4, 0xE6, 0x2D, 0x8B, 0x7D, 0x42}; 31 | esp_now_peer_info_t slave; 32 | private: 33 | 34 | void ConfigDeviceAP(void); 35 | void InitEspNow(void); 36 | void ScanForSlave(void); 37 | void manageSlave(void); 38 | void sendData(void); 39 | 40 | private: 41 | Preferences preferences; 42 | uint8_t sta_addr[6]; 43 | //esp_now_peer_info_t slave; 44 | esp_now_peer_info_t slaves[20] = {}; 45 | int SlaveCnt = 0; 46 | bool connectflag = false; 47 | uint8_t connect_addr[20][6]; 48 | int connect_num; 49 | //uint8_t peer_addr[6]= {0xB4, 0xE6, 0x2D, 0x8B, 0x7D, 0x42}; 50 | }; 51 | #endif 52 | -------------------------------------------------------------------------------- /esp_now/examples/Bot/espnow.cpp: -------------------------------------------------------------------------------- 1 | #include"espnow.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | Espnow::Espnow(void) 8 | { 9 | 10 | } 11 | 12 | void Espnow::BotInit(void){ 13 | 14 | //m5.begin(); 15 | //Serial.begin(115200); 16 | 17 | pinMode(37, INPUT); 18 | pinMode(38, INPUT); 19 | pinMode(39, INPUT); 20 | //digitalWrite(37, HIGH); 21 | // digitalWrite(38, HIGH); 22 | //digitalWrite(39, HIGH); 23 | 24 | WiFi.mode(WIFI_STA); 25 | Serial.println("ESPNow Master Example"); 26 | // This is the mac address of the Master in Station Mode 27 | Serial.print("STA MAC: "); 28 | Serial.println(WiFi.macAddress()); 29 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 30 | //WiFi.softAPmacAddress() 31 | //if(!bot) 32 | sscanf(WiFi.macAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 33 | //else 34 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 35 | //WiFi.softAPmacAddress() 36 | //WiFi.softAPmacAddress() 37 | InitEspNow(); 38 | 39 | preferences.begin("my-app", false); 40 | String mac_addr; 41 | mac_addr = preferences.getString("mac_addr", "error"); 42 | Serial.print("mac_addr = ");Serial.print(mac_addr); 43 | 44 | sscanf(mac_addr.c_str(), "%x:%x:%x:%x:%x:%x%c", &peer_addr[0], &peer_addr[1], &peer_addr[2], &peer_addr[3], &peer_addr[4], &peer_addr[5]); 45 | 46 | for (int i = 0; i < 6; ++i ){ 47 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 48 | } 49 | slave.channel = CHANNEL; 50 | esp_now_add_peer(&slave); 51 | } 52 | 53 | void Espnow::RemoteInit(void) 54 | { 55 | //m5.begin(); 56 | //Serial.begin(115200); 57 | 58 | pinMode(37, INPUT); 59 | pinMode(38, INPUT); 60 | pinMode(39, INPUT); 61 | //digitalWrite(37, HIGH); 62 | // digitalWrite(38, HIGH); 63 | //digitalWrite(39, HIGH); 64 | 65 | WiFi.mode(WIFI_AP_STA); 66 | //WiFi.mode(WIFI_AP); 67 | ConfigDeviceAP(); 68 | Serial.println("ESPNow Master Example"); 69 | // This is the mac address of the Master in Station Mode 70 | Serial.print(" AP MAC: "); 71 | Serial.println(WiFi.softAPmacAddress()); 72 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 73 | //WiFi.softAPmacAddress() 74 | //if(!bot) 75 | sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 76 | //else 77 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 78 | //WiFi.softAPmacAddress() 79 | //WiFi.softAPmacAddress() 80 | InitEspNow(); 81 | 82 | preferences.begin("my-app", false); 83 | String mac_addr; 84 | mac_addr = preferences.getString("mac_addr", "error"); 85 | Serial.print("mac_addr = ");Serial.print(mac_addr); 86 | 87 | sscanf(mac_addr.c_str(), "%x:%x:%x:%x:%x:%x%c", &peer_addr[0], &peer_addr[1], &peer_addr[2], &peer_addr[3], &peer_addr[4], &peer_addr[5]); 88 | 89 | for (int i = 0; i < 6; ++i ){ 90 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 91 | } 92 | slave.channel = CHANNEL; 93 | esp_now_add_peer(&slave); 94 | } 95 | 96 | // config AP SSID 97 | void Espnow::ConfigDeviceAP(void) 98 | { 99 | 100 | String Mac = WiFi.macAddress(); 101 | String SSID = "Slave:"+ Mac; 102 | bool result = WiFi.softAP(SSID.c_str(), "12345678", CHANNEL, 0); 103 | if (!result) 104 | { 105 | Serial.println("AP Config failed."); 106 | } else 107 | { 108 | Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID)); 109 | } 110 | } 111 | 112 | // Init ESP Now with fallback 113 | void Espnow::InitEspNow(void) 114 | { 115 | if (esp_now_init() == ESP_OK) 116 | { 117 | Serial.println("ESPNow Init Success"); 118 | } 119 | else 120 | { 121 | Serial.println("ESPNow Init Failed"); 122 | ESP.restart(); 123 | } 124 | } 125 | 126 | void Espnow::OnRemotSent(const uint8_t *mac_addr, esp_now_send_status_t status) 127 | { 128 | 129 | } 130 | int8_t Espnow::OnRemotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len){ 131 | 132 | if(data_len == 4) 133 | { 134 | connectflag = true; 135 | } 136 | if(data_len == 6) 137 | { 138 | int i = 0; 139 | for(i = 0; i < connect_num; i++) 140 | { 141 | int j = 0; 142 | for(j = 0; j < 6; j++) 143 | { 144 | if(connect_addr[i][j] != data[j]) 145 | { 146 | break; 147 | } 148 | } 149 | //Serial.printf("j = %d",j); 150 | if(j == 6) 151 | { 152 | break; 153 | } 154 | } 155 | 156 | if(i == connect_num) 157 | { 158 | for(int i = 0; i < 6;i++) 159 | { 160 | connect_addr[connect_num][i] = data[i]; 161 | } 162 | connect_num++; 163 | } 164 | } 165 | if(connect_num> 20) connect_num = 0; 166 | 167 | 168 | int mac_comp = 0; 169 | for(mac_comp = 0; mac_comp < 6; mac_comp++){ 170 | if(mac_addr[mac_comp] != peer_addr[mac_comp]){ 171 | return 1; 172 | } 173 | } 174 | return 0; 175 | } 176 | 177 | void Espnow::OnBotSent(const uint8_t *mac_addr, esp_now_send_status_t status) 178 | { 179 | } 180 | int8_t Espnow::OnBotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) 181 | { 182 | Serial.print("connectflag = ");Serial.println(connectflag); 183 | if ((data_len == 7) && (!connectflag)) 184 | { 185 | connectflag = true; 186 | int i = 0; 187 | for (i = 0; i < connect_num; i++) 188 | { 189 | int j = 0; 190 | for (j = 0; j < 6; j++) 191 | { 192 | if (connect_addr[i][j] != mac_addr[j]) 193 | { 194 | break; 195 | } 196 | } 197 | //Serial.printf("j = %d",j); 198 | if (j == 6) 199 | { 200 | break; 201 | } 202 | } 203 | if (i == connect_num) 204 | { 205 | for (int i = 0; i < 6; i++) 206 | { 207 | connect_addr[connect_num][i] = mac_addr[i]; 208 | } 209 | connect_num++; 210 | } 211 | } 212 | //if (connect_num > 20) connect_num = 0; 213 | 214 | int mac_comp = 0; 215 | for(mac_comp = 0; mac_comp < 6; mac_comp++){ 216 | if(mac_addr[mac_comp] != peer_addr[mac_comp]){ 217 | return 1; 218 | } 219 | } 220 | return 0; 221 | } 222 | 223 | void Espnow::RemoteConnectUpdate(){ 224 | 225 | static int updatetime; 226 | if (digitalRead(38) == LOW) 227 | { 228 | updatetime++; 229 | } 230 | //Serial.println(updatetime); 231 | if (updatetime >= 300){ 232 | updatetime = 0; 233 | delay(200); 234 | M5.Speaker.tone(80, 200); 235 | delay(200); 236 | M5.Lcd.fillScreen(TFT_BLACK); 237 | M5.Speaker.mute(); 238 | int ypos = 0; 239 | int old_ypos = 0; 240 | connect_num = 0; 241 | connectflag = false; 242 | while(true) 243 | { 244 | M5.Lcd.setCursor(0, 0, 4); 245 | M5.Lcd.println("LidarBot Scanning...."); 246 | for(int i = 0; i < connect_num; i++) 247 | { 248 | M5.Lcd.setCursor(30, 25 + 25 * i); // Set cursor near top left corner of screen 249 | M5.Lcd.printf("%02x:%02x:%02x:%02x:%02x:%02x", connect_addr[i][0], connect_addr[i][1], connect_addr[i][2], connect_addr[i][3], connect_addr[i][4], connect_addr[i][5]); 250 | } 251 | if(connect_num){ 252 | M5.Lcd.setCursor(270, 25 + ypos * 25); 253 | M5.Lcd.println("<<"); 254 | } 255 | 256 | if(ypos < 1){ 257 | //M5.Lcd.fillScreen(TFT_BLACK); 258 | if(old_ypos != ypos) 259 | M5.Lcd.fillRect(30, 210, 90, 50, TFT_BLACK); 260 | M5.Lcd.setCursor(30, 210); 261 | M5.Lcd.println("update"); 262 | } 263 | else 264 | { 265 | if(old_ypos != ypos) 266 | M5.Lcd.fillRect(30, 210, 100, 50, TFT_BLACK); 267 | //M5.Lcd.fillScreen(TFT_BLACK); 268 | M5.Lcd.setCursor(50, 210); 269 | M5.Lcd.println("up"); 270 | } 271 | old_ypos = ypos; 272 | M5.Lcd.setCursor(125, 210); 273 | M5.Lcd.println("down"); 274 | M5.Lcd.setCursor(220, 210); 275 | M5.Lcd.println("select"); 276 | 277 | 278 | //select down 279 | if ((digitalRead(37) == LOW) && (connect_num)) 280 | { 281 | M5.Lcd.fillScreen(TFT_BLACK); 282 | for (int i = 0; i < 6; ++i ){ 283 | peer_addr[i] = (uint8_t) connect_addr[ypos][i]; 284 | } 285 | //Serial.printf("%2x:%2x:%2x:%2x:%2x:%2x",slave.peer_addr[0],slave.peer_addr[1],slave.peer_addr[2], slave.peer_addr[3],slave.peer_addr[4], slave.peer_addr[5]); 286 | for (int i = 0; i < 6; ++i ){ 287 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 288 | } 289 | slave.channel = CHANNEL; 290 | esp_now_add_peer(&slave); 291 | M5.Lcd.setCursor(140, 210); 292 | M5.Lcd.println("Quit"); 293 | while(!connectflag){ 294 | //for(int i = 0;i < 6; i++){ 295 | esp_err_t result = esp_now_send(slave.peer_addr, sta_addr, sizeof(sta_addr)+ 1); 296 | Serial.print("Send Status: "); 297 | if (result == ESP_OK) { 298 | Serial.println("Success"); 299 | }else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 300 | Serial.println("ESPNOW not Init."); 301 | } else if (result == ESP_ERR_ESPNOW_ARG) { 302 | Serial.println("Invalid Argument"); 303 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 304 | Serial.println("Internal Error"); 305 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 306 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 307 | }else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 308 | Serial.println("Peer not found."); 309 | }else { 310 | Serial.println("Not sure what happened"); 311 | } 312 | delay(100); 313 | // } 314 | 315 | 316 | 317 | // while(!connectflag) 318 | // { 319 | if(digitalRead(38) == LOW) break; 320 | } 321 | if(connectflag){ 322 | char macStr[18]; 323 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", peer_addr[0], peer_addr[1], peer_addr[2], peer_addr[3], peer_addr[4], peer_addr[5]); 324 | String mac_addr = (char *)macStr; 325 | preferences.putString("mac_addr", mac_addr); 326 | delay(100); 327 | M5.Lcd.fillScreen(TFT_BLACK); 328 | break; 329 | } 330 | } 331 | 332 | 333 | delay(100); 334 | if (digitalRead(39) == LOW && (connect_num) ) 335 | { 336 | M5.Lcd.fillScreen(TFT_BLACK); 337 | ypos--; 338 | if(ypos <= 0) 339 | { 340 | ypos = 0; 341 | } 342 | } 343 | 344 | if (digitalRead(38) == LOW && (connect_num)) 345 | { 346 | // Serial.println("IdigitalRead(38) == LOW"); 347 | //M5.Lcd.print("39 low"); 348 | M5.Lcd.fillScreen(TFT_BLACK); 349 | ypos++; 350 | if(ypos >= connect_num - 1) 351 | { 352 | ypos = connect_num - 1; 353 | } 354 | } 355 | 356 | } 357 | } 358 | } 359 | 360 | 361 | void Espnow::ScanForSlave(void) { 362 | int8_t scanResults = WiFi.scanNetworks(); 363 | //reset slaves 364 | memset(slaves, 0, sizeof(slaves)); 365 | SlaveCnt = 0; 366 | Serial.println(""); 367 | if (scanResults == 0) { 368 | Serial.println("No WiFi devices in AP Mode found"); 369 | } else { 370 | Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices "); 371 | for (int i = 0; i < scanResults; ++i) { 372 | // Print SSID and RSSI for each device found 373 | String SSID = WiFi.SSID(i); 374 | int32_t RSSI = WiFi.RSSI(i); 375 | String BSSIDstr = WiFi.BSSIDstr(i); 376 | 377 | if (PRINTSCANRESULTS) { 378 | Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); 379 | } 380 | delay(10); 381 | // Check if the current device starts with `Slave` 382 | if (SSID.indexOf("Slave") == 0) { 383 | // SSID of interest 384 | Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); 385 | // Get BSSID => Mac Address of the Slave 386 | int mac[6]; 387 | 388 | if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x%c", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) { 389 | for (int ii = 0; ii < 6; ++ii ) { 390 | slaves[SlaveCnt].peer_addr[ii] = (uint8_t) mac[ii]; 391 | } 392 | } 393 | slaves[SlaveCnt].channel = CHANNEL; // pick a channel 394 | slaves[SlaveCnt].encrypt = 0; // no encryption 395 | SlaveCnt++; 396 | } 397 | } 398 | } 399 | 400 | if (SlaveCnt > 0) { 401 | Serial.print(SlaveCnt); Serial.println(" Slave(s) found, processing.."); 402 | } else { 403 | Serial.println("No Slave Found, trying again."); 404 | } 405 | 406 | // clean up ram 407 | WiFi.scanDelete(); 408 | } 409 | 410 | // Check if the slave is already paired with the master. 411 | // If not, pair the slave with master 412 | void Espnow::manageSlave(void) { 413 | if (SlaveCnt > 0) { 414 | for (int i = 0; i < SlaveCnt; i++) { 415 | const esp_now_peer_info_t *peer = &slaves[i]; 416 | const uint8_t *peer_addr = slaves[i].peer_addr; 417 | Serial.print("Processing: "); 418 | for (int ii = 0; ii < 6; ++ii ) { 419 | Serial.print((uint8_t) slaves[i].peer_addr[ii], HEX); 420 | if (ii != 5) Serial.print(":"); 421 | } 422 | Serial.print(" Status: "); 423 | // check if the peer exists 424 | bool exists = esp_now_is_peer_exist(peer_addr); 425 | if (exists) { 426 | // Slave already paired. 427 | Serial.println("Already Paired"); 428 | } else { 429 | // Slave not paired, attempt pair 430 | esp_err_t addStatus = esp_now_add_peer(peer); 431 | if (addStatus == ESP_OK) { 432 | // Pair success 433 | Serial.println("Pair success"); 434 | } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) { 435 | // How did we get so far!! 436 | Serial.println("ESPNOW Not Init"); 437 | } else if (addStatus == ESP_ERR_ESPNOW_ARG) { 438 | Serial.println("Add Peer - Invalid Argument"); 439 | } else if (addStatus == ESP_ERR_ESPNOW_FULL) { 440 | Serial.println("Peer list full"); 441 | } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) { 442 | Serial.println("Out of memory"); 443 | } else if (addStatus == ESP_ERR_ESPNOW_EXIST) { 444 | Serial.println("Peer Exists"); 445 | } else { 446 | Serial.println("Not sure what happened"); 447 | } 448 | delay(100); 449 | } 450 | } 451 | } else { 452 | // No slave found to process 453 | Serial.println("No Slave found to process"); 454 | } 455 | } 456 | 457 | 458 | // send data 459 | void Espnow::sendData(void) { 460 | //const uint8_t *phy_addr = peer_addr; 461 | for (int i = 0; i < SlaveCnt; i++) { 462 | const uint8_t *scan_addr = slaves[i].peer_addr; 463 | if (i == 0) { // print only for first slave 464 | Serial.print("Sending: "); 465 | //Serial.println(phy_addr); 466 | //Serial.print(phy_addr); 467 | for (int ii = 0; ii < 6; ++ii ) { 468 | Serial.print((uint8_t) sta_addr[ii], HEX); 469 | if (ii != 5) Serial.print(":"); 470 | } 471 | } 472 | //WiFi.softAPmacAddress() 473 | //esp_err_t result = esp_now_send(scan_addr, WiFi.softAPmacAddress(), sizeof(WiFi.softAPmacAddress())); 474 | esp_err_t result = esp_now_send(scan_addr, sta_addr, sizeof(sta_addr)); 475 | Serial.print("Send Status: "); 476 | if (result == ESP_OK) { 477 | Serial.println("Success"); 478 | } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 479 | // How did we get so far!! 480 | Serial.println("ESPNOW not Init."); 481 | } else if (result == ESP_ERR_ESPNOW_ARG) { 482 | Serial.println("Invalid Argument"); 483 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 484 | Serial.println("Internal Error"); 485 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 486 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 487 | } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 488 | Serial.println("Peer not found."); 489 | } else { 490 | Serial.println("Not sure what happened"); 491 | } 492 | delay(100); 493 | } 494 | } 495 | 496 | 497 | void Espnow::BotConnectUpdate(void) 498 | { 499 | static int updatetime; 500 | if (digitalRead(38) == LOW) 501 | { 502 | updatetime++; 503 | } 504 | //Serial.println(updatetime); 505 | if (updatetime >= 100){ 506 | updatetime = 0; 507 | delay(200); 508 | M5.Speaker.tone(80, 200); 509 | delay(200); 510 | M5.Lcd.fillScreen(TFT_BLACK); 511 | M5.Speaker.mute(); 512 | int ypos = 0; 513 | connect_num = 0; 514 | connectflag = false; 515 | while (true){ 516 | M5.Lcd.setCursor(0, 0, 4); 517 | M5.Lcd.println("Broadcasting..."); 518 | M5.Lcd.printf("("); 519 | M5.Lcd.print(WiFi.softAPmacAddress()); 520 | M5.Lcd.printf(")"); 521 | if(connect_num>=1) connect_num = 1; 522 | for (int i = 0; i < connect_num; i++){ 523 | M5.Lcd.setCursor(30, 75 + 25 * i); // Set cursor near top left corner of screen 524 | M5.Lcd.printf("%02x:%02x:%02x:%02x:%02x:%02x", connect_addr[i][0], connect_addr[i][1], connect_addr[i][2], connect_addr[i][3], connect_addr[i][4], connect_addr[i][5]); 525 | } 526 | if (connect_num) { 527 | M5.Lcd.setCursor(30, 125); 528 | M5.Lcd.println("Accept?"); 529 | M5.Lcd.setCursor(220, 210); 530 | M5.Lcd.println("Confirm"); 531 | } 532 | if ((digitalRead(37) == LOW) && (connect_num)){ 533 | M5.Lcd.fillScreen(TFT_BLACK); 534 | for (int i = 0; i < 6; ++i ) 535 | { 536 | peer_addr[i] = (uint8_t) connect_addr[ypos][i]; 537 | } 538 | for (int i = 0; i < 6; ++i ) 539 | { 540 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 541 | } 542 | slave.channel = CHANNEL; 543 | esp_now_add_peer(&slave); 544 | uint8_t ack[4] = {0x41, 0x43, 0x4B}; 545 | for (int i = 0; i < 6; i++) 546 | { 547 | esp_err_t result = esp_now_send(slave.peer_addr, ack, sizeof(ack)); 548 | /* 549 | Serial.print("Send Status: "); 550 | if (result == ESP_OK) { 551 | Serial.println("Success"); 552 | } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 553 | // How did we get so far!! 554 | Serial.println("ESPNOW not Init."); 555 | } else if (result == ESP_ERR_ESPNOW_ARG) { 556 | Serial.println("Invalid Argument"); 557 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 558 | Serial.println("Internal Error"); 559 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 560 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 561 | } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 562 | Serial.println("Peer not found."); 563 | } else { 564 | Serial.println("Not sure what happened"); 565 | }*/ 566 | } 567 | char macStr[18]; 568 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", peer_addr[0], peer_addr[1], peer_addr[2], peer_addr[3], peer_addr[4], peer_addr[5]); 569 | String mac_addr = (char *)macStr; 570 | preferences.putString("mac_addr", mac_addr); 571 | delay(100); 572 | //preferences.end(); 573 | break; 574 | } 575 | if (digitalRead(39) == LOW && (connect_num)) 576 | { 577 | M5.Lcd.fillScreen(TFT_BLACK); 578 | ypos--; 579 | if (ypos <= 0) 580 | { 581 | ypos = 0; 582 | } 583 | } 584 | 585 | if (!connect_num) 586 | { 587 | ScanForSlave(); 588 | if (SlaveCnt > 0) 589 | { 590 | manageSlave(); 591 | sendData(); 592 | } 593 | } 594 | } 595 | } 596 | } 597 | -------------------------------------------------------------------------------- /esp_now/espnow.cpp: -------------------------------------------------------------------------------- 1 | //#include"espnow.h 2 | #include"espnow.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Espnow::Espnow(void) 9 | { 10 | 11 | } 12 | 13 | void Espnow::BotInit(void){ 14 | 15 | //m5.begin(); 16 | //Serial.begin(115200); 17 | 18 | pinMode(37, INPUT); 19 | pinMode(38, INPUT); 20 | pinMode(39, INPUT); 21 | //digitalWrite(37, HIGH); 22 | // digitalWrite(38, HIGH); 23 | //digitalWrite(39, HIGH); 24 | 25 | WiFi.mode(WIFI_STA); 26 | Serial.println("ESPNow Master Example"); 27 | // This is the mac address of the Master in Station Mode 28 | Serial.print("STA MAC: "); 29 | Serial.println(WiFi.macAddress()); 30 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 31 | //WiFi.softAPmacAddress() 32 | //if(!bot) 33 | sscanf(WiFi.macAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 34 | //else 35 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 36 | //WiFi.softAPmacAddress() 37 | //WiFi.softAPmacAddress() 38 | InitEspNow(); 39 | 40 | preferences.begin("my-app", false); 41 | String mac_addr; 42 | mac_addr = preferences.getString("mac_addr", "error"); 43 | Serial.print("mac_addr = ");Serial.print(mac_addr); 44 | 45 | sscanf(mac_addr.c_str(), "%x:%x:%x:%x:%x:%x%c", &peer_addr[0], &peer_addr[1], &peer_addr[2], &peer_addr[3], &peer_addr[4], &peer_addr[5]); 46 | 47 | for (int i = 0; i < 6; ++i ){ 48 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 49 | } 50 | slave.channel = CHANNEL; 51 | esp_now_add_peer(&slave); 52 | } 53 | 54 | void Espnow::RemoteInit(void) 55 | { 56 | //m5.begin(); 57 | //Serial.begin(115200); 58 | 59 | pinMode(37, INPUT); 60 | pinMode(38, INPUT); 61 | pinMode(39, INPUT); 62 | //digitalWrite(37, HIGH); 63 | // digitalWrite(38, HIGH); 64 | //digitalWrite(39, HIGH); 65 | 66 | WiFi.mode(WIFI_AP_STA); 67 | //WiFi.mode(WIFI_AP); 68 | ConfigDeviceAP(); 69 | Serial.println("ESPNow Master Example"); 70 | // This is the mac address of the Master in Station Mode 71 | Serial.print(" AP MAC: "); 72 | Serial.println(WiFi.softAPmacAddress()); 73 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 74 | //WiFi.softAPmacAddress() 75 | //if(!bot) 76 | sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 77 | //else 78 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 79 | //WiFi.softAPmacAddress() 80 | //WiFi.softAPmacAddress() 81 | InitEspNow(); 82 | 83 | preferences.begin("my-app", false); 84 | String mac_addr; 85 | mac_addr = preferences.getString("mac_addr", "error"); 86 | Serial.print("mac_addr = ");Serial.print(mac_addr); 87 | 88 | sscanf(mac_addr.c_str(), "%x:%x:%x:%x:%x:%x%c", &peer_addr[0], &peer_addr[1], &peer_addr[2], &peer_addr[3], &peer_addr[4], &peer_addr[5]); 89 | 90 | for (int i = 0; i < 6; ++i ){ 91 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 92 | } 93 | slave.channel = CHANNEL; 94 | esp_now_add_peer(&slave); 95 | } 96 | 97 | // config AP SSID 98 | void Espnow::ConfigDeviceAP(void) 99 | { 100 | 101 | String Mac = WiFi.macAddress(); 102 | String SSID = "Slave:"+ Mac; 103 | bool result = WiFi.softAP(SSID.c_str(), "12345678", CHANNEL, 0); 104 | if (!result) 105 | { 106 | Serial.println("AP Config failed."); 107 | } else 108 | { 109 | Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID)); 110 | } 111 | } 112 | 113 | // Init ESP Now with fallback 114 | void Espnow::InitEspNow(void) 115 | { 116 | if (esp_now_init() == ESP_OK) 117 | { 118 | Serial.println("ESPNow Init Success"); 119 | } 120 | else 121 | { 122 | Serial.println("ESPNow Init Failed"); 123 | ESP.restart(); 124 | } 125 | } 126 | 127 | void Espnow::OnRemotSent(const uint8_t *mac_addr, esp_now_send_status_t status) 128 | { 129 | 130 | } 131 | int8_t Espnow::OnRemotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len){ 132 | 133 | if(data_len == 4) 134 | { 135 | connectflag = true; 136 | } 137 | if(data_len == 6) 138 | { 139 | int i = 0; 140 | for(i = 0; i < connect_num; i++) 141 | { 142 | int j = 0; 143 | for(j = 0; j < 6; j++) 144 | { 145 | if(connect_addr[i][j] != data[j]) 146 | { 147 | break; 148 | } 149 | } 150 | //Serial.printf("j = %d",j); 151 | if(j == 6) 152 | { 153 | break; 154 | } 155 | } 156 | 157 | if(i == connect_num) 158 | { 159 | for(int i = 0; i < 6;i++) 160 | { 161 | connect_addr[connect_num][i] = data[i]; 162 | } 163 | connect_num++; 164 | } 165 | } 166 | if(connect_num> 20) connect_num = 0; 167 | 168 | 169 | int mac_comp = 0; 170 | for(mac_comp = 0; mac_comp < 6; mac_comp++){ 171 | if(mac_addr[mac_comp] != peer_addr[mac_comp]){ 172 | return 1; 173 | } 174 | } 175 | return 0; 176 | } 177 | 178 | void Espnow::OnBotSent(const uint8_t *mac_addr, esp_now_send_status_t status) 179 | { 180 | } 181 | int8_t Espnow::OnBotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) 182 | { 183 | Serial.print("connectflag = ");Serial.println(connectflag); 184 | if ((data_len == 7) && (!connectflag)) 185 | { 186 | connectflag = true; 187 | int i = 0; 188 | for (i = 0; i < connect_num; i++) 189 | { 190 | int j = 0; 191 | for (j = 0; j < 6; j++) 192 | { 193 | if (connect_addr[i][j] != mac_addr[j]) 194 | { 195 | break; 196 | } 197 | } 198 | //Serial.printf("j = %d",j); 199 | if (j == 6) 200 | { 201 | break; 202 | } 203 | } 204 | if (i == connect_num) 205 | { 206 | for (int i = 0; i < 6; i++) 207 | { 208 | connect_addr[connect_num][i] = mac_addr[i]; 209 | } 210 | connect_num++; 211 | } 212 | } 213 | //if (connect_num > 20) connect_num = 0; 214 | 215 | int mac_comp = 0; 216 | for(mac_comp = 0; mac_comp < 6; mac_comp++){ 217 | if(mac_addr[mac_comp] != peer_addr[mac_comp]){ 218 | return 1; 219 | } 220 | } 221 | return 0; 222 | } 223 | 224 | void Espnow::RemoteConnectUpdate(){ 225 | 226 | static int updatetime; 227 | if (digitalRead(38) == LOW) 228 | { 229 | updatetime++; 230 | } 231 | //Serial.println(updatetime); 232 | if (updatetime >= 300){ 233 | updatetime = 0; 234 | delay(200); 235 | M5.Speaker.tone(80, 200); 236 | delay(200); 237 | M5.Lcd.fillScreen(TFT_BLACK); 238 | M5.Speaker.mute(); 239 | int ypos = 0; 240 | int old_ypos = 0; 241 | connect_num = 0; 242 | connectflag = false; 243 | while(true) 244 | { 245 | M5.Lcd.setCursor(0, 0, 4); 246 | M5.Lcd.println("LidarBot Scanning...."); 247 | for(int i = 0; i < connect_num; i++) 248 | { 249 | M5.Lcd.setCursor(30, 25 + 25 * i); // Set cursor near top left corner of screen 250 | M5.Lcd.printf("%02x:%02x:%02x:%02x:%02x:%02x", connect_addr[i][0], connect_addr[i][1], connect_addr[i][2], connect_addr[i][3], connect_addr[i][4], connect_addr[i][5]); 251 | } 252 | if(connect_num){ 253 | M5.Lcd.setCursor(270, 25 + ypos * 25); 254 | M5.Lcd.println("<<"); 255 | } 256 | 257 | if(ypos < 1){ 258 | //M5.Lcd.fillScreen(TFT_BLACK); 259 | if(old_ypos != ypos) 260 | M5.Lcd.fillRect(30, 210, 90, 50, TFT_BLACK); 261 | M5.Lcd.setCursor(30, 210); 262 | M5.Lcd.println("update"); 263 | } 264 | else 265 | { 266 | if(old_ypos != ypos) 267 | M5.Lcd.fillRect(30, 210, 100, 50, TFT_BLACK); 268 | //M5.Lcd.fillScreen(TFT_BLACK); 269 | M5.Lcd.setCursor(50, 210); 270 | M5.Lcd.println("up"); 271 | } 272 | old_ypos = ypos; 273 | M5.Lcd.setCursor(125, 210); 274 | M5.Lcd.println("down"); 275 | M5.Lcd.setCursor(220, 210); 276 | M5.Lcd.println("select"); 277 | 278 | 279 | //select down 280 | if ((digitalRead(37) == LOW) && (connect_num)) 281 | { 282 | M5.Lcd.fillScreen(TFT_BLACK); 283 | for (int i = 0; i < 6; ++i ){ 284 | peer_addr[i] = (uint8_t) connect_addr[ypos][i]; 285 | } 286 | //Serial.printf("%2x:%2x:%2x:%2x:%2x:%2x",slave.peer_addr[0],slave.peer_addr[1],slave.peer_addr[2], slave.peer_addr[3],slave.peer_addr[4], slave.peer_addr[5]); 287 | for (int i = 0; i < 6; ++i ){ 288 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 289 | } 290 | slave.channel = CHANNEL; 291 | esp_now_add_peer(&slave); 292 | M5.Lcd.setCursor(140, 210); 293 | M5.Lcd.println("Quit"); 294 | while(!connectflag){ 295 | //for(int i = 0;i < 6; i++){ 296 | esp_err_t result = esp_now_send(slave.peer_addr, sta_addr, sizeof(sta_addr)+ 1); 297 | Serial.print("Send Status: "); 298 | if (result == ESP_OK) { 299 | Serial.println("Success"); 300 | }else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 301 | Serial.println("ESPNOW not Init."); 302 | } else if (result == ESP_ERR_ESPNOW_ARG) { 303 | Serial.println("Invalid Argument"); 304 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 305 | Serial.println("Internal Error"); 306 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 307 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 308 | }else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 309 | Serial.println("Peer not found."); 310 | }else { 311 | Serial.println("Not sure what happened"); 312 | } 313 | delay(100); 314 | // } 315 | 316 | 317 | 318 | // while(!connectflag) 319 | // { 320 | if(digitalRead(38) == LOW) break; 321 | } 322 | if(connectflag){ 323 | char macStr[18]; 324 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", peer_addr[0], peer_addr[1], peer_addr[2], peer_addr[3], peer_addr[4], peer_addr[5]); 325 | String mac_addr = (char *)macStr; 326 | preferences.putString("mac_addr", mac_addr); 327 | delay(100); 328 | M5.Lcd.fillScreen(TFT_BLACK); 329 | break; 330 | } 331 | } 332 | 333 | 334 | delay(100); 335 | if (digitalRead(39) == LOW && (connect_num) ) 336 | { 337 | M5.Lcd.fillScreen(TFT_BLACK); 338 | ypos--; 339 | if(ypos <= 0) 340 | { 341 | ypos = 0; 342 | } 343 | } 344 | 345 | if (digitalRead(38) == LOW && (connect_num)) 346 | { 347 | // Serial.println("IdigitalRead(38) == LOW"); 348 | //M5.Lcd.print("39 low"); 349 | M5.Lcd.fillScreen(TFT_BLACK); 350 | ypos++; 351 | if(ypos >= connect_num - 1) 352 | { 353 | ypos = connect_num - 1; 354 | } 355 | } 356 | 357 | } 358 | } 359 | } 360 | 361 | 362 | void Espnow::ScanForSlave(void) { 363 | int8_t scanResults = WiFi.scanNetworks(); 364 | //reset slaves 365 | memset(slaves, 0, sizeof(slaves)); 366 | SlaveCnt = 0; 367 | Serial.println(""); 368 | if (scanResults == 0) { 369 | Serial.println("No WiFi devices in AP Mode found"); 370 | } else { 371 | Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices "); 372 | for (int i = 0; i < scanResults; ++i) { 373 | // Print SSID and RSSI for each device found 374 | String SSID = WiFi.SSID(i); 375 | int32_t RSSI = WiFi.RSSI(i); 376 | String BSSIDstr = WiFi.BSSIDstr(i); 377 | 378 | if (PRINTSCANRESULTS) { 379 | Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); 380 | } 381 | delay(10); 382 | // Check if the current device starts with `Slave` 383 | if (SSID.indexOf("Slave") == 0) { 384 | // SSID of interest 385 | Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); 386 | // Get BSSID => Mac Address of the Slave 387 | int mac[6]; 388 | 389 | if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x%c", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) { 390 | for (int ii = 0; ii < 6; ++ii ) { 391 | slaves[SlaveCnt].peer_addr[ii] = (uint8_t) mac[ii]; 392 | } 393 | } 394 | slaves[SlaveCnt].channel = CHANNEL; // pick a channel 395 | slaves[SlaveCnt].encrypt = 0; // no encryption 396 | SlaveCnt++; 397 | } 398 | } 399 | } 400 | 401 | if (SlaveCnt > 0) { 402 | Serial.print(SlaveCnt); Serial.println(" Slave(s) found, processing.."); 403 | } else { 404 | Serial.println("No Slave Found, trying again."); 405 | } 406 | 407 | // clean up ram 408 | WiFi.scanDelete(); 409 | } 410 | 411 | // Check if the slave is already paired with the master. 412 | // If not, pair the slave with master 413 | void Espnow::manageSlave(void) { 414 | if (SlaveCnt > 0) { 415 | for (int i = 0; i < SlaveCnt; i++) { 416 | const esp_now_peer_info_t *peer = &slaves[i]; 417 | const uint8_t *peer_addr = slaves[i].peer_addr; 418 | Serial.print("Processing: "); 419 | for (int ii = 0; ii < 6; ++ii ) { 420 | Serial.print((uint8_t) slaves[i].peer_addr[ii], HEX); 421 | if (ii != 5) Serial.print(":"); 422 | } 423 | Serial.print(" Status: "); 424 | // check if the peer exists 425 | bool exists = esp_now_is_peer_exist(peer_addr); 426 | if (exists) { 427 | // Slave already paired. 428 | Serial.println("Already Paired"); 429 | } else { 430 | // Slave not paired, attempt pair 431 | esp_err_t addStatus = esp_now_add_peer(peer); 432 | if (addStatus == ESP_OK) { 433 | // Pair success 434 | Serial.println("Pair success"); 435 | } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) { 436 | // How did we get so far!! 437 | Serial.println("ESPNOW Not Init"); 438 | } else if (addStatus == ESP_ERR_ESPNOW_ARG) { 439 | Serial.println("Add Peer - Invalid Argument"); 440 | } else if (addStatus == ESP_ERR_ESPNOW_FULL) { 441 | Serial.println("Peer list full"); 442 | } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) { 443 | Serial.println("Out of memory"); 444 | } else if (addStatus == ESP_ERR_ESPNOW_EXIST) { 445 | Serial.println("Peer Exists"); 446 | } else { 447 | Serial.println("Not sure what happened"); 448 | } 449 | delay(100); 450 | } 451 | } 452 | } else { 453 | // No slave found to process 454 | Serial.println("No Slave found to process"); 455 | } 456 | } 457 | 458 | 459 | // send data 460 | void Espnow::sendData(void) { 461 | //const uint8_t *phy_addr = peer_addr; 462 | for (int i = 0; i < SlaveCnt; i++) { 463 | const uint8_t *scan_addr = slaves[i].peer_addr; 464 | if (i == 0) { // print only for first slave 465 | Serial.print("Sending: "); 466 | //Serial.println(phy_addr); 467 | //Serial.print(phy_addr); 468 | for (int ii = 0; ii < 6; ++ii ) { 469 | Serial.print((uint8_t) sta_addr[ii], HEX); 470 | if (ii != 5) Serial.print(":"); 471 | } 472 | } 473 | //WiFi.softAPmacAddress() 474 | //esp_err_t result = esp_now_send(scan_addr, WiFi.softAPmacAddress(), sizeof(WiFi.softAPmacAddress())); 475 | esp_err_t result = esp_now_send(scan_addr, sta_addr, sizeof(sta_addr)); 476 | Serial.print("Send Status: "); 477 | if (result == ESP_OK) { 478 | Serial.println("Success"); 479 | } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 480 | // How did we get so far!! 481 | Serial.println("ESPNOW not Init."); 482 | } else if (result == ESP_ERR_ESPNOW_ARG) { 483 | Serial.println("Invalid Argument"); 484 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 485 | Serial.println("Internal Error"); 486 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 487 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 488 | } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 489 | Serial.println("Peer not found."); 490 | } else { 491 | Serial.println("Not sure what happened"); 492 | } 493 | delay(100); 494 | } 495 | } 496 | 497 | 498 | void Espnow::BotConnectUpdate(void) 499 | { 500 | static int updatetime; 501 | if (digitalRead(38) == LOW) 502 | { 503 | updatetime++; 504 | } 505 | //Serial.println(updatetime); 506 | if (updatetime >= 100){ 507 | updatetime = 0; 508 | delay(200); 509 | M5.Speaker.tone(80, 200); 510 | delay(200); 511 | M5.Lcd.fillScreen(TFT_BLACK); 512 | M5.Speaker.mute(); 513 | int ypos = 0; 514 | connect_num = 0; 515 | connectflag = false; 516 | while (true){ 517 | M5.Lcd.setCursor(0, 0, 4); 518 | M5.Lcd.println("Broadcasting..."); 519 | M5.Lcd.printf("("); 520 | M5.Lcd.print(WiFi.softAPmacAddress()); 521 | M5.Lcd.printf(")"); 522 | if(connect_num>=1) connect_num = 1; 523 | for (int i = 0; i < connect_num; i++){ 524 | M5.Lcd.setCursor(30, 75 + 25 * i); // Set cursor near top left corner of screen 525 | M5.Lcd.printf("%02x:%02x:%02x:%02x:%02x:%02x", connect_addr[i][0], connect_addr[i][1], connect_addr[i][2], connect_addr[i][3], connect_addr[i][4], connect_addr[i][5]); 526 | } 527 | if (connect_num) { 528 | M5.Lcd.setCursor(30, 125); 529 | M5.Lcd.println("Accept?"); 530 | M5.Lcd.setCursor(220, 210); 531 | M5.Lcd.println("Confirm"); 532 | } 533 | if ((digitalRead(37) == LOW) && (connect_num)){ 534 | M5.Lcd.fillScreen(TFT_BLACK); 535 | for (int i = 0; i < 6; ++i ) 536 | { 537 | peer_addr[i] = (uint8_t) connect_addr[ypos][i]; 538 | } 539 | for (int i = 0; i < 6; ++i ) 540 | { 541 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 542 | } 543 | slave.channel = CHANNEL; 544 | esp_now_add_peer(&slave); 545 | uint8_t ack[4] = {0x41, 0x43, 0x4B}; 546 | for (int i = 0; i < 6; i++) 547 | { 548 | esp_err_t result = esp_now_send(slave.peer_addr, ack, sizeof(ack)); 549 | /* 550 | Serial.print("Send Status: "); 551 | if (result == ESP_OK) { 552 | Serial.println("Success"); 553 | } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 554 | // How did we get so far!! 555 | Serial.println("ESPNOW not Init."); 556 | } else if (result == ESP_ERR_ESPNOW_ARG) { 557 | Serial.println("Invalid Argument"); 558 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 559 | Serial.println("Internal Error"); 560 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 561 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 562 | } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 563 | Serial.println("Peer not found."); 564 | } else { 565 | Serial.println("Not sure what happened"); 566 | }*/ 567 | } 568 | char macStr[18]; 569 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", peer_addr[0], peer_addr[1], peer_addr[2], peer_addr[3], peer_addr[4], peer_addr[5]); 570 | String mac_addr = (char *)macStr; 571 | preferences.putString("mac_addr", mac_addr); 572 | delay(100); 573 | //preferences.end(); 574 | break; 575 | } 576 | if (digitalRead(39) == LOW && (connect_num)) 577 | { 578 | M5.Lcd.fillScreen(TFT_BLACK); 579 | ypos--; 580 | if (ypos <= 0) 581 | { 582 | ypos = 0; 583 | } 584 | } 585 | 586 | if (!connect_num) 587 | { 588 | ScanForSlave(); 589 | if (SlaveCnt > 0) 590 | { 591 | manageSlave(); 592 | sendData(); 593 | } 594 | } 595 | } 596 | } 597 | } 598 | -------------------------------------------------------------------------------- /esp_now/examples/Remoter/espnow.cpp: -------------------------------------------------------------------------------- 1 | //#include"espnow.h 2 | #include"espnow.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Espnow::Espnow(void) 9 | { 10 | 11 | } 12 | 13 | void Espnow::BotInit(void){ 14 | 15 | //m5.begin(); 16 | //Serial.begin(115200); 17 | 18 | pinMode(37, INPUT); 19 | pinMode(38, INPUT); 20 | pinMode(39, INPUT); 21 | //digitalWrite(37, HIGH); 22 | // digitalWrite(38, HIGH); 23 | //digitalWrite(39, HIGH); 24 | 25 | WiFi.mode(WIFI_STA); 26 | Serial.println("ESPNow Master Example"); 27 | // This is the mac address of the Master in Station Mode 28 | Serial.print("STA MAC: "); 29 | Serial.println(WiFi.macAddress()); 30 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 31 | //WiFi.softAPmacAddress() 32 | //if(!bot) 33 | sscanf(WiFi.macAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 34 | //else 35 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 36 | //WiFi.softAPmacAddress() 37 | //WiFi.softAPmacAddress() 38 | InitEspNow(); 39 | 40 | preferences.begin("my-app", false); 41 | String mac_addr; 42 | mac_addr = preferences.getString("mac_addr", "error"); 43 | Serial.print("mac_addr = ");Serial.print(mac_addr); 44 | 45 | sscanf(mac_addr.c_str(), "%x:%x:%x:%x:%x:%x%c", &peer_addr[0], &peer_addr[1], &peer_addr[2], &peer_addr[3], &peer_addr[4], &peer_addr[5]); 46 | 47 | for (int i = 0; i < 6; ++i ){ 48 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 49 | } 50 | slave.channel = CHANNEL; 51 | esp_now_add_peer(&slave); 52 | } 53 | 54 | void Espnow::RemoteInit(void) 55 | { 56 | //m5.begin(); 57 | //Serial.begin(115200); 58 | 59 | pinMode(37, INPUT); 60 | pinMode(38, INPUT); 61 | pinMode(39, INPUT); 62 | //digitalWrite(37, HIGH); 63 | // digitalWrite(38, HIGH); 64 | //digitalWrite(39, HIGH); 65 | 66 | WiFi.mode(WIFI_AP_STA); 67 | //WiFi.mode(WIFI_AP); 68 | ConfigDeviceAP(); 69 | Serial.println("ESPNow Master Example"); 70 | // This is the mac address of the Master in Station Mode 71 | Serial.print(" AP MAC: "); 72 | Serial.println(WiFi.softAPmacAddress()); 73 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 74 | //WiFi.softAPmacAddress() 75 | //if(!bot) 76 | sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 77 | //else 78 | //sscanf(WiFi.softAPmacAddress().c_str(), "%x:%x:%x:%x:%x:%x", &sta_addr[0], &sta_addr[1], &sta_addr[2], &sta_addr[3], &sta_addr[4], &sta_addr[5] ); 79 | //WiFi.softAPmacAddress() 80 | //WiFi.softAPmacAddress() 81 | InitEspNow(); 82 | 83 | preferences.begin("my-app", false); 84 | String mac_addr; 85 | mac_addr = preferences.getString("mac_addr", "error"); 86 | Serial.print("mac_addr = ");Serial.print(mac_addr); 87 | 88 | sscanf(mac_addr.c_str(), "%x:%x:%x:%x:%x:%x%c", &peer_addr[0], &peer_addr[1], &peer_addr[2], &peer_addr[3], &peer_addr[4], &peer_addr[5]); 89 | 90 | for (int i = 0; i < 6; ++i ){ 91 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 92 | } 93 | slave.channel = CHANNEL; 94 | esp_now_add_peer(&slave); 95 | } 96 | 97 | // config AP SSID 98 | void Espnow::ConfigDeviceAP(void) 99 | { 100 | 101 | String Mac = WiFi.macAddress(); 102 | String SSID = "Slave:"+ Mac; 103 | bool result = WiFi.softAP(SSID.c_str(), "12345678", CHANNEL, 0); 104 | if (!result) 105 | { 106 | Serial.println("AP Config failed."); 107 | } else 108 | { 109 | Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID)); 110 | } 111 | } 112 | 113 | // Init ESP Now with fallback 114 | void Espnow::InitEspNow(void) 115 | { 116 | if (esp_now_init() == ESP_OK) 117 | { 118 | Serial.println("ESPNow Init Success"); 119 | } 120 | else 121 | { 122 | Serial.println("ESPNow Init Failed"); 123 | ESP.restart(); 124 | } 125 | } 126 | 127 | void Espnow::OnRemotSent(const uint8_t *mac_addr, esp_now_send_status_t status) 128 | { 129 | 130 | } 131 | int8_t Espnow::OnRemotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len){ 132 | 133 | if(data_len == 4) 134 | { 135 | connectflag = true; 136 | } 137 | if(data_len == 6) 138 | { 139 | int i = 0; 140 | for(i = 0; i < connect_num; i++) 141 | { 142 | int j = 0; 143 | for(j = 0; j < 6; j++) 144 | { 145 | if(connect_addr[i][j] != data[j]) 146 | { 147 | break; 148 | } 149 | } 150 | //Serial.printf("j = %d",j); 151 | if(j == 6) 152 | { 153 | break; 154 | } 155 | } 156 | 157 | if(i == connect_num) 158 | { 159 | for(int i = 0; i < 6;i++) 160 | { 161 | connect_addr[connect_num][i] = data[i]; 162 | } 163 | connect_num++; 164 | } 165 | } 166 | if(connect_num> 20) connect_num = 0; 167 | 168 | 169 | int mac_comp = 0; 170 | for(mac_comp = 0; mac_comp < 6; mac_comp++){ 171 | if(mac_addr[mac_comp] != peer_addr[mac_comp]){ 172 | return 1; 173 | } 174 | } 175 | return 0; 176 | } 177 | 178 | void Espnow::OnBotSent(const uint8_t *mac_addr, esp_now_send_status_t status) 179 | { 180 | } 181 | int8_t Espnow::OnBotRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) 182 | { 183 | Serial.print("connectflag = ");Serial.println(connectflag); 184 | if ((data_len == 7) && (!connectflag)) 185 | { 186 | connectflag = true; 187 | int i = 0; 188 | for (i = 0; i < connect_num; i++) 189 | { 190 | int j = 0; 191 | for (j = 0; j < 6; j++) 192 | { 193 | if (connect_addr[i][j] != mac_addr[j]) 194 | { 195 | break; 196 | } 197 | } 198 | //Serial.printf("j = %d",j); 199 | if (j == 6) 200 | { 201 | break; 202 | } 203 | } 204 | if (i == connect_num) 205 | { 206 | for (int i = 0; i < 6; i++) 207 | { 208 | connect_addr[connect_num][i] = mac_addr[i]; 209 | } 210 | connect_num++; 211 | } 212 | } 213 | //if (connect_num > 20) connect_num = 0; 214 | 215 | int mac_comp = 0; 216 | for(mac_comp = 0; mac_comp < 6; mac_comp++){ 217 | if(mac_addr[mac_comp] != peer_addr[mac_comp]){ 218 | return 1; 219 | } 220 | } 221 | return 0; 222 | } 223 | 224 | void Espnow::RemoteConnectUpdate(){ 225 | 226 | static int updatetime; 227 | if (digitalRead(38) == LOW) 228 | { 229 | updatetime++; 230 | } 231 | //Serial.println(updatetime); 232 | if (updatetime >= 300){ 233 | updatetime = 0; 234 | delay(200); 235 | M5.Speaker.tone(80, 200); 236 | delay(200); 237 | M5.Lcd.fillScreen(TFT_BLACK); 238 | M5.Speaker.mute(); 239 | int ypos = 0; 240 | int old_ypos = 0; 241 | connect_num = 0; 242 | connectflag = false; 243 | while(true) 244 | { 245 | M5.Lcd.setCursor(0, 0, 4); 246 | M5.Lcd.println("LidarBot Scanning...."); 247 | for(int i = 0; i < connect_num; i++) 248 | { 249 | M5.Lcd.setCursor(30, 25 + 25 * i); // Set cursor near top left corner of screen 250 | M5.Lcd.printf("%02x:%02x:%02x:%02x:%02x:%02x", connect_addr[i][0], connect_addr[i][1], connect_addr[i][2], connect_addr[i][3], connect_addr[i][4], connect_addr[i][5]); 251 | } 252 | if(connect_num){ 253 | M5.Lcd.setCursor(270, 25 + ypos * 25); 254 | M5.Lcd.println("<<"); 255 | } 256 | 257 | if(ypos < 1){ 258 | //M5.Lcd.fillScreen(TFT_BLACK); 259 | if(old_ypos != ypos) 260 | M5.Lcd.fillRect(30, 210, 90, 50, TFT_BLACK); 261 | M5.Lcd.setCursor(30, 210); 262 | M5.Lcd.println("update"); 263 | } 264 | else 265 | { 266 | if(old_ypos != ypos) 267 | M5.Lcd.fillRect(30, 210, 100, 50, TFT_BLACK); 268 | //M5.Lcd.fillScreen(TFT_BLACK); 269 | M5.Lcd.setCursor(50, 210); 270 | M5.Lcd.println("up"); 271 | } 272 | old_ypos = ypos; 273 | M5.Lcd.setCursor(125, 210); 274 | M5.Lcd.println("down"); 275 | M5.Lcd.setCursor(220, 210); 276 | M5.Lcd.println("select"); 277 | 278 | 279 | //select down 280 | if ((digitalRead(37) == LOW) && (connect_num)) 281 | { 282 | M5.Lcd.fillScreen(TFT_BLACK); 283 | for (int i = 0; i < 6; ++i ){ 284 | peer_addr[i] = (uint8_t) connect_addr[ypos][i]; 285 | } 286 | //Serial.printf("%2x:%2x:%2x:%2x:%2x:%2x",slave.peer_addr[0],slave.peer_addr[1],slave.peer_addr[2], slave.peer_addr[3],slave.peer_addr[4], slave.peer_addr[5]); 287 | for (int i = 0; i < 6; ++i ){ 288 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 289 | } 290 | slave.channel = CHANNEL; 291 | esp_now_add_peer(&slave); 292 | M5.Lcd.setCursor(140, 210); 293 | M5.Lcd.println("Quit"); 294 | while(!connectflag){ 295 | //for(int i = 0;i < 6; i++){ 296 | esp_err_t result = esp_now_send(slave.peer_addr, sta_addr, sizeof(sta_addr)+ 1); 297 | Serial.print("Send Status: "); 298 | if (result == ESP_OK) { 299 | Serial.println("Success"); 300 | }else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 301 | Serial.println("ESPNOW not Init."); 302 | } else if (result == ESP_ERR_ESPNOW_ARG) { 303 | Serial.println("Invalid Argument"); 304 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 305 | Serial.println("Internal Error"); 306 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 307 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 308 | }else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 309 | Serial.println("Peer not found."); 310 | }else { 311 | Serial.println("Not sure what happened"); 312 | } 313 | delay(100); 314 | // } 315 | 316 | 317 | 318 | // while(!connectflag) 319 | // { 320 | if(digitalRead(38) == LOW) break; 321 | } 322 | if(connectflag){ 323 | char macStr[18]; 324 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", peer_addr[0], peer_addr[1], peer_addr[2], peer_addr[3], peer_addr[4], peer_addr[5]); 325 | String mac_addr = (char *)macStr; 326 | preferences.putString("mac_addr", mac_addr); 327 | delay(100); 328 | M5.Lcd.fillScreen(TFT_BLACK); 329 | break; 330 | } 331 | } 332 | 333 | 334 | delay(100); 335 | if (digitalRead(39) == LOW && (connect_num) ) 336 | { 337 | M5.Lcd.fillScreen(TFT_BLACK); 338 | ypos--; 339 | if(ypos <= 0) 340 | { 341 | ypos = 0; 342 | } 343 | } 344 | 345 | if (digitalRead(38) == LOW && (connect_num)) 346 | { 347 | // Serial.println("IdigitalRead(38) == LOW"); 348 | //M5.Lcd.print("39 low"); 349 | M5.Lcd.fillScreen(TFT_BLACK); 350 | ypos++; 351 | if(ypos >= connect_num - 1) 352 | { 353 | ypos = connect_num - 1; 354 | } 355 | } 356 | 357 | } 358 | } 359 | } 360 | 361 | 362 | void Espnow::ScanForSlave(void) { 363 | int8_t scanResults = WiFi.scanNetworks(); 364 | //reset slaves 365 | memset(slaves, 0, sizeof(slaves)); 366 | SlaveCnt = 0; 367 | Serial.println(""); 368 | if (scanResults == 0) { 369 | Serial.println("No WiFi devices in AP Mode found"); 370 | } else { 371 | Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices "); 372 | for (int i = 0; i < scanResults; ++i) { 373 | // Print SSID and RSSI for each device found 374 | String SSID = WiFi.SSID(i); 375 | int32_t RSSI = WiFi.RSSI(i); 376 | String BSSIDstr = WiFi.BSSIDstr(i); 377 | 378 | if (PRINTSCANRESULTS) { 379 | Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); 380 | } 381 | delay(10); 382 | // Check if the current device starts with `Slave` 383 | if (SSID.indexOf("Slave") == 0) { 384 | // SSID of interest 385 | Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); 386 | // Get BSSID => Mac Address of the Slave 387 | int mac[6]; 388 | 389 | if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x%c", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) { 390 | for (int ii = 0; ii < 6; ++ii ) { 391 | slaves[SlaveCnt].peer_addr[ii] = (uint8_t) mac[ii]; 392 | } 393 | } 394 | slaves[SlaveCnt].channel = CHANNEL; // pick a channel 395 | slaves[SlaveCnt].encrypt = 0; // no encryption 396 | SlaveCnt++; 397 | } 398 | } 399 | } 400 | 401 | if (SlaveCnt > 0) { 402 | Serial.print(SlaveCnt); Serial.println(" Slave(s) found, processing.."); 403 | } else { 404 | Serial.println("No Slave Found, trying again."); 405 | } 406 | 407 | // clean up ram 408 | WiFi.scanDelete(); 409 | } 410 | 411 | // Check if the slave is already paired with the master. 412 | // If not, pair the slave with master 413 | void Espnow::manageSlave(void) { 414 | if (SlaveCnt > 0) { 415 | for (int i = 0; i < SlaveCnt; i++) { 416 | const esp_now_peer_info_t *peer = &slaves[i]; 417 | const uint8_t *peer_addr = slaves[i].peer_addr; 418 | Serial.print("Processing: "); 419 | for (int ii = 0; ii < 6; ++ii ) { 420 | Serial.print((uint8_t) slaves[i].peer_addr[ii], HEX); 421 | if (ii != 5) Serial.print(":"); 422 | } 423 | Serial.print(" Status: "); 424 | // check if the peer exists 425 | bool exists = esp_now_is_peer_exist(peer_addr); 426 | if (exists) { 427 | // Slave already paired. 428 | Serial.println("Already Paired"); 429 | } else { 430 | // Slave not paired, attempt pair 431 | esp_err_t addStatus = esp_now_add_peer(peer); 432 | if (addStatus == ESP_OK) { 433 | // Pair success 434 | Serial.println("Pair success"); 435 | } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) { 436 | // How did we get so far!! 437 | Serial.println("ESPNOW Not Init"); 438 | } else if (addStatus == ESP_ERR_ESPNOW_ARG) { 439 | Serial.println("Add Peer - Invalid Argument"); 440 | } else if (addStatus == ESP_ERR_ESPNOW_FULL) { 441 | Serial.println("Peer list full"); 442 | } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) { 443 | Serial.println("Out of memory"); 444 | } else if (addStatus == ESP_ERR_ESPNOW_EXIST) { 445 | Serial.println("Peer Exists"); 446 | } else { 447 | Serial.println("Not sure what happened"); 448 | } 449 | delay(100); 450 | } 451 | } 452 | } else { 453 | // No slave found to process 454 | Serial.println("No Slave found to process"); 455 | } 456 | } 457 | 458 | 459 | // send data 460 | void Espnow::sendData(void) { 461 | //const uint8_t *phy_addr = peer_addr; 462 | for (int i = 0; i < SlaveCnt; i++) { 463 | const uint8_t *scan_addr = slaves[i].peer_addr; 464 | if (i == 0) { // print only for first slave 465 | Serial.print("Sending: "); 466 | //Serial.println(phy_addr); 467 | //Serial.print(phy_addr); 468 | for (int ii = 0; ii < 6; ++ii ) { 469 | Serial.print((uint8_t) sta_addr[ii], HEX); 470 | if (ii != 5) Serial.print(":"); 471 | } 472 | } 473 | //WiFi.softAPmacAddress() 474 | //esp_err_t result = esp_now_send(scan_addr, WiFi.softAPmacAddress(), sizeof(WiFi.softAPmacAddress())); 475 | esp_err_t result = esp_now_send(scan_addr, sta_addr, sizeof(sta_addr)); 476 | Serial.print("Send Status: "); 477 | if (result == ESP_OK) { 478 | Serial.println("Success"); 479 | } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 480 | // How did we get so far!! 481 | Serial.println("ESPNOW not Init."); 482 | } else if (result == ESP_ERR_ESPNOW_ARG) { 483 | Serial.println("Invalid Argument"); 484 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 485 | Serial.println("Internal Error"); 486 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 487 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 488 | } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 489 | Serial.println("Peer not found."); 490 | } else { 491 | Serial.println("Not sure what happened"); 492 | } 493 | delay(100); 494 | } 495 | } 496 | 497 | 498 | void Espnow::BotConnectUpdate(void) 499 | { 500 | static int updatetime; 501 | if (digitalRead(38) == LOW) 502 | { 503 | updatetime++; 504 | } 505 | //Serial.println(updatetime); 506 | if (updatetime >= 100){ 507 | updatetime = 0; 508 | delay(200); 509 | M5.Speaker.tone(80, 200); 510 | delay(200); 511 | M5.Lcd.fillScreen(TFT_BLACK); 512 | M5.Speaker.mute(); 513 | int ypos = 0; 514 | connect_num = 0; 515 | connectflag = false; 516 | while (true){ 517 | M5.Lcd.setCursor(0, 0, 4); 518 | M5.Lcd.println("Broadcasting..."); 519 | M5.Lcd.printf("("); 520 | M5.Lcd.print(WiFi.softAPmacAddress()); 521 | M5.Lcd.printf(")"); 522 | if(connect_num>=1) connect_num = 1; 523 | for (int i = 0; i < connect_num; i++){ 524 | M5.Lcd.setCursor(30, 75 + 25 * i); // Set cursor near top left corner of screen 525 | M5.Lcd.printf("%02x:%02x:%02x:%02x:%02x:%02x", connect_addr[i][0], connect_addr[i][1], connect_addr[i][2], connect_addr[i][3], connect_addr[i][4], connect_addr[i][5]); 526 | } 527 | if (connect_num) { 528 | M5.Lcd.setCursor(30, 125); 529 | M5.Lcd.println("Accept?"); 530 | M5.Lcd.setCursor(220, 210); 531 | M5.Lcd.println("Confirm"); 532 | } 533 | if ((digitalRead(37) == LOW) && (connect_num)){ 534 | M5.Lcd.fillScreen(TFT_BLACK); 535 | for (int i = 0; i < 6; ++i ) 536 | { 537 | peer_addr[i] = (uint8_t) connect_addr[ypos][i]; 538 | } 539 | for (int i = 0; i < 6; ++i ) 540 | { 541 | slave.peer_addr[i] = (uint8_t) peer_addr[i]; 542 | } 543 | slave.channel = CHANNEL; 544 | esp_now_add_peer(&slave); 545 | uint8_t ack[4] = {0x41, 0x43, 0x4B}; 546 | for (int i = 0; i < 6; i++) 547 | { 548 | esp_err_t result = esp_now_send(slave.peer_addr, ack, sizeof(ack)); 549 | /* 550 | Serial.print("Send Status: "); 551 | if (result == ESP_OK) { 552 | Serial.println("Success"); 553 | } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 554 | // How did we get so far!! 555 | Serial.println("ESPNOW not Init."); 556 | } else if (result == ESP_ERR_ESPNOW_ARG) { 557 | Serial.println("Invalid Argument"); 558 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 559 | Serial.println("Internal Error"); 560 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 561 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 562 | } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 563 | Serial.println("Peer not found."); 564 | } else { 565 | Serial.println("Not sure what happened"); 566 | }*/ 567 | } 568 | char macStr[18]; 569 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", peer_addr[0], peer_addr[1], peer_addr[2], peer_addr[3], peer_addr[4], peer_addr[5]); 570 | String mac_addr = (char *)macStr; 571 | preferences.putString("mac_addr", mac_addr); 572 | delay(100); 573 | //preferences.end(); 574 | break; 575 | } 576 | if (digitalRead(39) == LOW && (connect_num)) 577 | { 578 | M5.Lcd.fillScreen(TFT_BLACK); 579 | ypos--; 580 | if (ypos <= 0) 581 | { 582 | ypos = 0; 583 | } 584 | } 585 | 586 | if (!connect_num) 587 | { 588 | ScanForSlave(); 589 | if (SlaveCnt > 0) 590 | { 591 | manageSlave(); 592 | sendData(); 593 | } 594 | } 595 | } 596 | } 597 | } 598 | --------------------------------------------------------------------------------