## ESP-NOW RandomNerd has a well-done [tutorial on one-way ESP NOW](https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/). It sends data in the form of a structure, which is great, but a little complex for a first example. RandomNerd also has a [tutorial](https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/) on two-way sending. This is completely asynchronous - the callback function for receiving puts the result in a memory location that can be accessed as needed. The tutorial code shows how to use a commercially available sensor and an OLED. Below we show a minimal example instead. We simplified the code so that each board receives a byte, adds one, and sends it back. First, get mac addresses of boards: ``` #include "WiFi.h" void setup(){ Serial.begin(115200); WiFi.mode(WIFI_MODE_STA); Serial.println(WiFi.macAddress()); } void loop(){ } ``` <!-- - [code for TxRx](./txrx.txt) --> ``` /* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp-now-two-way-communication-esp32/ Simplified for demo purposes by R Hart and N Melenbrink 3/29/22. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. */ #include <esp_now.h> #include <WiFi.h> // REPLACE WITH THE MAC Address of your receiver. Code is the same for both boards, with the exception of the following line. uint8_t broadcastAddress[] = {0xFC, 0xF5, 0xC4, 0x6B, 0xAC, 0x68}; this is board no 1. //uint8_t broadcastAddress[] = {0x24, 0x62, 0xAB, 0xB0, 0x34, 0x8C}; this is board no 2 // Variable to store if sending data was successful String success; byte incomingByte; byte outgoingByte; // Callback when data is sent void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { Serial.print("\r\nLast Packet Send Status:\t"); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); if (status ==0){ success = "Delivery Success :)"; } else{ success = "Delivery Fail :("; } } // Callback when data is received void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { memcpy(&incomingByte, incomingData, sizeof(incomingByte)); Serial.print("Bytes received: "); Serial.println(len); } void setup() { // Init Serial Monitor Serial.begin(115200); // Set device as a Wi-Fi Station WiFi.mode(WIFI_STA); // Init ESP-NOW if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; } // Register for a callback function that will be called when data is received esp_now_register_send_cb(OnDataSent); // Register peer esp_now_peer_info_t peerInfo; memset(&peerInfo, 0, sizeof(peerInfo)); memcpy(peerInfo.peer_addr, broadcastAddress, 6); peerInfo.channel = 0; peerInfo.encrypt = false; // Add peer if (esp_now_add_peer(&peerInfo) != ESP_OK){ Serial.println("Failed to add peer"); return; } // Register for a callback function that will be called when data is received esp_now_register_recv_cb(OnDataRecv); } void loop() { Serial.println(incomingByte); outgoingByte = incomingByte+1; // Send message via ESP-NOW esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &outgoingByte, sizeof(outgoingByte)); if (result == ESP_OK) { Serial.println("Sent with success"); } else { Serial.println("Error sending the data"); } delay(1000); } ``` [hello-world for ESP NOW at hackaday](https://hackaday.io/project/164132-hello-world-for-esp-now) uses a different mode of sending, in which one device broadcasts to all devices in range.