i have to programs running on two esp32 microcontrollers. I am sending with the first…
- User
i have to programs running on two esp32 microcontrollers. I am sending with the first code data to the second esp32 that runs the second code. The dataBuffer on the second code seems to be empty, what can cause the empty value? ESP32 1 code 1: #include <Wire.h> #include <esp_now.h> #include <WiFi.h> #include <esp_wifi.h>
#define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define BUFFER_SIZE 1024
#define I2C_DEV_ADDR 0x3C #define CHANNEL 1
uint8_t dataBuffer[BUFFER_SIZE]; unsigned int bufferHead = 0; unsigned int bufferTail = 0; unsigned int currentDataSize = 0; // Current size of data received
//uint8_t dataArray[MAX_POSSIBLE_DATA_SIZE]; //uint8_t currentDataSize = 0; // Current size of data received
esp_now_peer_info_t slave; bool isSlavePaired = false; // Track if slave is currently paired
void onReceive(int len) { currentDataSize = len; for (int i = 0; i < len; i++) { addToBuffer(Wire.read()); } sendData();/* for (int i = 0; i < BUFFER_SIZE; i++) { Serial.print(dataBuffer[i], HEX); Serial.print(" "); if ((i + 1) % 16 == 0) { // Print 16 values per line Serial.println(); } }*/ }
void addToBuffer(uint8_t data) { bufferHead = (bufferHead + 1) % BUFFER_SIZE; // Wrap around if buffer is full dataBuffer[bufferHead] = data; if (bufferHead == bufferTail) { bufferTail = (bufferTail + 1) % BUFFER_SIZE; // Remove oldest data when buffer is full } }
void sendData() { const uint8_t* peer_addr = slave.peer_addr; esp_err_t result = esp_now_send(peer_addr, dataBuffer + bufferTail, currentDataSize);
if (result == ESP_OK) { Serial.println("Success"); } else { Serial.println("Send Error"); } Serial.print("Data send:"); Serial.println(); for (int i = 0; i < BUFFER_SIZE; i++) { Serial.print(dataBuffer[i], HEX); Serial.print(" "); if ((i + 1) % 16 == 0) { // Print 16 values per line Serial.println(); } } }
void InitESPNow() { WiFi.disconnect();
for(int retries = 0; retries < 3; retries++) { if (esp_now_init() == ESP_OK) { Serial.println("ESPNow Init Success"); break; } else { if(retries == 2) { Serial.println("ESPNow Init Failed 3 times, restarting..."); delay(1000); ESP.restart(); } delay(500); } }}
bool manageSlave() { if (slave.channel == CHANNEL) { bool exists = esp_now_is_peer_exist(slave.peer_addr); if (exists) { Serial.println("Already Paired"); return true; } else { esp_err_t addStatus = esp_now_add_peer(&slave); if (addStatus == ESP_OK) { Serial.println("Pair success"); return true; } else { Serial.println("Pairing Error"); } } } else { Serial.println("No Slave found to process"); } return false; }
void OnDataSent(const uint8_t* mac_addr, esp_now_send_status_t status) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Last Packet Sent to: "); Serial.println(macStr); Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); }
- Assistant
There could be several reasons why the
dataBufferon the second ESP32 microcontroller is empty:-
Verify that the data is actually being sent from the first ESP32. Check the transmit code to ensure that data is being sent correctly.
-
Make sure that the second ESP32 is successfully paired with the first ESP32. Check that the
isSlavePairedvariable is set to true in the second ESP32 code. -
Confirm that the
OnDataRecvfunction is being called correctly. Put a debug print statement in the function to check if it is being triggered when data is received. -
Double-check the buffer indices and wrapping logic. Ensure that the buffer indices
bufferHeadandbufferTailare being updated correctly and that the data is being written to the buffer properly. -
Check the connection between the second ESP32 and the display. Make sure that the display is properly initialized and that the wire connections (SDA, SCL) are correct.
By inspecting and debugging these areas, you should be able to identify the cause of the empty
dataBufferon the second ESP32. -
- User
the data of the following snippit seems always to be : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 , what am i doing wrong void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Data Received from :"); Serial.println(macStr); Serial.println(); bufferHead = (bufferHead + 1) % BUFFER_SIZE; dataBuffer[bufferHead] = *data; if (bufferHead == bufferTail) { bufferTail = (bufferTail + 1) % BUFFER_SIZE;
}Serial.print("Databuffer on data receive."); Serial.println(); for (int i = 0; i < BUFFER_SIZE; i++) { Serial.print(dataBuffer[i], HEX); Serial.print(" "); if ((i + 1) % 16 == 0) { // Print 16 values per line Serial.println(); } } Wire.beginTransmission(SCREEN_ADDRESS); for (int i = 0; i < data_len; i++) { Wire.write(dataBuffer[i]); } Wire.endTransmission();}
- Assistant
The issue seems to be with the line
dataBuffer[bufferHead] = *data;. Here, you are assigning the value ofdata(which is a pointer) todataBuffer[bufferHead]. Instead, you should be dereferencing the pointer and assigning the actual value todataBuffer[bufferHead]. You can do this by modifying the line todataBuffer[bufferHead] = *data[i];:void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Data Received from :"); Serial.println(macStr); Serial.println(); bufferHead = (bufferHead + 1) % BUFFER_SIZE; dataBuffer[bufferHead] = *data[i]; if (bufferHead == bufferTail) { bufferTail = (bufferTail + 1) % BUFFER_SIZE; } Serial.print("Databuffer on data receive."); Serial.println(); for (int i = 0; i < BUFFER_SIZE; i++) { Serial.print(dataBuffer[i], HEX); Serial.print(" "); if ((i + 1) % 16 == 0) { // Print 16 values per line Serial.println(); } } Wire.beginTransmission(SCREEN_ADDRESS); for (int i = 0; i < data_len; i++) { Wire.write(dataBuffer[i]); } Wire.endTransmission(); } - User
Compilation error: 'i' was not declared in this scope
- Assistant
Apologies for the mistake. It seems that there is a naming conflict between the variable
iused in theforloop and theiused inside thedataBufferassignment. To resolve this, you can change the name of the innerivariable to a different name, such asj. Here's the modified code:void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Data Received from :"); Serial.println(macStr); Serial.println(); bufferHead = (bufferHead + 1) % BUFFER_SIZE; dataBuffer[bufferHead] = *data; if (bufferHead == bufferTail) { bufferTail = (bufferTail + 1) % BUFFER_SIZE; } Serial.print("Databuffer on data receive."); Serial.println(); for (int j = 0; j < BUFFER_SIZE; j++) { Serial.print(dataBuffer[j], HEX); Serial.print(" "); if ((j + 1) % 16 == 0) { // Print 16 values per line Serial.println(); } } Wire.beginTransmission(SCREEN_ADDRESS); for (int j = 0; j < data_len; j++) { Wire.write(dataBuffer[j]); } Wire.endTransmission(); } - User
Data send: 0 0 B0 10 1 40 8 18 30 60 C0 80 0 B1 10 1 Data received: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- Assistant
It seems that the issue lies in the transmission and reception of data between the two ESP32 microcontrollers. Here are a few things to check:
-
Verify that the
OnDataRecvfunction is being called correctly when data is received. You can add debug print statements to check if it is triggered and ifdata_lenhas the correct value. -
Confirm that the data is being sent from the first ESP32 with the correct length and content. Double-check the data being sent in the
sendDatafunction of the first ESP32 code. -
Check the connection and communication between the two ESP32 microcontrollers. Ensure that they are using the same channel and have successfully paired.
-
Verify the transmission and reception of data on both sides. Check the
esp_now_sendandesp_now_recvfunctions to ensure they are being used correctly.
By reviewing these aspects and ensuring that the transmission and reception of data are properly implemented, you should be able to resolve the issue and receive the expected data on the second ESP32 microcontroller.
-
- Assistant
Apologies for the confusion. Upon reviewing your code again, I noticed the issue:
In the
OnDataRecvfunction of the second ESP32 code, you are assigning the received data incorrectly todataBuffer[bufferHead]. Instead of*data, you should access the individual elements of thedataarray using the index. Modify the assignment line todataBuffer[bufferHead] = data[i];:void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Data Received from : "); Serial.println(macStr); Serial.println(); bufferHead = (bufferHead + 1) % BUFFER_SIZE; dataBuffer[bufferHead] = data[i]; if (bufferHead == bufferTail) { bufferTail = (bufferTail + 1) % BUFFER_SIZE; } Serial.println("Databuffer on data receive:"); for (int i = 0; i < BUFFER_SIZE; i++) { Serial.print(dataBuffer[i], HEX); Serial.print(" "); if ((i + 1) % 16 == 0) { Serial.println(); } } Wire.beginTransmission(SCREEN_ADDRESS); for (int i = 0; i < data_len; i++) { Wire.write(dataBuffer[i]); } Wire.endTransmission(); }