Blynk Switch not working

Hi guys I am having trouble while doing for my project. I create a code which turn on the mosfet if one of the conditions are met,

  1. Moisture is less than 30%
  2. Temperature higher than 34C
  3. Manuallly turn on by switch from blynk

the first two i am able to run but when I use the switch from Blynk, it does not have any effect on the mosfet. I tried to find solution from the internet but the result still the same. Can you guys find where did i do wrong? Here is my full code

#define BLYNK_TEMPLATE_ID "ccccccccccb"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "iNMatAZYudN3h1IOmqHdkp4659UeQ_CY"

#include <Wire.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define soil_moisture_pin 5  // Analog pin for soil moisture sensor
#define mosfetPin 25    // Digital pin for MOSFET gate
#define moistureThreshold 30  // Define moisture threshold value (adjust as needed)
#define ONE_WIRE_BUS 21  // Digital pin for DS18B20 temperature sensor

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

BlynkTimer timer;

// Enter your Auth token
char auth[] = "iNMatAZYudN3h1IOmqHdkp4659UeQ_CY";

// Enter your WiFi SSID and password
char ssid[] = "xxxxxxxxxxxx ";
char pass[] = "ihyiuhgtyyutbi";

void setup() {
  Serial.println("Simulation, Battery Gauge, and MOSFET Control started.");
  pinMode(mosfetPin, OUTPUT);
  digitalWrite(mosfetPin, LOW);  // Initialize MOSFET gate to LOW state
  sensors.begin();  // Initialize temperature sensor
   // Print message indicating Wi-Fi connection attempt
  WiFi.begin(ssid, pass);
  Serial.println("Connecting to Wi-Fi...");
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("\nConnected to Wi-Fi!");
  Blynk.begin(auth, ssid, pass, "", 80);  // Initialize Blynk

// Function to read soil moisture sensor value and send to Blynk
void soilMoisture() {
  int value = 31;

  Blynk.virtualWrite(V0, value); // Send moisture percentage to Blynk
  Serial.print("Soil Moisture: ");

  // Check if soil moisture is below threshold
  if (value < moistureThreshold) {
    digitalWrite(mosfetPin, HIGH); // Turn on pump (MOSFET gate HIGH)
    Serial.println("Pump turned ON");
  } else {
    digitalWrite(mosfetPin, LOW); // Turn off pump (MOSFET gate LOW)
    Serial.println("Pump turned OFF");
  // Read temperature
  float tempC = sensors.getTempCByIndex(0);

    Serial.print("Temperature: ");

    Blynk.virtualWrite(V1, tempC); // Send temperature to Blynk
    // Check temperature and turn on pump if above 30°C
    if (tempC > 34) {
      digitalWrite(mosfetPin, HIGH); // Turn on pump (MOSFET gate HIGH)
      Serial.println("Pump turned ON due to high temperature");
  } else {
    Serial.println("Error: Could not read temperature data");

// Function to control MOSFET based on Blynk button state
  int mosfetState = param.asInt(); // Get button state from Blynk
  digitalWrite(mosfetPin, mosfetState); // Control MOSFET based on button state

void loop() {
  soilMoisture();; // Run Blynk
   // Read soil moisture sensor and temperature; // Run Blynk timer

A few comments from me…

  1. This code is redundant if you are using Blynk.begin()…
  1. You are using GPIO5 as an analog input…

but GPIO5 is not connect to an Analog to Digital Converter (ADC) in tge ESP32 chip. You need to use one of the GPIO pins that is connected to ADC1 (Because the ADC2 pins can’t be used at the same time was WiFi). ADC1 pins are…

  • ADC1_CH0 (GPIO 36)
  • ADC1_CH1 (GPIO 37)
  • ADC1_CH2 (GPIO 38)
  • ADC1_CH3 (GPIO 39)
  • ADC1_CH4 (GPIO 32)
  • ADC1_CH5 (GPIO 33)
  • ADC1_CH6 (GPIO 34)
  • ADC1_CH7 (GPIO 35)

See this tutorial for more info…

  1. You have defined a BlynkTimer object, and you are servicing that object by having in your void loop, but you don’t have any BlynkTimer instances defined in your void setup.
    Instead, you are calling the soilMoisture function directly from your void loop, and using a blocking delay in your void loop to provide the necessary timing (although I have no urea why you’d need to check the soil moisture value twice every second).
    You should read this tutorial about how to correctly use BlynkTimer instead of delays, and how to correctly structure your void loop…
  1. You aren’t reading the value of your soil moisture sensor, you are simply saying that the reading is always always 31…
  1. Your switch attached to Blynk virtual pin V2 will only trigger once, when the value of V2 changes. It will then be overridden no more than 500ms later when your void loop executes the soilMoisture function.
    If you want to take into account the status of the V2 virtual pin then you need to make mosfetState a global variable and check the status of that variable in your soilmoisture function.