#define BLYNK_TEMPLATE_ID "TMPL6OcE_ipFI"
#define BLYNK_TEMPLATE_NAME "SMART SOLAR TRACKER"
#define BLYNK_AUTH_TOKEN "7diveLnZEh7maIIEnxq1OrlbDxnrmtut"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Blynk authentication token
char auth[] = "7diveLnZEh7maIIEnxq1OrlbDxnrmtut";
// Define the pins for the relay, LDR sensors, servo motors, potentiometer, LCD I2C, and DS18B20
const int relayPin = 22; // GPIO22 for relay module (IN pin)
const int ldrPin1 = 14; // GPIO14 for LDR 1 (AO pin)
const int ldrPin2 = 12; // GPIO12 for LDR 2 (AO pin)
const int servoPin1 = 17; // GPIO17 for Servo 1 (PWM pin)
const int servoPin2 = 16; // GPIO16 for Servo 2 (PWM pin)
const int potentiometerPin = 4; // GPIO4 for potentiometer
const int SDA_PIN = 34; // GPIO34 for SDA (I2C)
const int SCL_PIN = 35; // GPIO35 for SCL (I2C)
const int ONE_WIRE_BUS = 33; // GPIO33 for DS18B20
// Setup the OneWire bus
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Setup the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns, 2 rows
// Define servo objects
Servo servo1;
Servo servo2;
// Blynk connection details
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Blynk virtual pins
#define RELAY_VPIN V0
BlynkTimer timer;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Configure the relay pin as output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is off initially
// Configure the LDR pins as inputs (default state)
pinMode(ldrPin1, INPUT);
pinMode(ldrPin2, INPUT);
// Attach servo objects to pins
servo1.setPeriodHertz(50); // Standard 50 Hz servo
servo1.attach(servoPin1, 1000, 2000); // servoPin1, min pulse width, max pulse width
servo2.setPeriodHertz(50);
servo2.attach(servoPin2, 1000, 2000);
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temperature:");
// Initialize the DS18B20 temperature sensor
sensors.begin();
// Setup a timed function to update Blynk
timer.setInterval(1000L, sendSensorData);
}
void loop() {
Blynk.run();
timer.run();
}
void sendSensorData() {
// Read the analog values from the LDR sensors
int ldrValue1 = analogRead(ldrPin1);
int ldrValue2 = analogRead(ldrPin2);
// Read the potentiometer value
int potValue = analogRead(potentiometerPin);
// Read temperature from DS18B20
sensors.requestTemperatures(); // Send the command to get temperature readings
float temperatureC = sensors.getTempCByIndex(0); // Get temperature in Celsius
// Print temperature to LCD
lcd.setCursor(0, 1);
lcd.print(temperatureC, 1);
lcd.print(" C ");
// Control the relay based on LDR values
// (Controlled by Blynk app)
// Control the servo motors based on LDR values and potentiometer value
// Example: Servos move based on the difference between LDR readings, speed controlled by potentiometer
int difference = ldrValue1 - ldrValue2;
if (difference > 50) {
// Move servo1 towards sunlight with speed controlled by the potentiometer
servo1.write(map(potValue, 0, 1023, 10, 170));
servo2.write(map(potValue, 0, 1023, 10, 170));
} else if (difference < -50) {
// Move servo2 towards sunlight with speed controlled by the potentiometer
servo1.write(map(potValue, 0, 1023, 10, 170));
servo2.write(map(potValue, 0, 1023, 10, 170));
} else {
// No significant difference, stop both servos
servo1.write(90);
servo2.write(90);
}
}
BLYNK_WRITE(RELAY_VPIN) {
int relayState = param.asInt();
digitalWrite(relayPin, relayState);
}