Transmitting Water Depth using ESP32NOW and Uploading to Arduino IDE

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!

Maybe there is a problem with the "outside unit" hardware, which you forgot to describe.

Keep in mind at a powered PCB will corrode fairly rapidly outdoors, or in any other humid environment.

Sorry I say outdoor unit but I actually haven't used outdoors. They are both nano ESP32 and as mentioned they both work fine when I change the receiver (indoor unit) to work with an LCD display rather than uploading to the Arduino IOT.

The first thing to do if you don't have communication is to go back to the basic ESPNOW example, and make sure that the transmitter and receiver can talk to each other.

It is pointless to ask questions about code, if you have not established that all the hardware is working correctly.

Again they do talk to each other when I use the exact code I am using on the transmitter (outside unit) and a different receiving code for the receiver (inside unit). I just tested it out and they are communicating. This is the receiver code I can switch to right now and it works.

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

// Define the I2C address of LCD module
#define I2C_ADDRESS 0x27  // Change this to the correct address for your module

// Create a LiquidCrystal_I2C object
LiquidCrystal_I2C lcd(I2C_ADDRESS, 16, 2);  // 16x2 character display

// 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;

// 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.print("Bytes received: ");
    Serial.println(len);
    Serial.print("Ultrasonic Distance: ");
    Serial.print(ultrasonicData.distance);
    Serial.println(" cm");
    Serial.println();
    
    // Print the well depth on the LCD
    lcd.clear();  // Clear the LCD display
    lcd.setCursor(0, 0);  // Set the cursor to the first line
    lcd.print("Well Depth: ");
    lcd.setCursor(0, 1);  // Set the cursor to the second line
    lcd.print(ultrasonicData.distance);
    lcd.print(" cm");
  } else {
    Serial.println("Received data size mismatch.");
  }
}

void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);

  // Initialize the I2C bus
  Wire.begin();

  // Initialize the LCD
  lcd.init();  // Initialize the LCD
  lcd.backlight();  // Turn on the backlight

  // 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 recv CB to
  // get received packet info
  esp_now_register_recv_cb(OnDataRecv);
}

void loop() {

Great! Comparing the working and nonworking codes would be a good place to start in determining where the problem might be.

Take a look through your original post and try to see why it is unclear and misleading.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.