├── .gitattributes ├── Master_BUTTON └── Master_BUTTON.ino ├── Master_DHT11 └── Master_DHT11.ino ├── Slave_DHT11 └── Slave_DHT11.ino └── Slave_LED └── Slave_LED.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Master_BUTTON/Master_BUTTON.ino: -------------------------------------------------------------------------------- 1 | /** 2 | ESPNOW - Basic communication - Master 3 | Date: 26th September 2017 4 | Author: Arvind Ravulavaru 5 | Purpose: ESPNow Communication between a Master ESP32 and a Slave ESP32 6 | Description: This sketch consists of the code for the Master module. 7 | Resources: (A bit outdated) 8 | a. https://espressif.com/sites/default/files/documentation/esp-now_user_guide_en.pdf 9 | b. http://www.esploradores.com/practica-6-conexion-esp-now/ 10 | 11 | << This Device Master >> 12 | 13 | Flow: Master 14 | Step 1 : ESPNow Init on Master and set it in STA mode 15 | Step 2 : Start scanning for Slave ESP32 (we have added a prefix of `slave` to the SSID of slave for an easy setup) 16 | Step 3 : Once found, add Slave as peer 17 | Step 4 : Register for send callback 18 | Step 5 : Start Transmitting data from Master to Slave 19 | 20 | Flow: Slave 21 | Step 1 : ESPNow Init on Slave 22 | Step 2 : Update the SSID of Slave with a prefix of `slave` 23 | Step 3 : Set Slave in AP mode 24 | Step 4 : Register for receive callback and wait for data 25 | Step 5 : Once data arrives, print it in the serial monitor 26 | 27 | Note: Master and Slave have been defined to easily understand the setup. 28 | Based on the ESPNOW API, there is no concept of Master and Slave. 29 | Any devices can act as master or salve. 30 | */ 31 | 32 | #include 33 | #include 34 | 35 | // Global copy of slave 36 | esp_now_peer_info_t slave; 37 | #define CHANNEL 0 38 | #define PRINTSCANRESULTS 0 39 | #define DELETEBEFOREPAIR 0 40 | 41 | int mac[6] = {0xAC, 0x67, 0xB2, 0x3C, 0x95, 0xD1}; 42 | 43 | // Init ESP Now with fallback 44 | void InitESPNow() { 45 | WiFi.disconnect(); 46 | if (esp_now_init() == ESP_OK) { 47 | Serial.println("ESPNow Init Success"); 48 | } 49 | else { 50 | Serial.println("ESPNow Init Failed"); 51 | // Retry InitESPNow, add a counte and then restart? 52 | // InitESPNow(); 53 | // or Simply Restart 54 | ESP.restart(); 55 | } 56 | } 57 | 58 | // Scan for slaves in AP mode 59 | void ScanForSlave() { 60 | //int8_t scanResults = WiFi.scanNetworks(); 61 | // reset on each scan 62 | bool slaveFound = 0; 63 | memset(&slave, 0, sizeof(slave)); 64 | 65 | 66 | for (int ii = 0; ii < 6; ++ii ) 67 | { 68 | slave.peer_addr[ii] = (uint8_t) mac[ii]; 69 | } 70 | 71 | slave.channel = CHANNEL; // pick a channel 72 | slave.encrypt = 0; // no encryption 73 | 74 | slaveFound = 1; 75 | 76 | 77 | 78 | if (slaveFound) 79 | { 80 | Serial.println("Slave Found, processing.."); 81 | } else 82 | { 83 | Serial.println("Slave Not Found, trying again."); 84 | } 85 | 86 | } 87 | 88 | // Check if the slave is already paired with the master. 89 | // If not, pair the slave with master 90 | bool manageSlave() { 91 | if (slave.channel == CHANNEL) { 92 | if (DELETEBEFOREPAIR) { 93 | deletePeer(); 94 | } 95 | 96 | Serial.print("Slave Status: "); 97 | // check if the peer exists 98 | bool exists = esp_now_is_peer_exist(slave.peer_addr); 99 | if ( exists) { 100 | // Slave already paired. 101 | Serial.println("Already Paired"); 102 | return true; 103 | } else { 104 | // Slave not paired, attempt pair 105 | esp_err_t addStatus = esp_now_add_peer(&slave); 106 | if (addStatus == ESP_OK) { 107 | // Pair success 108 | Serial.println("Pair success"); 109 | return true; 110 | } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) { 111 | // How did we get so far!! 112 | Serial.println("ESPNOW Not Init"); 113 | return false; 114 | } else if (addStatus == ESP_ERR_ESPNOW_ARG) { 115 | Serial.println("Invalid Argument"); 116 | return false; 117 | } else if (addStatus == ESP_ERR_ESPNOW_FULL) { 118 | Serial.println("Peer list full"); 119 | return false; 120 | } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) { 121 | Serial.println("Out of memory"); 122 | return false; 123 | } else if (addStatus == ESP_ERR_ESPNOW_EXIST) { 124 | Serial.println("Peer Exists"); 125 | return true; 126 | } else { 127 | Serial.println("Not sure what happened"); 128 | return false; 129 | } 130 | } 131 | } else { 132 | // No slave found to process 133 | Serial.println("No Slave found to process"); 134 | return false; 135 | } 136 | } 137 | 138 | void deletePeer() { 139 | esp_err_t delStatus = esp_now_del_peer(slave.peer_addr); 140 | Serial.print("Slave Delete Status: "); 141 | if (delStatus == ESP_OK) { 142 | // Delete success 143 | Serial.println("Success"); 144 | } else if (delStatus == ESP_ERR_ESPNOW_NOT_INIT) { 145 | // How did we get so far!! 146 | Serial.println("ESPNOW Not Init"); 147 | } else if (delStatus == ESP_ERR_ESPNOW_ARG) { 148 | Serial.println("Invalid Argument"); 149 | } else if (delStatus == ESP_ERR_ESPNOW_NOT_FOUND) { 150 | Serial.println("Peer not found."); 151 | } else { 152 | Serial.println("Not sure what happened"); 153 | } 154 | } 155 | 156 | uint8_t data = 0; 157 | // send data 158 | void sendData() { 159 | 160 | data = digitalRead(0); 161 | 162 | const uint8_t *peer_addr = slave.peer_addr; 163 | Serial.print("Sending: "); Serial.println(data); 164 | // Send message via ESP-NOW 165 | esp_err_t result = esp_now_send(peer_addr, (uint8_t *) &myData, sizeof(myData)); 166 | 167 | Serial.print("Send Status: "); 168 | if (result == ESP_OK) { 169 | Serial.println("Success"); 170 | } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 171 | // How did we get so far!! 172 | Serial.println("ESPNOW not Init."); 173 | } else if (result == ESP_ERR_ESPNOW_ARG) { 174 | Serial.println("Invalid Argument"); 175 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 176 | Serial.println("Internal Error"); 177 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 178 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 179 | } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 180 | Serial.println("Peer not found."); 181 | } else { 182 | Serial.println("Not sure what happened"); 183 | } 184 | } 185 | 186 | // callback when data is sent from Master to Slave 187 | void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { 188 | char macStr[18]; 189 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", 190 | mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); 191 | Serial.print("Last Packet Sent to: "); Serial.println(macStr); 192 | Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); 193 | } 194 | 195 | void setup() { 196 | Serial.begin(115200); 197 | pinMode(0,INPUT); 198 | //Set device in STA mode to begin with 199 | WiFi.mode(WIFI_STA); 200 | Serial.println("ESPNow/Basic/Master Example"); 201 | // This is the mac address of the Master in Station Mode 202 | Serial.print("STA MAC: "); Serial.println(WiFi.macAddress()); 203 | // Init ESPNow with a fallback logic 204 | InitESPNow(); 205 | // Once ESPNow is successfully Init, we will register for Send CB to 206 | // get the status of Trasnmitted packet 207 | esp_now_register_send_cb(OnDataSent); 208 | } 209 | 210 | void loop() { 211 | // In the loop we scan for slave 212 | ScanForSlave(); 213 | // If Slave is found, it would be populate in `slave` variable 214 | // We will check if `slave` is defined and then we proceed further 215 | if (slave.channel == CHANNEL) { // check if slave channel is defined 216 | // `slave` is defined 217 | // Add slave as peer if it has not been added already 218 | bool isPaired = manageSlave(); 219 | if (isPaired) { 220 | // pair success or already paired 221 | // Send data to device 222 | sendData(); 223 | } else { 224 | // slave pair failed 225 | Serial.println("Slave pair failed!"); 226 | } 227 | } 228 | else { 229 | // No slave found to process 230 | } 231 | 232 | // wait for 3seconds to run the logic again 233 | 234 | } 235 | -------------------------------------------------------------------------------- /Master_DHT11/Master_DHT11.ino: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define DHTPIN 2 8 | 9 | #define DHTTYPE DHT11 10 | 11 | DHT dht(DHTPIN, DHTTYPE); 12 | 13 | // Global copy of slave 14 | esp_now_peer_info_t slave; 15 | #define CHANNEL 0 16 | #define PRINTSCANRESULTS 0 17 | #define DELETEBEFOREPAIR 0 18 | 19 | // Structure example to send data 20 | // Must match the receiver structure 21 | typedef struct struct_message { 22 | float temp; 23 | float hum; 24 | } struct_message; 25 | 26 | // Create a struct_message called myData 27 | struct_message sensor; 28 | // Init ESP Now with fallback 29 | void InitESPNow() { 30 | WiFi.disconnect(); 31 | if (esp_now_init() == ESP_OK) { 32 | Serial.println("ESPNow Init Success"); 33 | } 34 | else { 35 | Serial.println("ESPNow Init Failed"); 36 | // Retry InitESPNow, add a counte and then restart? 37 | // InitESPNow(); 38 | // or Simply Restart 39 | ESP.restart(); 40 | } 41 | } 42 | 43 | // Scan for slaves in AP mode 44 | void ScanForSlave() { 45 | //int8_t scanResults = WiFi.scanNetworks(); 46 | // reset on each scan 47 | bool slaveFound = 0; 48 | memset(&slave, 0, sizeof(slave)); 49 | 50 | int mac[6] = {0xAC, 0x67, 0xB2, 0x3C, 0x95, 0xD1}; 51 | 52 | for (int ii = 0; ii < 6; ++ii ) 53 | { 54 | slave.peer_addr[ii] = (uint8_t) mac[ii]; 55 | } 56 | 57 | slave.channel = CHANNEL; // pick a channel 58 | slave.encrypt = 0; // no encryption 59 | 60 | slaveFound = 1; 61 | 62 | 63 | 64 | if (slaveFound) 65 | { 66 | Serial.println("Slave Found, processing.."); 67 | } else 68 | { 69 | Serial.println("Slave Not Found, trying again."); 70 | } 71 | 72 | } 73 | 74 | // Check if the slave is already paired with the master. 75 | // If not, pair the slave with master 76 | bool manageSlave() { 77 | if (slave.channel == CHANNEL) { 78 | if (DELETEBEFOREPAIR) { 79 | deletePeer(); 80 | } 81 | 82 | Serial.print("Slave Status: "); 83 | // check if the peer exists 84 | bool exists = esp_now_is_peer_exist(slave.peer_addr); 85 | if ( exists) { 86 | // Slave already paired. 87 | Serial.println("Already Paired"); 88 | return true; 89 | } else { 90 | // Slave not paired, attempt pair 91 | esp_err_t addStatus = esp_now_add_peer(&slave); 92 | if (addStatus == ESP_OK) { 93 | // Pair success 94 | Serial.println("Pair success"); 95 | return true; 96 | } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) { 97 | // How did we get so far!! 98 | Serial.println("ESPNOW Not Init"); 99 | return false; 100 | } else if (addStatus == ESP_ERR_ESPNOW_ARG) { 101 | Serial.println("Invalid Argument"); 102 | return false; 103 | } else if (addStatus == ESP_ERR_ESPNOW_FULL) { 104 | Serial.println("Peer list full"); 105 | return false; 106 | } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) { 107 | Serial.println("Out of memory"); 108 | return false; 109 | } else if (addStatus == ESP_ERR_ESPNOW_EXIST) { 110 | Serial.println("Peer Exists"); 111 | return true; 112 | } else { 113 | Serial.println("Not sure what happened"); 114 | return false; 115 | } 116 | } 117 | } else { 118 | // No slave found to process 119 | Serial.println("No Slave found to process"); 120 | return false; 121 | } 122 | } 123 | 124 | void deletePeer() { 125 | esp_err_t delStatus = esp_now_del_peer(slave.peer_addr); 126 | Serial.print("Slave Delete Status: "); 127 | if (delStatus == ESP_OK) { 128 | // Delete success 129 | Serial.println("Success"); 130 | } else if (delStatus == ESP_ERR_ESPNOW_NOT_INIT) { 131 | // How did we get so far!! 132 | Serial.println("ESPNOW Not Init"); 133 | } else if (delStatus == ESP_ERR_ESPNOW_ARG) { 134 | Serial.println("Invalid Argument"); 135 | } else if (delStatus == ESP_ERR_ESPNOW_NOT_FOUND) { 136 | Serial.println("Peer not found."); 137 | } else { 138 | Serial.println("Not sure what happened"); 139 | } 140 | } 141 | 142 | uint8_t data = 0; 143 | // send data 144 | void sendData() 145 | { 146 | sensor.temp = dht.readHumidity(); 147 | sensor.hum = dht.readTemperature(); 148 | 149 | const uint8_t *peer_addr = slave.peer_addr; 150 | Serial.print("Sending: "); Serial.println(data); 151 | // Send message via ESP-NOW 152 | esp_err_t result = esp_now_send(peer_addr, (uint8_t *) &sensor, sizeof(sensor)); 153 | 154 | Serial.print("Send Status: "); 155 | if (result == ESP_OK) { 156 | Serial.println("Success"); 157 | } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { 158 | // How did we get so far!! 159 | Serial.println("ESPNOW not Init."); 160 | } else if (result == ESP_ERR_ESPNOW_ARG) { 161 | Serial.println("Invalid Argument"); 162 | } else if (result == ESP_ERR_ESPNOW_INTERNAL) { 163 | Serial.println("Internal Error"); 164 | } else if (result == ESP_ERR_ESPNOW_NO_MEM) { 165 | Serial.println("ESP_ERR_ESPNOW_NO_MEM"); 166 | } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) { 167 | Serial.println("Peer not found."); 168 | } else { 169 | Serial.println("Not sure what happened"); 170 | } 171 | } 172 | 173 | // callback when data is sent from Master to Slave 174 | void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { 175 | char macStr[18]; 176 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", 177 | mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); 178 | Serial.print("Last Packet Sent to: "); Serial.println(macStr); 179 | Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); 180 | } 181 | 182 | void setup() { 183 | Serial.begin(115200); 184 | dht.begin(); 185 | pinMode(0,INPUT); 186 | //Set device in STA mode to begin with 187 | WiFi.mode(WIFI_STA); 188 | Serial.println("ESPNow/Basic/Master Example"); 189 | // This is the mac address of the Master in Station Mode 190 | Serial.print("STA MAC: "); Serial.println(WiFi.macAddress()); 191 | // Init ESPNow with a fallback logic 192 | InitESPNow(); 193 | // Once ESPNow is successfully Init, we will register for Send CB to 194 | // get the status of Trasnmitted packet 195 | esp_now_register_send_cb(OnDataSent); 196 | } 197 | 198 | void loop() { 199 | // In the loop we scan for slave 200 | ScanForSlave(); 201 | // If Slave is found, it would be populate in `slave` variable 202 | // We will check if `slave` is defined and then we proceed further 203 | if (slave.channel == CHANNEL) { // check if slave channel is defined 204 | // `slave` is defined 205 | // Add slave as peer if it has not been added already 206 | bool isPaired = manageSlave(); 207 | if (isPaired) { 208 | // pair success or already paired 209 | // Send data to device 210 | sendData(); 211 | } else { 212 | // slave pair failed 213 | Serial.println("Slave pair failed!"); 214 | } 215 | } 216 | else { 217 | // No slave found to process 218 | } 219 | delay(3000); 220 | // wait for 3seconds to run the logic again 221 | 222 | } 223 | -------------------------------------------------------------------------------- /Slave_DHT11/Slave_DHT11.ino: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | 6 | #define CHANNEL 0 7 | 8 | // Structure example to send data 9 | // Must match the receiver structure 10 | typedef struct struct_message { 11 | float temp; 12 | float hum; 13 | } struct_message; 14 | 15 | // Create a struct_message called myData 16 | struct_message sensor; 17 | // Init ESP Now with fallback 18 | void InitESPNow() { 19 | WiFi.disconnect(); 20 | if (esp_now_init() == ESP_OK) { 21 | Serial.println("ESPNow Init Success"); 22 | } 23 | else { 24 | Serial.println("ESPNow Init Failed"); 25 | // Retry InitESPNow, add a counte and then restart? 26 | // InitESPNow(); 27 | // or Simply Restart 28 | ESP.restart(); 29 | } 30 | } 31 | 32 | // config AP SSID 33 | void configDeviceAP() { 34 | const char *SSID = "Slave_1"; 35 | bool result = WiFi.softAP(SSID, "Slave_1_Password", CHANNEL, 0); 36 | if (!result) { 37 | Serial.println("AP Config failed."); 38 | } else { 39 | Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID)); 40 | } 41 | } 42 | 43 | void setup() { 44 | Serial.begin(115200); 45 | pinMode(2,OUTPUT); 46 | Serial.println("ESPNow/Basic/Slave Example"); 47 | //Set device in AP mode to begin with 48 | WiFi.mode(WIFI_AP); 49 | // configure device AP mode 50 | configDeviceAP(); 51 | // This is the mac address of the Slave in AP Mode 52 | Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress()); 53 | // Init ESPNow with a fallback logic 54 | InitESPNow(); 55 | // Once ESPNow is successfully Init, we will register for recv CB to 56 | // get recv packer info. 57 | esp_now_register_recv_cb(OnDataRecv); 58 | } 59 | 60 | // callback when data is recv from Master 61 | void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { 62 | memcpy(&sensor, data, sizeof(sensor)); 63 | Serial.print("Bytes received: "); 64 | Serial.println(data_len); 65 | Serial.print("Temperature: "); 66 | Serial.println(sensor.temp); 67 | Serial.print("Humidity: "); 68 | Serial.println(sensor.hum); 69 | Serial.println(); 70 | } 71 | 72 | void loop() { 73 | // Chill 74 | } 75 | -------------------------------------------------------------------------------- /Slave_LED/Slave_LED.ino: -------------------------------------------------------------------------------- 1 | /** 2 | ESPNOW - Basic communication - Slave 3 | Date: 26th September 2017 4 | Author: Arvind Ravulavaru 5 | Purpose: ESPNow Communication between a Master ESP32 and a Slave ESP32 6 | Description: This sketch consists of the code for the Slave module. 7 | Resources: (A bit outdated) 8 | a. https://espressif.com/sites/default/files/documentation/esp-now_user_guide_en.pdf 9 | b. http://www.esploradores.com/practica-6-conexion-esp-now/ 10 | 11 | << This Device Slave >> 12 | 13 | Flow: Master 14 | Step 1 : ESPNow Init on Master and set it in STA mode 15 | Step 2 : Start scanning for Slave ESP32 (we have added a prefix of `slave` to the SSID of slave for an easy setup) 16 | Step 3 : Once found, add Slave as peer 17 | Step 4 : Register for send callback 18 | Step 5 : Start Transmitting data from Master to Slave 19 | 20 | Flow: Slave 21 | Step 1 : ESPNow Init on Slave 22 | Step 2 : Update the SSID of Slave with a prefix of `slave` 23 | Step 3 : Set Slave in AP mode 24 | Step 4 : Register for receive callback and wait for data 25 | Step 5 : Once data arrives, print it in the serial monitor 26 | 27 | Note: Master and Slave have been defined to easily understand the setup. 28 | Based on the ESPNOW API, there is no concept of Master and Slave. 29 | Any devices can act as master or salve. 30 | */ 31 | 32 | #include 33 | #include 34 | 35 | #define CHANNEL 0 36 | 37 | 38 | // Init ESP Now with fallback 39 | void InitESPNow() { 40 | WiFi.disconnect(); 41 | if (esp_now_init() == ESP_OK) { 42 | Serial.println("ESPNow Init Success"); 43 | } 44 | else { 45 | Serial.println("ESPNow Init Failed"); 46 | // Retry InitESPNow, add a counte and then restart? 47 | // InitESPNow(); 48 | // or Simply Restart 49 | ESP.restart(); 50 | } 51 | } 52 | 53 | // config AP SSID 54 | void configDeviceAP() { 55 | const char *SSID = "Slave_1"; 56 | bool result = WiFi.softAP(SSID, "Slave_1_Password", CHANNEL, 0); 57 | if (!result) { 58 | Serial.println("AP Config failed."); 59 | } else { 60 | Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID)); 61 | } 62 | } 63 | 64 | void setup() { 65 | Serial.begin(115200); 66 | pinMode(2, OUTPUT); 67 | Serial.println("ESPNow/Basic/Slave Example"); 68 | //Set device in AP mode to begin with 69 | WiFi.mode(WIFI_AP); 70 | // configure device AP mode 71 | configDeviceAP(); 72 | // This is the mac address of the Slave in AP Mode 73 | Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress()); 74 | // Init ESPNow with a fallback logic 75 | InitESPNow(); 76 | // Once ESPNow is successfully Init, we will register for recv CB to 77 | // get recv packer info. 78 | esp_now_register_recv_cb(OnDataRecv); 79 | } 80 | 81 | // callback when data is recv from Master 82 | void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { 83 | char macStr[18]; 84 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", 85 | mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); 86 | Serial.print("Last Packet Recv from: "); Serial.println(macStr); 87 | Serial.print("Last Packet Recv Data: "); Serial.println(*data); 88 | Serial.println(""); 89 | digitalWrite(2, *data); 90 | } 91 | 92 | void loop() { 93 | // Chill 94 | } 95 | --------------------------------------------------------------------------------