ESP32 and Annoying Soil Moisture Sensor
Posted: 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.
Somebody has a clue ?
Regards
JPDaviau
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++;
}
Regards
JPDaviau