#include <ESP32Servo.h>
#include <WiFi.h>
#include <ThingSpeak.h>
#include <NTPClient.h>
#include <time.h>
Servo solar panel;
#define LDR PIN 36
const float GAMMA 0.7;
const float RL10 = 50;
float angle;
const int servoPin = 13;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
unsigned long myChannelNumber 2128230,
const char myWriteAPIKey = "33PDM29T8Sceocne"; int statusCode;
WiFiUDP ntpuDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000);
void setup() {
solarPanel.attach(servoPin);
Serial.begin(115200);
pinMode(LDR_PIN, INPUT);
void setup() {
solarPanel.attach(servoPin);
Serial.begin(115200);
pinMode(LDR PIN, INPUT); WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); wificonnection();
timeClient.begin();
}
void wificonnection(){
Serial.println("Attempting to connect"); while (WiFi.status() != WL_CONNECTED) (
WiFi.begin(ssid, pass); D
Serial.print("."); delay(5000);
}
void channelupdation() {
if(statusCode== 200) {//successful writing code Serial.println("Channel update successful.");
} else {
}
void channelupdation()
{
if(statusCode == 200) {//successful writing code Serial.println("Channel update successful."); }
else {
Serial.println("Problem Writing data. HTTP error code:"+
String(statusCode));
}
}
void loop() {
int analogValue = analogRead(36);
float voltage analogValue / 4699.5
float resistance 2000 voltage (1 voltage / 5);
float lux pow(RL10 163 pow(10, GAMMA) / resistance, (1/GAMMA));
timeClient.update();
time t now timeClient.getEpochTime(),
setenv("TZ", "Asia/Kolkata", 1);
tzset();
timeClient.update();
time t now timeClient.getEpochTime(); setenv("TZ", "Asia/Kolkata", 1); tzset();
struct tm int hour timeinfo localtime(&now); timeinfo->tm hour, int minute timeinfo->tm_min; if(hour>=7 && hour<11)
{ } angle=45;
else if(hour>=11 && hour<14)
{ angle=90;
else if(hour>=14 && hour<17)
angle=135;
else if(hour>=17 && hour<=18)
{ angle=179;
}
else{
angle=0;
}
solarPanel.write(angle);
Serial.println("Time: "+String(hour)+":"+String(minute));
Serial.println("Angle: "+String(angle));
Serial.println("Lux: ");
Serial.print(lux);
Serial.println("Voltage: "+String(voltage));
Serial.println("<--
ThingSpeak.setField(1, voltage);
ThingSpeak.setField(2, angle);
ThingSpeak.setField(3, lux);
ThingSpeak.setField(4,hour); ThingSpeak.setField(5,minute);
statusCode ThingSpeak-writeFields(myChannelNumber, mykriteAPIKey),
channelupdation();
delay(15000);
}