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,
- Moisture is less than 30%
- Temperature higher than 34C
- 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.begin(9600);
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) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi!");
Blynk.begin(auth, ssid, pass, "blynk.cloud", 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: ");
Serial.print(value);
Serial.println("%");
// 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
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
if (tempC != DEVICE_DISCONNECTED_C) {
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println("C");
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
BLYNK_WRITE(V2) {
int mosfetState = param.asInt(); // Get button state from Blynk
digitalWrite(mosfetPin, mosfetState); // Control MOSFET based on button state
}
void loop() {
soilMoisture();
Blynk.run(); // Run Blynk
// Read soil moisture sensor and temperature
timer.run(); // Run Blynk timer
delay(500);
}