Soil moisture sensor readings on Blynk

The main problem is that as of now for dry conditions, the readings are 1024 which is very high but it should be somewhere between 300 to 600. Is there any way I can do that?

I dont know what your if-else statements really do.

But it seem to me your:

  • function sendTemps could be called by time every 10 or 60 secs instead of being called from loop().
  • The if-else statements in the loop could PROBABLY be part of the sendTemps function. (but i havent really analyzed what they are for .)

In general your loop could be like this:

void loop()
{
  Blynk.run();
  timer.run();
}

and then you have all your sensor reading in functions which you call using a timer.
Once you have understood this concept - it is much simpler and easier to maintain.

Other thing
In your last code examples you read wet-sensor from A0 which I think will return a value 0-1023.
You then send this to blynk V2 which seems to be set 0-100.
If the wet sensor normally returns 200-400 or 800-1000 I guess that will mean the blynk V2 always will display 100.
You will need to map the 0-1023 to 0-100.
(In a previous code example you did this in a bad way causing a “round-off” or “cut-off” by using an integer so it would most often return the value 0. If I’m not wrong.)

1 Like

Another quite boring answer.

I suggest you run your moisture sensor a bit to test its functionality.

  • Let it be dry, read every 1 mins and display the value (A0) in a serial monitor.
  • Let if be wet -"-
  • Put it in dry soil -"-
  • Put it in wet/humid soil -"-

Repeat this test for a few hours and probably also next day and next day.

=>
If you now feel the moisture sensor is working as expected - THEN you continue to make it work with BLYNK.

It seems to me you are as well struggling to understand if the wet/humid sensors really works as well as trying to get the data to blynk.

1 Like

Yeah, It makes sense, Jonas. Thank you so much for your explanation. The if-else statement is simply for a notification on the application which will be triggered if moisture is detected.

Yeah :sweat_smile: it is a bit of a struggle really. I have made several projects with python and JS(software projects) but since I’m doing Arduino now, I have a bit of difficulty with cpp. But the sensor’s working pretty well now cuz I have been testing it for the past 3-4 days.

Okay, now my void loop is clean. I just have to get the value on a scale of 0-100 and for wet it should be more and for dry it should be less. Just help me with this one guys.
New code -

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "4pd1tQr1dAilNauRpvQQR0--wfNMaoDW";
char ssid[] = "KUMAR";
char pass[] = "SRAVENKY@861614";

#define sensorPin D3
int sensorState = 0;
int lastState = 0;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
  
}
void setup()
{
  Blynk.begin(auth, ssid, pass);
  pinMode(sensorPin, INPUT);
  dht.begin();

  timer.setInterval(10000L, sendSensor);
  Serial.begin(115200);
  sensors.begin();
}
int sensor = A0;
void sendTemps()
{
  sensor = analogRead(A0);
  sensors.requestTemperatures();
  float temp = sensors.getTempCByIndex(0);
  Serial.println(temp);
  Serial.println(sensor);
  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, sensor);
  timer.setInterval(10000L, sendSensor);
}
void loop()
{
  Blynk.run();
  timer.run();
}

in function sendTemps(), REMOVE the line:

  timer.setInterval(10000L, sendSensor);

in setup(), ADD the line:

  timer.setInterval(10000L, sendTemps);

REMOVE the line:

int sensor = A0;

Replace it with these MODIFIED line in the sendTemps function:

 int sensor = analogRead(A0);

To map A0 reading to a “percentage” value I BELIEVE you could do like this (I have not tested):

int blynkVal = map (sensor, 1023,0, 0,100);    // map(value, fromLow, fromHigh, toLow, toHigh)
Blynk.virtualWrite(V2, blynkVal);

Note, I am not used to this map function, so I cannot guarantee it works as I believe when “inverse-mapping” like this.

1 Like

It WORKED flamboyantly. Thank you so much Jonas for your time, effort, and expertise :hugs:
You just made my day !

TBH, you’d be far better having one function which takes a reading from your DHT sensor and your analog pin and send the values to Blynk.

You need to use a Map command in your sketch, but I don’t think you understand the Map command syntax, which is…

map(value, fromLow, fromHigh, toLow, toHigh)

Your original sketch used…

This means that an input of 0 would produce an output of 1023, and an input of 1023 would produce an output of 0 (thereby inverting the readings but not re-scaling them)

You actually want…
sensor = map (sensor,0,1023,0,100);

Pete.

1 Like

Thank you, Pete :hugs:

Hi,
I am working on an automatic soil watering system using blynk. I’ve made the app UI and have added a button that is connected to a 5v relay module which switches on and switches off the motor pump with the button. To make this more automatic I was thinking of adding an if-else statement that switches on and off the pump if the moisture content is low and if the conditions are dry and will switch it off if there is enough moisture. The parameter can be set as to when it should start automatically and when to stop. Here’s my code -

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "4pd1tQr1dAilNauRpvQQR0--wfNMaoDW";
char ssid[] = "KUMAR";
char pass[] = "SRAVENKY@861614";

#define sensorPin D3
int sensorState = 0;
int lastState = 0;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V5, h);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
  
}
void setup()
{
  Blynk.begin(auth, ssid, pass);
  pinMode(sensorPin, INPUT);
  dht.begin();

  timer.setInterval(10000L, sendSensor);
  Serial.begin(115200);
  sensors.begin();
  timer.setInterval(10000L, sendTemps);
}

void sendTemps()
{
  int sensor = analogRead(A0);
  int blynkVal = map (sensor, 1023,0, 0,100);
  Blynk.virtualWrite(V2, blynkVal);  
  sensors.requestTemperatures();
  float temp = sensors.getTempCByIndex(0);
  Serial.println(temp);
  Serial.println(sensor);
  Blynk.virtualWrite(V1, temp);

}
void loop()
{
  Blynk.run();
  timer.run();
}

Is there any way I can achieve this task?
Your help will be greatly appreciated.

I’ve merged your new topic back into this one.

The latest code you posted has number of issues, which have already been discussed in this topic, and which you need to address before you go any further.

Please don’t create new topics to discuss what is basically the same issue.

Pete.

Ok, sure.

You can achieve this in the sketch using if condition, and you can add a setpoint and change when to start/stop using a slider for example without modifying the sketch and reupload it, or you can use Automation which is the easiest way.

He’s using Legacy :slightly_frowning_face:

Pete.

Okay, sorry :sweat_smile:
Just ignore automation.
You can use eventor which is similar to automation

Koushai

One comment re your idea to switch motor on (WATER ON) when sensor is DRY and WATER OFF when sensor is WET.

There is a risk this will often/always end up in flooding (water all over the place…)
Why?
It depends, of course, on how much water you pump per time, i.e. the water flow. There is always a risk your water flow is “more” than your sensor system can detect - and if so - you would too late detect “oh, it is wet” and turn OFF the motor/water.

Either you have full or good control of this. Or you could instead use a different approach. (I would.)

loop
{

  • When sensor is DRY - turn motor ON (water ON) - wait NN secs - turn motor OFF (water OFF).
  • Wait some time (NN minutes)
    }

You will have to learn your system of a “good” time period for WATERING and a good time period for PAUSING in between,
But I believe this would be a safer method.
Then, you should also try to find out whether the sensor system gives DRY or WET when it is “caput”. If it is DRY - that will always be a problem as it will tell your system “keep on watering”.
Try to make sure the system says WET when Caput (not working OK).

This is a systems design approach and not about programming.

// Others may have other ideas and not agree to this. This is just my way of design. Not necessarily the best.

2 Likes

The other issue is that this type of sensor corrodes very quickly when power is applied to it constantly (electrolysis effect).
A better approach is to switch the VCC supply to the sensor off when it’s not being read.

Your approach of taking an infrequent reading then turning on the pump for a short period works well when you do this.

Pete.

Your code is worked? can you show me the last results?

@Koushal_B hasn’t visited the forum for over a year, so you may not get a response.

I’d suggest that you start a new topic explaining exactly which code you are trying to use and with which hardware, and what results you are getting.

This post should give you a good idea of the type of information you should be providing, and how it should be presented…

Pete.