Soil moisture value opposite

Thank you all, all problem solved.

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

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

void setup() {
  Serial.begin(9600);
  pinMode(fan,OUTPUT);
  pinMode(valve,OUTPUT);
  pinMode(moist_pin,INPUT);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(2000L, temperature);
  delay(100);
  timer.setInterval(2000L, soilMoisture);
}

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, ");
   
  WidgetLED led1(V4);
  
  if (temperature <= 30){
  digitalWrite(fan,LOW);
  led1.off();
  }
  if (temperature >= 31){
  digitalWrite(fan,HIGH);
  led1.on();
  }
  if (temperature >= 35){
  Blynk.notify("Temperature Too High");
  digitalWrite(fan,HIGH);
  led1.on();
  }
  Blynk.virtualWrite(V1, temperature);
}

void soilMoisture() {
  float moisture_percentage;
  int moist_analog;
  moist_analog = analogRead(moist_pin);
  moist_analog = map(moist_analog, 0, 1023, 100, 0);
  Serial.print(moist_analog);
  Serial.print("&\n\n");
  
  Blynk.virtualWrite(V3,moist_analog);
  WidgetLED led2(V5);
  
  if (moist_analog <= 10){
  digitalWrite(valve,HIGH);
  led2.on();
  }
  else {
  digitalWrite(valve,LOW);
  led2.off();
  }
  
}

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

As you’ve since discovered, you can turn LED widgets on/off with an on/off command, but what you probably didn’t realise is that you can also set their brightness on a scale of 0 to 255.

When you do a virtualWrite(v6,fan) what you’re actually doing is writing “16” to V6, because you’ve set fan==D0 and D0==GPIO16.
It’s value won’t change with the state of the D0 pin, so what you’re doing is setting is to brightness 16 in the range 0-255.

Glad you’ve solved the problem.
It was actually very difficult for us to help you with this because you didn’t share key pieces of information with us. In future it would help if you explained EXACTLY what you were doing, along with the exact code that you are running, the exact setup of your widgets and the output from your serial monitor.

Pete.

2 Likes