├── assets └── esp-now-gw-wifi-router.jpg ├── LICENSE ├── Readme.md └── src ├── receiver.cpp └── sender.cpp /assets/esp-now-gw-wifi-router.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m1cr0lab-esp32/esp-now-network-and-wifi-gateway/HEAD/assets/esp-now-gw-wifi-router.jpg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Stéphane Calderoni 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # ESP-NOW network + WiFi gateway to the Internet 2 | 3 | The objective of this mini-project is to illustrate a communication based on the (connectionless) ESP-NOW protocol between a set of ESP32 senders and an ESP32 receiver, while configuring the receiver as a gateway to the Internet by connecting it to a WiFi router. 4 | 5 | ![Scheme of implementation][picture] 6 | 7 | This mini-project was born from the observation of an erroneous 8 | implementation on the following tutorial, published by Rui and Sara on 9 | Random Nerd Tutorials: 10 | 11 | [ESP32: ESP-NOW Web Server Sensor Dashboard (ESP-NOW + Wi-Fi)][tutorial] 12 | 13 | This observation led me to open a [discussion][discussion] on the RNT Lab forum to get the opinion of interested readers. And I'd especially like to thank Lee and Steve for taking the time to discuss the issue with me. We were finally able to identify what was wrong. 14 | 15 | The solution was initially published on the Espressif forum by **ardcp32**, which I thank in passing, and which [you will find here][solution]. 16 | 17 | This mini-project is therefore intended to illustrate the right way to proceed for all readers of Random Nerd Tutorials. 18 | 19 | You'll find here the code to be implemented on all ESP-NOW sending nodes, as well as that of the ESP-NOW receiving gateway, which is simultaneously connected to the Internet through a WiFi router. 20 | 21 | Note that for this to work, each sender must use the same radio channel as the WiFi router. You can hard-code it, but if your router is configured to automatically switch to the least congested channel, it is best to have a routine that scans available networks and looks for the SSID of your router. This way you can easily identify the channel it is using. So I added this routine in the sender's code. 22 | 23 | [picture]: assets/esp-now-gw-wifi-router.jpg 24 | [tutorial]: https://randomnerdtutorials.com/esp32-esp-now-wi-fi-web-server/ 25 | [discussion]: https://rntlab.com/question/esp-now-gateway-wifi_mode_sta-with-a-wifi-router/ 26 | [solution]: https://www.esp32.com/viewtopic.php?f=19&t=12992#p51338 -------------------------------------------------------------------------------- /src/receiver.cpp: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // ESP-NOW network + WiFi gateway to the Internet 3 | // ---------------------------------------------------------------------------- 4 | // ESP-NOW receiver acting as a WiFi gateway to the Internet 5 | // ---------------------------------------------------------------------------- 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | // ---------------------------------------------------------------------------- 12 | // WiFi handling 13 | // ---------------------------------------------------------------------------- 14 | 15 | constexpr char WIFI_SSID[] = "your wifi SSID"; 16 | constexpr char WIFI_PASS[] = "your wifi password"; 17 | 18 | void initWiFi() { 19 | 20 | WiFi.mode(WIFI_MODE_APSTA); 21 | WiFi.begin(WIFI_SSID, WIFI_PASS); 22 | 23 | Serial.printf("Connecting to %s .", WIFI_SSID); 24 | while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(200); } 25 | Serial.println(" ok"); 26 | 27 | IPAddress ip = WiFi.localIP(); 28 | 29 | Serial.printf("SSID: %s\n", WIFI_SSID); 30 | Serial.printf("Channel: %u\n", WiFi.channel()); 31 | Serial.printf("IP: %u.%u.%u.%u\n", ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, ip >> 24); 32 | } 33 | 34 | // ---------------------------------------------------------------------------- 35 | // ESP-NOW handling 36 | // ---------------------------------------------------------------------------- 37 | 38 | void onReceive(const uint8_t *mac_addr, const uint8_t *data, int len) { 39 | 40 | Serial.printf("received: %3u from %02x:%02x:%02x:%02x:%02x:%02x\n", 41 | (uint8_t) *data, 42 | mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5] 43 | ); 44 | 45 | } 46 | 47 | void initEspNow() { 48 | 49 | if (esp_now_init() != ESP_OK) { 50 | Serial.println("ESP NOW failed to initialize"); 51 | while (1); 52 | } 53 | 54 | esp_now_register_recv_cb(onReceive); 55 | } 56 | 57 | // ---------------------------------------------------------------------------- 58 | // Initialization 59 | // ---------------------------------------------------------------------------- 60 | 61 | void setup() { 62 | Serial.begin(115200); 63 | delay(500); 64 | 65 | initWiFi(); 66 | initEspNow(); 67 | } 68 | 69 | // ---------------------------------------------------------------------------- 70 | // Main control loop 71 | // ---------------------------------------------------------------------------- 72 | 73 | void loop() {} -------------------------------------------------------------------------------- /src/sender.cpp: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // ESP-NOW network + WiFi gateway to the Internet 3 | // ---------------------------------------------------------------------------- 4 | // ESP-NOW senders 5 | // ---------------------------------------------------------------------------- 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // ---------------------------------------------------------------------------- 13 | // WiFi handling 14 | // ---------------------------------------------------------------------------- 15 | 16 | constexpr char WIFI_SSID[] = "your wifi SSID"; 17 | 18 | int32_t getWiFiChannel(const char *ssid) { 19 | 20 | if (int32_t n = WiFi.scanNetworks()) { 21 | for (uint8_t i=0; i 2000) { 89 | 90 | uint8_t data = random(1, 256); 91 | esp_now_send(ESP_NOW_RECEIVER, (uint8_t *) &data, sizeof(uint8_t)); 92 | 93 | Serial.printf("sent: %3u on channel: %u\n", data, WiFi.channel()); 94 | 95 | last = millis(); 96 | } 97 | } --------------------------------------------------------------------------------