/*
Solar Panel Monitoring System
The system adjusts the angle of the solar panel based on the time of day to optimize energy capture.
Manual adjustments can be made using the joystick for testing and fine-tuning.
Light intensity is measured using the LDR to determine the optimal angle for the solar panel.
Data including light intensity, servo angle, and timestamp are collected and sent to ThingSpeak.
The system also prints the collected data on the Serial Monitor for monitoring and debugging purposes.
*/
// Include required libraries
#include <WiFi.h>
#include <ThingSpeak.h>
#include <Wire.h>
#include <ESP32Servo.h>
#include "RTClib.h"
// Define pins
#define SERVO_PIN 13
#define LDR_PIN 34 // Analog pin for LDR
#define SERVO_MIN_ANGLE 0
#define SERVO_MAX_ANGLE 180
RTC_DS1307 rtc;
Servo servo;
// Define wifi credentials
const char *ssid = "Wokwi-GUEST";
const char *pass = "";
// Define ThingSpeak parameters
const char *server = "api.thingspeak.com";
unsigned long myChannelNumber = 2544803;
const char *myWriteAPIKey = "1XJGACT0WM8AFCWO";
WiFiClient client;
// Define calibration constants
const float RL10 = 50; // Reference resistance
const float GAMMA = 0.7; // Gamma value
// Function to calibrate lux value
float lux()
{
int analogValue = analogRead(LDR_PIN); // Read from LDR_PIN
float voltage = analogValue * (5.0 / 4095.0); // Convert analog value to voltage
float resistance = 2000 * voltage / (5.0 - voltage); // Calculate resistance
float luxValue = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA))/10; // Calculate lux
return luxValue;
}
void setup() {
Serial.begin(9600);
Wire.begin();
rtc.begin();
servo.attach(SERVO_PIN);
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
float luxValue = lux(); // Call lux function to calibrate lux value
int servoAngle = 0; // Default servo angle
// Adjust servo angle based on the time of day
if (hour >= 6 && hour < 11) {
servoAngle = 90; // Set angle to 90 degrees from 6 AM to 11 AM
} else if (hour >= 11 && hour < 16) {
servoAngle = 0; // Set angle to 0 degrees from 11 AM to 4 PM
} else if (hour >= 16 && hour < 18) {
servoAngle = 180; // Set angle to 180 degrees from 4 PM to 6 PM
}
// Update data to ThingSpeak fields
ThingSpeak.setField(1, luxValue); // Lux value
ThingSpeak.setField(2, servoAngle); // Angle
int status = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (status == 200) {
Serial.println("Data sent to ThingSpeak successfully!");
} else {
Serial.println("Error sending data to ThingSpeak");
}
// Print data to Serial Monitor
Serial.print("Lux: ");
Serial.print(luxValue);
Serial.print(" Servo Angle: ");
Serial.print(servoAngle);
Serial.print(" Timestamp: ");
Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL));
Serial.println();
delay(10000); //
}