Model Scenery Tutorials

A Comprenshive Modeler's And Model Railroader's Guide

model scenery center

How To Use Use ESP Now Step By Step Guide

ESP-NOW is a protocol developed by Espressif for ESP8266 and ESP32 boards that enables low-power, low-latency wireless communication without needing a Wi-Fi network. Here’s a guide on how to use ESP-NOW with the Arduino IDE for ESP8266 and ESP32. With these steps, you can set up ESP-NOW communication between ESP8266 or ESP32 boards using the Arduino IDE.

Requirements
1. Arduino IDE (version 1.8.10 or later)
2. **ESP8266 or ESP32 board**
3. **ESP8266 or ESP32 board package** installed in the Arduino IDE

Setup

1. Install the ESP8266/ESP32 Board Package

– Open Arduino IDE.
– Go to `File > Preferences`.
– In the `Additional Board Manager URLs` field, add the following URLs:
– For ESP8266: `http://arduino.esp8266.com/stable/package_esp8266com_index.json`
– For ESP32: `https://dl.espressif.com/dl/package_esp32_index.json`
– Go to `Tools > Board > Boards Manager`.
– Search for `esp8266` or `esp32` and install the relevant package.

2. Select Your Board and Port

– Go to `Tools > Board` and select your ESP8266 or ESP32 board.
– Connect your board to the computer and select the appropriate port under `Tools > Port`.

ESP-NOW Example Code

Here are example sketches for sending and receiving data using ESP-NOW on ESP32. Similar steps apply for ESP8266 with minor modifications.

Sender Code

#include <esp_now.h>
#include <WiFi.h>

// MAC Address of the receiver (change this to the MAC address of your receiver)
uint8_t broadcastAddress[] = {0x24, 0x6F, 0x28, 0xAE, 0x92, 0xD8};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// 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”);
}

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 send callback
esp_now_register_send_cb(OnDataSent);

// Register peer
esp_now_peer_info_t 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;
}

// Fill the structure with data
strcpy(myData.a, “Hello ESP-NOW”);
myData.b = 123;
myData.c = 45.67;
myData.d = true;
}

void loop() {
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));

if (result == ESP_OK) {
Serial.println(“Sent with success”);
} else {
Serial.println(“Error sending the data”);
}

delay(2000);
}

Receiver Code

#include <esp_now.h>
#include <WiFi.h>

// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} struct_message;

// Create a struct_message called incomingData
struct_message incomingData;

// Callback when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
memcpy(&incomingData, incomingData, sizeof(incomingData));
Serial.print(“Bytes received: “);
Serial.println(len);
Serial.print(“String: “);
Serial.println(incomingData.a);
Serial.print(“Int: “);
Serial.println(incomingData.b);
Serial.print(“Float: “);
Serial.println(incomingData.c);
Serial.print(“Bool: “);
Serial.println(incomingData.d);
}

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 receive callback
esp_now_register_recv_cb(OnDataRecv);
}

void loop() {
}

Explanation

1. Sender Code
– Initializes ESP-NOW.
– Registers the send callback function.
– Adds a peer using the MAC address of the receiver.
– Fills the structure `myData` with some data.
– Sends the data periodically every 2 seconds.

2. Receiver Code
– Initializes ESP-NOW.
– Registers the receive callback function.
– Prints the received data.

Notes

– **MAC Address**: Replace the `broadcastAddress` in the sender code with the actual MAC address of your receiver.
– **Data Structure**: Ensure that the structure of the data (`struct_message`) is the same in both the sender and receiver code.
– **Callback Functions**: Customize the callback functions as needed for your application.

 

Scroll to top