ESP8266 running 3 soil moisture sensors?

Does anyone know how to make this sketch run for 3 soil moisture sensors on one board? I’m quite new to this! Also I removed the auth token and internet settings on purpose.


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

const int sensorPin = 15; 
int sensorState = 0;
int lastState = 0;


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "SSID", "Password");
  pinMode(sensorPin, INPUT);
}

void loop()
{ 
  Blynk.run();

  sensorState = digitalRead(sensorPin);
Serial.println(sensorState);

if (sensorState == 1 && lastState == 0) {
  Serial.println("needs water, send notification");
  Blynk.notify("Water your plants");
  lastState = 1;
  delay(1000);
//send notification
    
  } 
  else if (sensorState == 1 && lastState == 1) {
    //do nothing, has not been watered yet
  Serial.println("has not been watered yet");
  delay(1000);
  }
  else {
    //st
    Serial.println("does not need water");
    lastState = 0;
    delay(1000);
  }
  
  delay(100);
}

I’ve tried but I keep getting that one sensor is already receiving moisture even though its not. And It’s not a sensor problem either, since I switched them out and I get the same problem.

I would first just get one working by itself. That is, without BLYNK. Then read through the DOCS and see what is needed to convert a sketch into something that is BLYNK compatible (HINT: use timers, no delay, and only two lines in the loop()). Once you have one sensor working with BLYNK, I suspect getting the other two to work will be a breeze.

A good starting point for an example sketch would be the PUSH DATA example, from the SKETCH BUILDER.

I’ve got one working with BLYNK at the moment, I’m just lost at how to put 3, since they seem to conflict.

if you are using the sketch you posted above, then I would say that you need to look at the example provided, and re-structure your code so that it uses no delays, and has only two lines of code in the loop(). You will need to use timers.

Well, before you start adding any more sensors, you need to re-structure this code so that it’s Blynk friendly. This means:

  • Removing everything from your void loop except Blynk.run and Timer.run (the Timer.run will become clear later

  • Removing all of the delays from your code

  • Putting the code you’ve removed from void loop into a function that’s called by a timer (hence the Timer.run in void loop)

You really don’t need to be checking your soil moisture level every second or two - pick a sensible frequency at which your timer calls the moisture testing processs.

You should look at the logic of your if then else statements, and use sensible variable names like ‘Notification_Sent_Flag’.

Your additional sensors will need to be attached to their own GPIO pins, and make sure that you choose ‘safe’ pins for this. See this post for more info:

You will also need additional functions to check each of the sensors, but you’ll need to be sensible about how you handle your notifications. You can only send one every 15 seconds, and you should really have one that tells you the status of each zone that your email monitoring.

Don’t make the mistake of calling all three timers that monitor the various zones at the same time. Have them on different timing schedules, or call them one after another.

Pete.