[SOLVED] How do i use ESP NOW with ESP32 and a soil moisture sensor

I want to use my ESP32 DEVKITC V4 with my soil moisture sensor this one and ESP NOW.

So my goal is to have multiple ESP32 modules with a soil moisture senor that sends data to one ESP32 module using ESP NOW where i can monitor the data.

Here is the circuit diagram:

And here is the code for the sensor, which works fine:

int moisture, sensor_analog;
const int sensor_pin = A0; /* Soil moisture sensor O/P pin */

void setup(void) {
  Serial.begin(115200); /* Set the baudrate to 115200*/
}

void loop(void) {
  sensor_analog = analogRead(sensor_pin);
  moisture = (100 - ((sensor_analog / 4095.00) * 100));
  Serial.print("Moisture = ");
  Serial.print(moisture); /* Print Temperature on the serial window */
  Serial.println("%");
  delay(1000); /* Wait for 1000mS */
}

And here is the code that i tried using but the sender keeps restarting when i plug my sensor in:

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-many-to-one-esp32/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

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

int sensor_analog;
const int sensor_pin = A0; /* Soil moisture sensor O/P pin */

// REPLACE WITH THE RECEIVER'S MAC Address
uint8_t broadcastAddress[] = {0x34, 0x85, 0x18, 0x8C, 0xB6, 0x0C};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
    int id; // must be unique for each sender board
    int moisture;
    String text;
} struct_message;

// Create a struct_message called readings
struct_message readings;

// Create peer interface
esp_now_peer_info_t peerInfo;

// 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");
}
 
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 Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  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;
  }
}
 
void loop() {
  // Set values to send
  sensor_analog = analogRead(sensor_pin);
  readings.id = 1;
  readings.moisture = (100 - ((sensor_analog / 4095.00) * 100));
  if(readings.moisture < 35) {
    readings.text = "The plant needs to be watered!";
  }

  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &readings, sizeof(readings));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(1000);
}

And here is the code for the receiver, which works fine:

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-many-to-one-esp32/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

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

// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
  int id;  // must be unique for each sender board
  int moisture;
  String text;
} struct_message;

// Create a struct_message called readings
struct_message readings;

// Create a structure to hold the readings from each board
struct_message board1;
struct_message board2;
struct_message board3;

// Create an array with all the structures
struct_message boardsStruct[3] = { board1, board2, board3 };

// callback function that will be executed when data is received
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len) {
  char macStr[18];
  Serial.print("Packet received from: ");
  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.println(macStr);
  memcpy(&readings, incomingData, sizeof(readings));
  Serial.printf("Board ID %u: %u bytes\n", readings.id, len);
  // Update the structures with the new incoming data
  boardsStruct[readings.id - 1].moisture = readings.moisture;
  boardsStruct[readings.id - 1].text = readings.text;
  Serial.printf("Moisture level: %d% \n", boardsStruct[readings.id - 1].moisture);
  Serial.printf("Message: %s \n", boardsStruct[readings.id - 1].text);
  Serial.println();
}

void setup() {
  //Initialize 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 Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);
}

void loop() {
  // Acess the variables for each board
  /*int board1X = boardsStruct[0].x;
  int board1Y = boardsStruct[0].y;
  int board2X = boardsStruct[1].x;
  int board2Y = boardsStruct[1].y;
  int board3X = boardsStruct[2].x;
  int board3Y = boardsStruct[2].y;*/

  delay(10000);
}

Any help will be appreciated. Thanks :heart:.

Wiring problem.

You should never change wiring when the circuitry is powered.

Ok, but it does the same thing when i have the sensor plugged in and then plug in the USB cable.

Wiring problem. You have a short circuit or other serious mistake somewhere.

Ok, but where...

There is no information in your post which could allow anyone to guess which mistake you made. Take all the wiring apart and redo it, double checking every connection.

Post a clear, focused photo of your setup.

Wait i just plugged the USB cable in and it doesn't reboot and it works but the receiver gets the moisture value 100 and i don't have the sensor in a plant.

EDIT: I got it to work but the part where i do readings.text = "The plant needs to be watered!"; doesn't work, instead the message i get is: Message: �~�? and i don't know why.

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