|
| 1 | +/* |
| 2 | + ESP-NOW Serial Example - Unicast transmission |
| 3 | + Lucas Saavedra Vaz - 2024 |
| 4 | + Send data between two ESP32s using the ESP-NOW protocol in one-to-one (unicast) configuration. |
| 5 | + Note that different MAC addresses are used for different interfaces. |
| 6 | + To properly visualize the data being sent, set the line ending in the Serial Monitor to "Both NL & CR". |
| 7 | +*/ |
| 8 | + |
| 9 | +#include "ESP32_NOW_Serial.h" |
| 10 | +#include "MacAddress.h" |
| 11 | +#include "WiFi.h" |
| 12 | + |
| 13 | +#define ESPNOW_WIFI_MODE_STATION 0 |
| 14 | +#define ESPNOW_WIFI_CHANNEL 1 |
| 15 | + |
| 16 | +#if ESPNOW_WIFI_MODE_STATION // ESP-NOW using WiFi Station mode |
| 17 | +#define ESPNOW_WIFI_IF WIFI_IF_STA |
| 18 | +#define GET_IF_MAC WiFi.macAddress |
| 19 | + |
| 20 | +// Station mode MAC addresses of the devices in the network |
| 21 | +// Device 1 - F4:12:FA:42:B6:E8 |
| 22 | +const MacAddress mac1({0xF4, 0x12, 0xFA, 0x42, 0xB6, 0xE8}); |
| 23 | +// Device 2 - F4:12:FA:40:64:4C |
| 24 | +const MacAddress mac2({0xF4, 0x12, 0xFA, 0x40, 0x64, 0x4C}); |
| 25 | +#else // ESP-NOW using WiFi AP mode |
| 26 | +#define ESPNOW_WIFI_IF WIFI_IF_AP |
| 27 | +#define GET_IF_MAC WiFi.softAPmacAddress |
| 28 | + |
| 29 | +// AP mode MAC addresses of the devices in the network |
| 30 | +// Device 1 - F6:12:FA:42:B6:E8 |
| 31 | +const MacAddress mac1({0xF6, 0x12, 0xFA, 0x42, 0xB6, 0xE8}); |
| 32 | +// Device 2 - F6:12:FA:40:64:4C |
| 33 | +const MacAddress mac2({0xF6, 0x12, 0xFA, 0x40, 0x64, 0x4C}); |
| 34 | +#endif |
| 35 | + |
| 36 | +ESP_NOW_Serial_Class *esp_now_serial; |
| 37 | + |
| 38 | +void setup() { |
| 39 | + MacAddress current_mac; |
| 40 | + |
| 41 | + Serial.begin(115200); |
| 42 | + |
| 43 | + Serial.print("WiFi Mode: "); |
| 44 | + |
| 45 | + #if ESPNOW_WIFI_MODE_STATION |
| 46 | + Serial.println("STA"); |
| 47 | + WiFi.mode(ESPNOW_WIFI_MODE); |
| 48 | + esp_wifi_set_channel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE); |
| 49 | + #else |
| 50 | + Serial.println("AP"); |
| 51 | + WiFi.softAP(WiFi.getHostname(), NULL, ESPNOW_WIFI_CHANNEL, 1); |
| 52 | + #endif |
| 53 | + |
| 54 | + Serial.print("Channel: "); |
| 55 | + Serial.println(ESPNOW_WIFI_CHANNEL); |
| 56 | + |
| 57 | + String mac = GET_IF_MAC(); |
| 58 | + Serial.print("MAC Address: "); |
| 59 | + current_mac.fromString(mac); |
| 60 | + Serial.println(mac); |
| 61 | + |
| 62 | + WiFi.disconnect(); |
| 63 | + |
| 64 | + if (current_mac == mac1) |
| 65 | + { |
| 66 | + Serial.println("I'm the Device 1\n"); |
| 67 | + // Pass the MAC address of the other device to the ESP_NOW_Serial_Class |
| 68 | + esp_now_serial = new ESP_NOW_Serial_Class(mac2, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF); |
| 69 | + } |
| 70 | + else if (current_mac == mac2) |
| 71 | + { |
| 72 | + Serial.println("I'm the Device 2\n"); |
| 73 | + // Pass the MAC address of the other device to the ESP_NOW_Serial_Class |
| 74 | + esp_now_serial = new ESP_NOW_Serial_Class(mac1, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + Serial.println("Unknown MAC address. Please update the mac addresses in the sketch\n"); |
| 79 | + while(1); |
| 80 | + } |
| 81 | + |
| 82 | + // Start the ESP-NOW communication |
| 83 | + Serial.println("ESP-NOW communication starting..."); |
| 84 | + esp_now_serial->begin(115200); |
| 85 | + Serial.println("You can now send data between the devices using the Serial Monitor\n"); |
| 86 | +} |
| 87 | + |
| 88 | +void loop() { |
| 89 | + while (esp_now_serial->available() > 0) |
| 90 | + { |
| 91 | + Serial.write(esp_now_serial->read()); |
| 92 | + } |
| 93 | + |
| 94 | + while (Serial.available() && esp_now_serial->availableForWrite()) |
| 95 | + { |
| 96 | + if (esp_now_serial->write(Serial.read()) <= 0) |
| 97 | + { |
| 98 | + Serial.println("Failed to send data"); |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + delay(1); |
| 104 | +} |
0 commit comments