I have set up my project to have a main esp32 that controls water valves and some other stuff. I have sensor esp32 that I want to send sensor data to main esp32 which in return sends to a virtual pin in blynk. I can get the main esp32 to receive sensor data through esp-now when I have not added the device to blynk. Once I add it to blynk It says esp-now is active and added a peer but I receive no data from sensor. I believe it has to do with changing of wifi during setup of device in blynk but I am not positive. Here is snippets of my code where i handle esp-now. Any help on where I am going wrong would be awesome.
// Initialize ESP-NOW if it hasn't been initialized yet
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW already initialized or failed to initialize.");
return; // If initialization failed or is already initialized, exit the function
}
// Register ESP-NOW receive callback
esp_now_register_recv_cb(onDataReceive);
// Define the peer's broadcast address
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Set up the peer information
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0; // Use the current Wi-Fi channel
peerInfo.encrypt = false;
// Attempt to add the peer
if (esp_now_add_peer(&peerInfo) == ESP_OK) {
Serial.println("ESP-NOW peer added successfully.");
} else {
// If adding the peer fails, it might already exist or there's another issue.
// You can decide how to handle this in your application.
Serial.println("Failed to add ESP-NOW peer, it might already exist.");
}
}
void setup() {
Serial.begin(115200);
delay(100);
xTaskCreate(
blinkLEDTask, /* Task function */
"BlinkLEDTask", /* Name of the task */
1000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
NULL); /* Task handle (not used) */
xTaskCreate(
buttonTask, /* Task function */
"ButtonTask", /* Name of the task */
1000, /* Stack size in words */
NULL, /* Task input parameter */
2, /* Priority of the task */
NULL); /* Task handle (not used) */
WiFi.mode(WIFI_STA);
BlynkEdgent.begin();
setupRelays();
updateWateringTimes();
pinMode(buttonPin, INPUT_PULLUP);
// Initialize ESP-NOW after Blynk
if (esp_now_init() == ESP_OK) {
Serial.println("ESP-NOW Initialized");
esp_now_register_recv_cb(onDataReceive);
} else {
Serial.println("Error initializing ESP-NOW");
}
}
// ESP-NOW callback function
void onDataReceive(const uint8_t * mac, const uint8_t *incomingData, int len) {
// Print the MAC address of the sender
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.print("Received message from: ");
Serial.println(macStr);
if (len == sizeof(SensorData)) {
SensorData receivedData;
memcpy(&receivedData, incomingData, sizeof(SensorData));
// Print the received message details
Serial.print("Temperature: ");
Serial.print(receivedData.temperature);
Serial.print("°C, Humidity: ");
Serial.print(receivedData.humidity);
Serial.print("%, Soil Moisture: ");
Serial.println(receivedData.soilMoisture);
// Now update the Blynk virtual pins with the received values
Blynk.virtualWrite(V24, receivedData.temperature);
Blynk.virtualWrite(V19, receivedData.humidity);
Blynk.virtualWrite(V18, receivedData.soilMoisture);
} else {
Serial.println("Received data does not match expected structure size.");
}
}```