Example Code for Arduino-ESP-NOW Data Transfer
ESP-NOW is a kind of connectionless WiFi communication protocol which is defined by Espressif. It can be widely used in smart light, remote controlling, sensor, etc.
Hardware Preparation
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Add ESP32 board to Arduino IDE
Step
- Use the Get Controller MAC Address code snippet to retrieve the device's MAC address.
- Fill the MAC address into the
Transmitting & Receiving DataorTransmitting Data to Multiple Devicescode, then flash the code to the device.
Sample Code
Get Controller MAC Address
Burn the code, open the serial port and you can see the device MAC address.
#include "WiFi.h"
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_MODE_STA);
}
void loop(){
Serial.println(WiFi.macAddress());
delay(1000);
}
Transimitting & Receiving Data
Fill in the MAC address and burn the code, then data can be transmitted and received between two devices.
#include <esp_now.h>
#include <WiFi.h>
//MAC
uint8_t MAC1[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
typedef struct struct_message {
char a[16];
int b;
float c;
bool d;
} struct_message;
struct_message sendData;
struct_message recvData;
esp_now_peer_info_t peerInfo;
//Callback function when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
if(status == ESP_NOW_SEND_SUCCESS){
Serial.println("Send_Success");
}else{
Serial.println("Send_Fail");
}
}
//Callback function when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *Data, int len) {
memcpy(&recvData, Data, sizeof(recvData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.println(recvData.a);
Serial.println(recvData.b);
Serial.println(recvData.c);
Serial.println(recvData.d);
Serial.println("---------");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
//Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing");
return;
}
//Register for callback function when data is sent
esp_now_register_send_cb(OnDataSent);
peerInfo.channel = 0;
peerInfo.encrypt = false;
//Register MAC1 device
memcpy(peerInfo.peer_addr, MAC1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
//Register for callback function whe data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
strcpy(sendData.a, "DFRobot");
sendData.b = 10;
sendData.c = 9.9;
sendData.d = true;
esp_err_t result = esp_now_send(MAC1, (uint8_t *)&sendData, sizeof(sendData));
if (result == ESP_OK) {
Serial.println("Send success");
}
else {
Serial.println("Send Fail");
}
delay(2000);
}
Transimitting Data to Multiple Devices
Fill in the MAC address and burn the code, then data can be transmitted to multiple devices at the same time.
#include <esp_now.h>
#include <WiFi.h>
//MAC
uint8_t MAC1[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
uint8_t MAC2[] = {0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB};
typedef struct struct_message {
uint8_t ID;
int data;
} struct_message;
struct_message sendData;
struct_message recvData;
esp_now_peer_info_t peerInfo;
//Callback function when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
if(status == ESP_NOW_SEND_SUCCESS){
Serial.println("Send Success");
}else{
Serial.println("Send Fail");
}
}
// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *Data, int len) {
memcpy(&recvData, Data, sizeof(recvData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.println(recvData.ID);
Serial.println(recvData.data);
Serial.println("---------");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
//Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing");
return;
}
//Register for callback function when data is sent
esp_now_register_send_cb(OnDataSent);
peerInfo.channel = 0;
peerInfo.encrypt = false;
//Register MAC1 device
memcpy(peerInfo.peer_addr, MAC1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
//Register MAC2 device
memcpy(peerInfo.peer_addr, MAC2, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
//Register for callback function when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
sendData.ID = 0;
sendData.data = 10;
//Send information to all registered devices
esp_err_t result = esp_now_send(0, (uint8_t *)&sendData, sizeof(sendData));
if (result == ESP_OK) {
Serial.println("Send success");
}
else {
Serial.println("Send Fail");
}
//Send information to the specified MAC device
//esp_err_t result = esp_now_send(MAC1, (uint8_t *)&sendData, sizeof(sendData));
//if (result == ESP_OK) {
// Serial.println("Send success");
//}
//else {
// Serial.println("Send Fail");
//}
delay(2000);
}
Was this article helpful?
