├── .gitattributes └── ESPNOW_Many2Many └── ESPNOW_Many2Many.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /ESPNOW_Many2Many/ESPNOW_Many2Many.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * This is the code for Many To Many Communication using ESPNOW for ESP32 3 | * 4 | * 5 | * The Full tutorial video of this project is uploaded on our youtube channel 6 | * 7 | * https://www.youtube.com/techiesms 8 | * 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | #define but_on 13 15 | #define but_off 12 16 | #define LED 14 17 | 18 | 19 | bool new_data = 0; // Flag to send data only once 20 | 21 | 22 | // Universal MAC Address 23 | uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 24 | 25 | bool incomingLED_status; 26 | 27 | // Structure example to send data 28 | // Must match the receiver structure 29 | typedef struct struct_message 30 | { 31 | int but_status; 32 | } struct_message; 33 | 34 | // Create a struct_message called myData 35 | struct_message myData; 36 | 37 | struct_message incomingReadings; 38 | // Create peer interface 39 | esp_now_peer_info_t peerInfo; 40 | 41 | // callback when data is sent 42 | void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { 43 | Serial.print("\r\nLast Packet Send Status:\t"); 44 | Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); 45 | } 46 | void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { 47 | memcpy(&incomingReadings, incomingData, sizeof(incomingReadings)); 48 | Serial.print("Bytes received: "); 49 | Serial.println(len); 50 | incomingLED_status = incomingReadings.but_status; 51 | Serial.println(incomingReadings.but_status); 52 | digitalWrite(LED, incomingLED_status); 53 | 54 | } 55 | 56 | void setup() { 57 | // Init Serial Monitor 58 | Serial.begin(115200); 59 | pinMode(but_on, INPUT_PULLUP); 60 | pinMode(but_off, INPUT_PULLUP); 61 | pinMode(LED, OUTPUT); 62 | 63 | // Set device as a Wi-Fi Station 64 | WiFi.mode(WIFI_STA); 65 | 66 | // Init ESP-NOW 67 | if (esp_now_init() != ESP_OK) { 68 | Serial.println("Error initializing ESP-NOW"); 69 | return; 70 | } 71 | 72 | // Send Callback Function 73 | esp_now_register_send_cb(OnDataSent); 74 | 75 | // Receive Callback Function 76 | esp_now_register_recv_cb(OnDataRecv); 77 | 78 | // Register peer 79 | memcpy(peerInfo.peer_addr, broadcastAddress, 6); 80 | peerInfo.channel = 0; 81 | peerInfo.encrypt = false; 82 | 83 | // Add peer 84 | if (esp_now_add_peer(&peerInfo) != ESP_OK) { 85 | Serial.println("Failed to add peer"); 86 | return; 87 | } 88 | } 89 | 90 | void loop() 91 | { 92 | // Set values to send 93 | if (digitalRead(but_on) == LOW && new_data == 1) 94 | { 95 | myData.but_status = 1; 96 | new_data = 0; 97 | } 98 | if (digitalRead(but_off) == LOW && new_data == 1) 99 | { 100 | myData.but_status = 0; 101 | new_data = 0; 102 | } 103 | 104 | esp_err_t result; // declaration 105 | 106 | // Send message via ESP-NOW 107 | if (new_data == 0) 108 | { 109 | 110 | result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData)); 111 | new_data = 1; 112 | } 113 | 114 | if (result == ESP_OK) 115 | { 116 | Serial.println("Sent with success"); 117 | } 118 | else 119 | { 120 | Serial.println("Error sending the data"); 121 | } 122 | 123 | // delay(10000); 124 | } 125 | --------------------------------------------------------------------------------