Update index.md

This commit is contained in:
2025-07-27 04:27:02 -04:00
committed by GitHub
parent 5d6743c594
commit ddbe20c6a5

View File

@@ -8,9 +8,9 @@ title: "IoT Link | Wireless communication for embedded devices powered by bitBoa
<i>Controlling embedded devices wirelessly using ESP32-C3 WROOM</i>
This repository presents a practical introduction to implementing ESP-NOW communication between two ESP32 microcontrollers, one configured as a transmitter and the other as a receiver. The transmitter collects control data—such as joystick positions and motor PWM values—and sends it using a structured format and FreeRTOS tasks to manage concurrent operations. The sendData() function handles data preparation and transmission, while a callback monitors delivery status and manages errors.
This repository presents a practical introduction to implementing **ESP-NOW** communication between two ESP32 microcontrollers, one configured as a transmitter and the other as a receiver. The transmitter collects control data—such as joystick positions and motor PWM values—and sends it using a structured format and FreeRTOS tasks to manage concurrent operations. The *sendData()* function handles data preparation and transmission, while a callback monitors delivery status and manages errors.
On the receiver side, the focus is on registering the transmitters MAC address and using the onDataReceived() callback to process incoming data. The post emphasizes the importance of consistent configuration between devices, including shared data structures, Wi-Fi channel settings, and peer registration. This ensures reliable, low-latency communication without the need for a traditional Wi-Fi network, making ESP-NOW a suitable protocol for remote control applications using ESP32.
On the receiver side, the focus is on registering the transmitters MAC address and using the *onDataReceived()* callback to process incoming data. The post emphasizes the importance of consistent configuration between devices, including shared data structures, Wi-Fi channel settings, and peer registration. This ensures reliable, low-latency communication without the need for a traditional Wi-Fi network, making ESP-NOW a suitable protocol for remote control applications using ESP32.
# COMMON CODE BLOCKS FIRST
@@ -38,7 +38,7 @@ typedef struct {
## Getting ESP-NOW Ready
The part of code responsible for initializing ESP-NOW is the same both the systems. The app_main() in both transmitter and receiver devices must begin by initializing the Non-Volatile Storage (NVS) required for Wi-Fi and ESP-NOW operations. After that, calling wifi_init() sets the devices to Wi-Fi station mode necessary for ESP-NOW communication.
The part of code responsible for initializing ESP-NOW is the same both the systems. The *app_main()* in both transmitter and receiver devices must begin by initializing the Non-Volatile Storage (NVS) required for Wi-Fi and ESP-NOW operations. After that, calling *wifi_init()* sets the devices to Wi-Fi station mode necessary for ESP-NOW communication.
This function is essential for both transmitter and receiver devices, as ESP-NOW must be initialized before sending or receiving any data.
@@ -76,9 +76,9 @@ void wifi_init()
## Transmitter
On the transmitter device, the code is organized around two main functions: transmission and verification. The transmission function transmission_init() is called from the app_main() function for sending the data to the receiver using ESP-NOW. The verification function ensures that the data has been sent successfully by checking the transmission status using the call-back function. Since the ESP32-C3 microcontroller may be handling multiple tasks simultaneously, the code uses FreeRTOS tasks to allow different operations to run concurrently. This multitasking approach helps maintain responsiveness and efficiency, especially when managing communication alongside other system functions.
On the transmitter device, the code is organized around two main functions: transmission and verification. The transmission function *transmission_init()* is called from the *app_main()* function for sending the data to the receiver using ESP-NOW. The verification function ensures that the data has been sent successfully by checking the transmission status using the call-back function. Since the ESP32-C3 microcontroller may be handling multiple tasks simultaneously, the code uses FreeRTOS tasks to allow different operations to run concurrently. This multitasking approach helps maintain responsiveness and efficiency, especially when managing communication alongside other system functions.
The code block below outlines the function responsible for initializing the ESP-NOW protocol on the transmitter device. Specifically, the transmission_init() function begins by calling esp_now_init() to activate the ESP-NOW feature. If initialization is successful, the program proceeds to register a callback function, statusDataSend, which is triggered after each transmission to verify whether the data was sent correctly. A key part of this setup involves specifying the receiving device using its MAC address, along with other configuration parameters such as the communication channel and encryption settings. These details are then registered using esp_now_add_peer(). Finally, a FreeRTOS task named rc_send_data_task is created to manage the periodic transmission of remote control data. This structured approach ensures that the transmitter is properly configured for reliable communication with the receiver using ESP-NOW.
The code block below outlines the function responsible for initializing the ESP-NOW protocol on the transmitter device. Specifically, the *transmission_init()* function begins by calling esp_now_init() to activate the ESP-NOW feature. If initialization is successful, the program proceeds to register a callback function, statusDataSend, which is triggered after each transmission to verify whether the data was sent correctly. A key part of this setup involves specifying the receiving device using its MAC address, along with other configuration parameters such as the communication channel and encryption settings. These details are then registered using *esp_now_add_peer()*. Finally, a **FreeRTOS** task named rc_send_data_task is created to manage the periodic transmission of remote control data. This structured approach ensures that the transmitter is properly configured for reliable communication with the receiver using ESP-NOW.
``` c
void transmission_init()
@@ -96,7 +96,6 @@ void transmission_init()
devices.channel = 11;
devices.encrypt = false;
esp_now_add_peer(&devices);
// Defince a task for periodically sending ESPNOW remote control data
xTaskCreate(rc_send_data_task, "RC", 2048, NULL, 4, NULL);
}
@@ -119,7 +118,7 @@ static void rc_send_data_task()
Finally, shown below is the sendData() function responsible for preparing and transmitting a structured set of control data from the transmitter device to the receiver using the ESP-NOW protocol. It begins by updating the fields of a data buffer, including joystick coordinates, button states, LED status, and PWM values for four motors. Note that the composition of variables corresponds to the data struct defined earlier.
Before sending the data, the function retrieves and logs the current Wi-Fi channel to ensure the device is operating on the correct frequency. It then calls esp_now_send(), passing the receivers MAC address, a pointer to the data buffer, and the size of the data. If the transmission fails, the function logs detailed error messages, including the error code and the receivers MAC address, and calls deletePeer() to remove the peer configuration. This function plays a central role in the communication process, ensuring that the transmitter sends the data to the receiver while providing feedback in case of transmission issues.
Before sending the data, the function retrieves and logs the current Wi-Fi channel to ensure the device is operating on the correct frequency. It then calls *esp_now_send()*, passing the receivers MAC address, a pointer to the data buffer, and the size of the data. If the transmission fails, the function logs detailed error messages, including the error code and the receivers MAC address, and calls deletePeer() to remove the peer configuration. This function plays a central role in the communication process, ensuring that the transmitter sends the data to the receiver while providing feedback in case of transmission issues.
``` c
static void sendData (void)
@@ -158,14 +157,13 @@ static void sendData (void)
ESP_LOGE(TAG, "Ensure that received MAC is: %02X:%02X:%02X:%02X:%02X:%02X",
receiver_mac[0], receiver_mac[1], receiver_mac[2],
receiver_mac[3], receiver_mac[4], receiver_mac[5]);
deletePeer();
}
}
```
Lastly, the statusDataSend() function serves as a callback that is automatically triggered after each ESP-NOW data transmission. Its primary role is to check whether the data was sent successfully and to provide feedback based on the result. If the transmission is successful, the function logs a confirmation message along with the MAC address of the receiving device. However, in the event of a failure, the function also removes the peer configuration using deletePeer() and restarts the device with esp_restart() to attempt another transmission session. This callback is essential for monitoring the reliability of communication.
Lastly, the statusDataSend() function serves as a callback that is automatically triggered after each ESP-NOW data transmission. Its primary role is to check whether the data was sent successfully and to provide feedback based on the result. If the transmission is successful, the function logs a confirmation message along with the MAC address of the receiving device. However, in the event of a failure, the function also removes the peer configuration using *deletePeer()* and restarts the device with *esp_restart()* to attempt another transmission session. This callback is essential for monitoring the reliability of communication.
``` c
// Callback function to handle the status of data transmission
@@ -192,7 +190,9 @@ static void statusDataSend(const uint8_t *mac_addr, esp_now_send_status_t status
## Receiver
On the receiver device, the code is slightly simpler, as its primary role is to receive and process incoming data. Within the app_main() function, the transmitter device is registered by specifying its MAC address and communication parameters using the esp_now_peer_info_t structure. This includes setting the Wi-Fi interface, communication channel, and encryption settings. Once the peer information is configured, it is added to the ESP-NOW peer list using esp_now_add_peer(). Importantly, a callback function named onDataReceived is registered using esp_now_register_recv_cb(). This function is automatically triggered whenever data is received, allowing the program to store the incoming information into a predefined data structure. This setup ensures that the receiver is properly configured to recognize the transmitter and handle incoming ESP-NOW messages efficiently.
On the receiver device, the code is slightly simpler, as its primary role is to receive and process incoming data. Within the *app_main()* function, the transmitter device is registered by specifying its MAC address and communication parameters using the esp_now_peer_info_t structure. This includes setting the Wi-Fi interface, communication channel, and encryption settings. Once the peer information is configured, it is added to the ESP-NOW peer list using *esp_now_add_peer()*.
Importantly, a callback function named onDataReceived is registered using *esp_now_register_recv_cb()*. This function is automatically triggered whenever data is received, allowing the program to store the incoming information into a predefined data structure. This setup ensures that the receiver is properly configured to recognize the transmitter and handle incoming ESP-NOW messages efficiently.
``` c
esp_now_peer_info_t transmitterInfo = {0};
@@ -205,25 +205,17 @@ ESP_ERROR_CHECK(esp_now_add_peer(&transmitterInfo));
ESP_ERROR_CHECK(esp_now_register_recv_cb((void*)onDataReceived));
```
The onDataReceived() function is designed to handle incoming data on the receiver device in an ESP-NOW communication setup. When data is received, this callback function is automatically triggered. It begins by logging the MAC address of the transmitting device and the length of the received data, which helps verify the source and size of the transmission. The actual data is then copied into a predefined buffer using memcpy(), allowing the receiver to store and later process the information in a structured format. This function plays a key role in ensuring that incoming data is captured accurately and efficiently for further use within the application.
The *onDataReceived()* function is designed to handle incoming data on the receiver device in an ESP-NOW communication setup. When data is received, this callback function is automatically triggered. It begins by logging the MAC address of the transmitting device and the length of the received data, which helps verify the source and size of the transmission. The actual data is then copied into a predefined buffer using *memcpy()*, allowing the receiver to store and later process the information in a structured format. This function plays a key role in ensuring that incoming data is captured accurately and efficiently for further use within the application.
``` c
void onDataReceived (const uint8_t *mac_addr, const uint8_t *data, uint8_t data_len) {
ESP_LOGI(TAG, "Data received from: %02x:%02x:%02x:%02x:%02x:%02x, len=%d", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5], data_len);
memcpy(&buf, data, sizeof(buf));
}
```
# CONCLUSION
In conclusion, establishing ESP-NOW communication between two ESP32 devices requires a consistent and well-structured approach. Both the transmitter and receiver must share identical data structures and initialization routines, including the setup of NVS and Wi-Fi in station mode. On the transmitter side, FreeRTOS tasks manage data preparation and transmission, while callback functions monitor success and handle errors. The receiver, in contrast, focuses on registering the transmitter and storing incoming data through a dedicated callback. This setup ensures efficient, reliable, and direct communication between devices without relying on a traditional Wi-Fi network.
In conclusion, establishing **ESP-NOW** communication between two ESP32 devices requires a consistent and well-structured approach. Both the transmitter and receiver must share identical data structures and initialization routines, including the setup of NVS and Wi-Fi in station mode. On the transmitter side, FreeRTOS tasks manage data preparation and transmission, while callback functions monitor success and handle errors. The receiver, in contrast, focuses on registering the transmitter and storing incoming data through a dedicated callback. This setup ensures efficient, reliable, and direct communication between devices without relying on a traditional Wi-Fi network.
---