Soil moisture value opposite

Hi,
My soil moisture reading give 1024 when dry.
I am using nodemcu. I tried remap (0,1023,1023,0). I also tried moist = !analogRead(A0)

#include <Blynk.h>
#define BLYNK_PRINT Serial
#include <SimpleDHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[]="47f5d05f740a411380aff7ae68e0dc9";
char ssid[]="iPhone";
char pass[]="1234567899";

int fan = D0;
int valve = D1;
int dht = D2;
int moist = analogRead(A0);
SimpleDHT11 dht11(dht);
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(fan,OUTPUT);
pinMode(valve,OUTPUT);
pinMode(moist,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
Blynk.run();
 byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
    return;
  }
  
  Serial.print("Current temperature: ");
  Serial.print((int)temperature); Serial.print(" *C, "); 
  Serial.print("Current Moisture: ");
  Serial.println(analogRead(A0));

Blynk.virtualWrite(V1, temperature);
if (temperature <= 30){
  digitalWrite(fan,LOW);
  delay(100);
}
if (temperature >= 31){
  digitalWrite(fan,HIGH);
  delay(100);
}
if (temperature >= 35){
  Blynk.notify("Temperature Too High,FAN ON");
  digitalWrite(fan,HIGH);
  delay(100);
}
int moist = analogRead(A0);

Blynk.virtualWrite(V2,moist);
if (moist >= 800){
  digitalWrite(valve,HIGH);
  delay(100);
}
else {
  digitalWrite(valve,LOW);
  delay(100);
}
delay(5000);
}

Hello, Please do not post a new issue into an old topic (that one was 2 years old!!)… I moved your post into your own current topic.

Please also properly format all posted code, as required and demonstrated in the Welcome Topic along with solid details of your issue and what you have been trying

2 Likes

Very bad void loop()

1 Like

On an ESP8266, the analog pin range is 0v-3.3v (or debatibly 3.1v or 3.2v at the high end) What is the voltage output of your moisture sensor?

the spec: operating voltage 3.3v to 5v
digital output of (0v or operating voltage)

Then i recommend you test it first with a basic, non-Blynk, sketch to confirm it is working correctly with your ESP… then it is easy to port that code function over to Blynk.

hi @Gunner I have tried with basic, non blynk. Now the sensor working correctly. now the problem is Blynk.virtualWrite (V5,moisture_percentage); not working in blynk app. But Blynk.virutalWrite(V1,temperature); is working fine in the app.

#include <Blynk.h>
#define BLYNK_PRINT Serial
#include <SimpleDHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[]="8e6dd1bed4d44071a156d4490160885";
char ssid[]="iPhone";
char pass[]="1234567899";

const int moist_pin =A0;
int fan = D0;
int valve = D1;
int dht = D2;
int SensorValuePercent = 0;
SimpleDHT11 dht11(dht);
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(fan,OUTPUT);
pinMode(valve,OUTPUT);
pinMode(moist_pin,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
Blynk.run();
 byte temperature = 0;
  byte humidity = 0;
  byte moiture_percentage = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
    return;
  }
  
  Serial.print("Current temperature: ");
  Serial.print((int)temperature); Serial.print(" *C, "); 



Blynk.virtualWrite(V1, temperature);
if (temperature <= 30){
  digitalWrite(fan,LOW);
  delay(100);
}
if (temperature >= 31){
  digitalWrite(fan,HIGH);
  delay(100);
}
if (temperature >= 35){
  Blynk.notify("Temperature Too High,FAN ON");
  digitalWrite(fan,HIGH);
  delay(100);
}

float moisture_percentage;
int moist_analog;
Blynk.virtualWrite(V5,moisture_percentage);
moist_analog = analogRead(moist_pin);
moisture_percentage = (100 - ( (moist_analog/1024.00) * 100));
Serial.print(moisture_percentage);
Serial.print("%\n\n");
if (moisture_percentage >= 20){
  digitalWrite(valve,HIGH);
  delay(100);
}
else {
  digitalWrite(valve,LOW);
  delay(100);
}
delay(5000);
}

Did you actually read, and understand the “keep your void loop() clean” document that @Gunner provided a link to?

You certainly haven’t re-written your code in the way suggested by that document, which is why it’s not working.

Pete.

3 Likes

You also need to move the Blynk.virtualwrite to after where you update the variable.

Before:

float moisture_percentage;
int moist_analog;
Blynk.virtualWrite(V5,moisture_percentage);
moist_analog = analogRead(moist_pin);
moisture_percentage = (100 - ( (moist_analog/1024.00) * 100));
Serial.print(moisture_percentage);
Serial.print("%\n\n");

After:

float moisture_percentage;
int moist_analog;
moist_analog = analogRead(moist_pin);
moisture_percentage = (100 - ( (moist_analog/1024.00) * 100));
Blynk.virtualWrite(V5,moisture_percentage);
Serial.print(moisture_percentage);
Serial.print("%\n\n");
1 Like

Hi,
I have already done that before, but cannot upload to board (error: espcomm_upload_mem failed). compiled without error. already googled the soloution, still dont know. here is the coding. thats why I still using the very bad void loop coding. pls help. thanks

#include <Blynk.h>
#define BLYNK_PRINT Serial
#include <SimpleDHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[]="8e6dd1bed4d44071a156d44901608855";
char ssid[]="iPhone";
char pass[]="1234567899";

const int moist_pin =A0;
int fan = D0;
int valve = D1;
int dht = D2;
int SensorValuePercent = 0;
SimpleDHT11 dht11(dht);
BlynkTimer timer;


void temperature(){
  
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
  Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
  return;
  }
  Serial.print("Current temperature: ");
  Serial.print((int)temperature); Serial.print(" *C, "); 

  Blynk.virtualWrite(V1, temperature);
  if (temperature <= 30){
  digitalWrite(fan,LOW);
  delay(100);
  }
  if (temperature >= 31){
  digitalWrite(fan,HIGH);
  delay(100);
  }
  if (temperature >= 35){
  Blynk.notify("Temperature Too High,FAN ON");
  digitalWrite(fan,HIGH);
  delay(100);
}
 
}
void soilMoisture() {
  float moisture_percentage;
  int moist_analog;
  moist_analog = analogRead(moist_pin);
  moisture_percentage = (100 - ( (moist_analog/1024.00) * 100));
  Blynk.virtualWrite(V5,moisture_percentage);
  Serial.print(moisture_percentage);
  Serial.print("%\n\n");
  if (moisture_percentage >= 20){
  digitalWrite(valve,HIGH);
  delay(100);
  }
  else {
  digitalWrite(valve,LOW);
  delay(100);
  }
}
void setup() {
  Serial.begin(9600);
  timer.setInterval(1000, temperature);
  timer.setInterval(1000, soilMoisture);
  pinMode(fan,OUTPUT);
  pinMode(valve,OUTPUT);
  pinMode(moist_pin,INPUT);
  Blynk.begin(auth, ssid, pass);
}

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

Whenever I get that message I just try to upload again and usually works the second/third time around.

not working for me, as I already tried many times. It working fine when i upload other code.

Ok now it can be upload, but still no value of Blynk.virtualWrite(V5,moisture_percentage); in blynk app, A0 pin is working. :disappointed_relieved:

1 Like

Both of these are trying to run at the exact same time… either combine them into one function or put a small delay in between so that both timers are offset.

timer.setInterval(1000, temperature);  // Starts a timer to call function every second
delay(100);
timer.setInterval(1000, soilMoisture);  // Starts a timer to call function every second, but with a starting gap of 100ms after the previous timer
2 Likes

What is this??

First, you should learn to avoid repetitive delays in your running code… use timers instead.

Secondly, no fan or valve is going to start up and run for only 100ms… it wouldn’t do more than twitch at best.

1 Like

Still not working, am i supposed to set other thing on the widget other than setting the pin?

#include <Blynk.h>
#define BLYNK_PRINT Serial
#include <SimpleDHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[]="8e6dd1bed4d44071a156d44901608855";
char ssid[]="iPhone";
char pass[]="1234567899";

const int moist_pin =A0;
int fan = D0;
int valve = D1;
int dht = D2;
int SensorValuePercent = 0;
SimpleDHT11 dht11(dht);
BlynkTimer timer;



void temperature(){
  
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
  Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
  return;
  }
  Serial.print("Current temperature: ");
  Serial.print((int)temperature); Serial.print(" *C, "); 

  Blynk.virtualWrite(V1, temperature);
  if (temperature <= 30){
  digitalWrite(fan,LOW);
  }
  if (temperature >= 31){
  digitalWrite(fan,HIGH);
  }
  if (temperature >= 35){
  Blynk.notify("Temperature Too High,FAN ON");
  digitalWrite(fan,HIGH);
}
 
}
void soilMoisture() {
  float moisture_percentage;
  int moist_analog;
  moist_analog = analogRead(moist_pin);
  moisture_percentage = (100 - ( (moist_analog/1024.00) * 100));
  Blynk.virtualWrite(V5,moisture_percentage);
  Serial.print(moisture_percentage);
  Serial.print("%\n\n");
  
  if (moisture_percentage <= 20){
  digitalWrite(valve,HIGH);
  }
  else {
  digitalWrite(valve,LOW);
  }
}
void setup() {
  Serial.begin(9600);
  timer.setInterval(1000, temperature);
  delay(100);
  timer.setInterval(1000, soilMoisture);
  pinMode(fan,OUTPUT);
  pinMode(valve,OUTPUT);
  pinMode(moist_pin,INPUT);
  Blynk.begin(auth, ssid, pass);
}

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

I don’t understand this question.

never mind, now its work.
changed from this

 moist_analog = analogRead(moist_pin);
  moisture_percentage = (100 - ( (moist_analog/1024.00) * 100));
  Blynk.virtualWrite(V5,moisture_percentage);

to this

 moist_analog = analogRead(moist_pin);
  moist_analog = map(moist_analog, 0, 1023, 100, 0);
  Blynk.virtualWrite(V5,moist_analog);

one more thing, if else condition not working, but it work when i use the very bad void loop coding. do u know why?

1 Like

This is a foundational code that will work just fine in a timed loop.

sorry, not if else, digitalWrite problem.

 if (temperature <= 30){
  digitalWrite(fan,LOW);
  }
  if (temperature >= 31){
  digitalWrite(fan,HIGH);
  }
  if (temperature >= 35){
  Blynk.notify("Temperature Too High,FAN ON");
  digitalWrite(fan,HIGH);

only Blynk.notify works, other not working.