Blynk Switch not working

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 timer.run() 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.

Pete.