ESP32 and Annoying Soil Moisture Sensor

j4e8a16n
Posts: 9
Joined: Mon Nov 18, 2019 5:55 pm

ESP32 and Annoying Soil Moisture Sensor

Postby j4e8a16n » Thu Nov 21, 2019 9:46 pm

Hi,

Here is the sensor https://www.dfrobot.com/blog-735.html


It works perfectly with Arduino boards.

With ESP32: I tried, sensor pins, ADC pins, I2C pins (one at a time) without success.

Code: Select all

/*CALIBRATION CODE
   -------------------------------
    pot  10cm * 4^2 * pi = 500ml
  +20  30ml  316
  +10  40ml  216
  +10  50ml  148
  +10  60ml  102
  +10  70ml   69
  +10  80ml   47
  +10  90ml   37
  +10  100ml  22
  +10  110ml  15  30oct
  +10  120ml  10
  +10  130ml   7
  +10  140ml   5

*/
//connect sensor to Analog A0
byte pinToRead = 34;    //input
// change with your threshold value
const int threshold = 20;

float store [100];
float  j = 10.0;
uint8_t increase = 0, data = 0;
uint8_t mean = 0;
uint8_t meanVal, val = 0, once = 0;
float  previousVal = 0;
char buff[7] = "";
String ww = "A string : ";

int fraction(float input) {
  if ( (input - (int)input)  > 0.5) {
    input += 1;
  }
  return ((int)input);
}

void  showThem() {
  float total = 0, doTheTrick = 0;
  String ww = "";
  for (int i = 0; i < j; i++) {
    Serial.print("store[i] ");
    Serial.print(i);
    Serial.print(" = ");
    Serial.println(store[i]);
    total += store[i];
  }
  Serial.print("      total : ");
  Serial.println(total);
  Serial.print("    moyenne : ");
  doTheTrick = fraction((total / j));
  Serial.println(doTheTrick);
  sprintf(buff, "Buff Moyenne = %d", (int)doTheTrick);
  Serial.println();
  Serial.println(buff);



  while (1) {}
}

void setup() {
  Serial.begin(115200); // or open serial port, set the baud rate as 9600 bps
  //vroom upload speed 921600
}
void loop() {
  //---------------------------------
  if (once < 1) {
    delay(6E2);//lets the device warm up after opening it
    once = 1;
    Serial.print("once done  ");
    Serial.println(6E5);
    Serial.println();
  }
  val = touchRead(pinToRead); //for esp32 vroom   carte Node32s upload speed 921600
  // val = analogRead(pinToRead); //connect sensor to Analog 11 or else
  delay(2000);// Sensor readings may also be up to 2 secs
  ////////////////////  esp32  begin
  if (val > threshold) {
    // turn LED on
    //digitalWrite(ledPin, HIGH);
    Serial.println(val > threshold);
  }
  else {
    // turn LED off
    // digitalWrite(ledPin, LOW);
    Serial.println("val < threshold");
  }
  /////////////////// esp32 end
  if (isnan(val)) {
    Serial.println("Failed to read from DHT");
  }
  Serial.print("data ");
  Serial.print(data);
  Serial.print(" : ");
  Serial.println(val);  //wake up
  if (data == j) {
    Serial.println();
    showThem();
    Serial.println();
  }

  store[data] = val;
  data++;
}
Somebody has a clue ?

Regards

JPDaviau
Arduino 1.8.10; Windows 10; ESP32 VROOM

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: ESP32 and Annoying Soil Moisture Sensor

Postby ESP_Angus » Thu Nov 21, 2019 11:15 pm

Moderator's note: Moved to Arduino subforum.

JPDaviau, it's not really clear from your post what the problem you're seeing is.

One thing is that your DFRobot sensor has an analog output, so you definitely want to hook this up to an ADC input pin and read it with analogRead(). Trying to treat it as a capacitive touch channel (touchRead) or anything else will definitely not work.

Suggest you change the code to use analogRead() then reply with the exact code you're running, the exact output you see, and explain the difference between the output you see and the output you expect. A photo of the hardware setup might also be useful. Then someone can probably help you further.

Angus

Haldi4803
Posts: 9
Joined: Thu Nov 21, 2019 1:09 am

Re: ESP32 and Annoying Soil Moisture Sensor

Postby Haldi4803 » Sat Nov 23, 2019 4:45 pm

Mine works pretty fine with a Lolin D32 and this code:

you can omit the UDP send part and activate Serial instead for debugging.

Code: Select all

#include "WiFi.h"
#include "AsyncUDP.h"

const char * ssid = "***";
const char * password = "***";

AsyncUDP udp;
float asoilmoist=analogRead(32);//global variable to store exponential smoothed soil moisture reading
float avbat=analogRead(35)/4096.0*7.445;//global variable to store Battery Voltage
#define uS_TO_S_FACTOR 1000000ULL  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 600         /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int battCount = 0; //Save BatteryCounter in RTC Memory not wiping it in Sleep


void setup()
{
    //Serial.begin(115200); //Serial Print Deactivated if not needed

    if (avbat < 3.3)  //BATTERY Low Voltage Check
    {
      while (battCount < 5) { //Check Z Times within normal Interval if Battery is Below Y
      battCount++;
      //Serial.println((String)"Batterie unter 3,3V"); //Debug Only
      //Serial.println((String)"Bat="+avbat); //Debug Only  
      //Serial.println((String)"BattCount="+battCount); //Debug Only
      esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //Go to Sleep for Time X  
      esp_deep_sleep_start(); //GOING into sleep!     
      }
    esp_deep_sleep_start(); //GOING into sleep without Timer, Never waking up!   
    }
    
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
        //Serial.println("WiFi Failed");
        while(1) {
            delay(1000);
        }
    }
    if(udp.connect(IPAddress(192,168,1,2), 8089)) //{//Open UDP to Target
    //Serial.println("UDP connected");}// Debug Only
    delay(50); //Needed cause sometimes no Delay = can't send UDP packages fast enough
    //Serial.println((String)"Pflanze1 Erdfeuchte="+asoilmoist); //Debug Only
    //Serial.println((String)"Pflanze1 Batterie="+avbat); //Debug Only
    udp.print((String)"Pflanze1 Erdfeuchte="+asoilmoist);
    udp.print((String)"Pflanze1 Batterie="+avbat);    
    delay(50); //Not really sure if needed....
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //Go to Sleep for Time X  
    esp_deep_sleep_start(); //GOING into sleep!   
    
}

void loop()
{

}
Just make sure you select one pin which is green/marked as ADC:
Image

j4e8a16n
Posts: 9
Joined: Mon Nov 18, 2019 5:55 pm

Re: ESP32 and Annoying Soil Moisture Sensor

Postby j4e8a16n » Sun Nov 24, 2019 5:58 pm

20191124_120148.jpg
20191124_120148.jpg (521.1 KiB) Viewed 9412 times
  1.  
  2. I was expecting more consistent readings
  3.    -------------------------------
  4.   ESP32 VROOM
  5.  
  6.   5% of 442 ml   = 22 ml    80 (sec)
  7.   10% of 442 ml  = 44 ml    248
  8.   12.5% of 442 ml  = 54 ml  178
  9.   15%  of 442 ml  = 64 ml   98,102,116, 129,140,142, 151
  10.  
  11. */
  12.  
  13. #include <EEPROM.h>
  14.  
  15. //connect sensor to Analog A0
  16. byte pinToRead = 39;    //input
  17. // change with your threshold value
  18. const int threshold = 10;
  19.  
  20. float store [100];
  21. float  j = 20.0;
  22. uint8_t increase = 0, data = 0;
  23. uint8_t mean = 0;
  24. uint8_t meanVal, val = 0, once = 0;
  25. float  previousVal = 0;
  26. char buff[7] = "";
  27. String ww = "A string : ";
  28.  
  29.  
  30. void setup() {
  31.   Serial.begin(115200); // or open serial port, set the baud rate as 9600 bps
  32.   //vroom upload speed 921600
  33. }
  34.  
  35. int fraction(float input) {
  36.   if ( (input - (int)input)  > 0.5) {
  37.     input += 1;
  38.   }
  39.   return ((int)input);
  40. }
  41.  
  42. void  showThem() {
  43.   float total = 0, doTheTrick = 0;
  44.   String ww = "";
  45.   for (int i = 0; i < j; i++) {
  46.     Serial.print("store[i] ");
  47.     Serial.print(i);
  48.     Serial.print(" = ");
  49.     Serial.println(store[i]);
  50.     total += store[i];
  51.   }
  52.   Serial.print("      total : ");
  53.   Serial.println(total);
  54.   Serial.print("    moyenne : ");
  55.   doTheTrick = fraction((total / j));
  56.   Serial.println(doTheTrick);
  57.   sprintf(buff, "Buff Moyenne = %d", (int)doTheTrick);
  58.   Serial.println();
  59.   Serial.println(buff);
  60.  
  61.   while (1) {}
  62. }
  63.  
  64.  
  65. void loop() {
  66.   //---------------------------------
  67.   if (once < 1) {
  68.     delay(6E2);//lets the device warm up after opening it
  69.     once = 1;
  70.     Serial.print("once done  ");
  71.     Serial.println(6E5);
  72.     Serial.println();
  73.   }
  74.  
  75.   val = analogRead(pinToRead);
  76.   //////////////////////////////////
  77.   delay(3000);// Sensor readings may also be up to 2 secs
  78.   ////////////////////  esp32  begin
  79.   if (val > threshold) {
  80.     // turn LED on
  81.     //digitalWrite(ledPin, HIGH);
  82.     Serial.println(val > threshold);
  83.   }
  84.   else {
  85.     // turn LED off
  86.     // digitalWrite(ledPin, LOW);
  87.     Serial.println("val < threshold");
  88.   }
  89.   /////////////////// esp32 end
  90.   if (isnan(val)) {
  91.     Serial.println("Failed to read from DHT");
  92.   }
  93.   Serial.print("data ");
  94.   Serial.print(data);
  95.   Serial.print(" : ");
  96.   Serial.println(val);  //wake up
  97.   if (data == j) {
  98.     Serial.println();
  99.     showThem();
  100.     Serial.println();
  101.   }
  102.  
  103.   store[data] = val;
  104.   data++;
  105. }
  106.  
Ref: https://www.amazon.com/gp/product/B07H3 ... 4ab2051d54
I am trying to read the mean value of readings spaced by 3 millis() interval.
https://www.youtube.com/watch?v=pdGRs7GXBeE

Here is what I got.

Buff Mean = 129 //previous means with the same % of water: 98,102,116, 129,140,142, 151



/*CALIBRATION CODE

OUTPUT

store 0 = 115.00
store 1 = 111.00
store 2 = 117.00
store 3 = 227.00
store 4 = 117.00
store 5 = 114.00
store 6 = 111.00
store 7 = 117.00
store 8 = 123.00
store 9 = 119.00
store[i] 10 = 127.00
store[i] 11 = 119.00
store[i] 12 = 115.00
store[i] 13 = 115.00
store[i] 14 = 117.00
store[i] 15 = 123.00
store[i] 16 = 123.00
store[i] 17 = 127.00
store[i] 18 = 118.00
store[i] 19 = 223.00
total : 2578.00
moyenne : 129.00

Buff Mean = 129 //previous means with the same 15% of water: 98,102,116, 129,140,142, 151

I was expecting more consistent readings



On the other hand the DHT11 readings are ok.(not shown here)


Regards,


JPDaviau
Arduino 1.8.10; Windows 10; ESP32 VROOM

j4e8a16n
Posts: 9
Joined: Mon Nov 18, 2019 5:55 pm

Re: ESP32 and Annoying Soil Moisture Sensor

Postby j4e8a16n » Mon Nov 25, 2019 6:03 pm

Haldi4803 wrote:
Sat Nov 23, 2019 4:45 pm

you can omit the UDP send part and activate Serial instead for debugging.

if(udp.connect(IPAddress(192,168,1,2), 8089)) //{//Open UDP to Target
[/code]

I had problem finding the //{ error


>Just make sure you select one pin which is green/marked as ADC:
It is. Pin 39 is one of them.

What was your readings ?
Here, they go from 1929 (damp), 2993(dry)

Another thing, as it is capacitive I doubt the readings can be mapped (map function) to a linear % gradation.

https://www.youtube.com/watch?v=pdGRs7GXBeE

I am joining a file with the formula and the curve of some tests I mage with a Mega 2560 board.

Regards

JPDaviau
Attachments
echelle expo.png
echelle expo.png (59.09 KiB) Viewed 9192 times
Arduino 1.8.10; Windows 10; ESP32 VROOM

Who is online

Users browsing this forum: No registered users and 122 guests