Hey everyone, I am trying to send data from outside using one ESP32 outside and one inside. The one inside connects to the internet and receives the data from the one outside VIA ESP32NOW. The one inside then sends the data to the Arduino IOT where it is displayed. I have first tested this just using an LCD on the inside unit and it works great. Now comes the frustrating part, about a month ago (last time I worked on it) it was all working and displaying to the IOT dashboard but now for some reason the same value is being displayed over and over on the dashboard and based off the debugging info it seems as though the inside unit (one connected to IOT) is not receiving the data from the outside unit. Here is the code for the inside unit.
#include "arduino_secrets.h"
#include "thingProperties.h"
#include <esp_now.h>
#include <WiFi.h>
// Structure to receive ultrasonic data
typedef struct struct_ultrasonic_data {
float distance;
} struct_ultrasonic_data;
// Create a struct_ultrasonic_data called ultrasonicData
struct_ultrasonic_data ultrasonicData;
// Wi-Fi and ESP-NOW settings
const char* ssid = "Left Out";
const char* password = "Left Out";
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
if (len == sizeof(ultrasonicData)) {
memcpy(&ultrasonicData, incomingData, sizeof(ultrasonicData));
Serial.println("Received data from ESP-NOW:");
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Ultrasonic Distance: ");
Serial.print(ultrasonicData.distance);
Serial.println(" cm");
Serial.println();
// Update the waterDepth variable in Arduino IoT Cloud
waterDepth = ultrasonicData.distance;
} else {
Serial.println("Received data size mismatch.");
}
}
void onWaterDepthChange() {
// Your implementation goes here
// For example, you can print a message to Serial Monitor
Serial.println("Water depth changed!");
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for Wi-Fi connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Initialize Arduino IoT Cloud
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(3);
ArduinoCloud.printDebugInfo();
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully initialized, register for recv CB to
// get received packet info
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
// Update Arduino IoT Cloud variables and handle communication
ArduinoCloud.update();
// Add a delay to slow down the loop
delay(1000);
}
Here is the code for the outside unit however I dont think there is an issue with this code because this exact code works perfectly with another receiver code that I set up to work with an LCD.
#include <esp_now.h>
#include <WiFi.h>
#include <SoftwareSerial.h>
uint8_t broadcastAddress[] = {Left Out};
// Structure for sending ultrasonic data
typedef struct struct_ultrasonic_data {
float distance;
} struct_ultrasonic_data;
struct_ultrasonic_data ultrasonicData;
esp_now_peer_info_t peerInfo;
SoftwareSerial mySerial(11, 10); // RX, TX
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;
}
// Once ESPNow is successfully initialized, register for Send CB to
// get the status of transmitted packets
esp_now_register_send_cb(OnDataSent);
// Register peer
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;
}
mySerial.begin(9600);
}
void loop() {
unsigned char data[4] = {};
do {
for (int i = 0; i < 4; i++) {
data[i] = mySerial.read();
}
} while (mySerial.read() == 0xff);
mySerial.flush();
if (data[0] == 0xff) {
int sum;
sum = (data[0] + data[1] + data[2]) & 0x00FF;
if (sum == data[3]) {
float distance = (data[1] << 8) + data[2];
// Send ultrasonic data via ESP-NOW
ultrasonicData.distance = distance / 10.0; // Convert to cm
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&ultrasonicData, sizeof(ultrasonicData));
if (result == ESP_OK) {
Serial.println("Sent ultrasonic data with success");
} else {
Serial.println("Error sending ultrasonic data");
}
} else {
Serial.println("ERROR");
}
}
delay(150);
}
// 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");
}
Any help would be appreciated!