#include <ESP32Servo.h>
Servo horizontalServo;
int servoh = 90;
int servohLimitHigh = 160;
int servohLimitLow = 20;
int tolerance = 10;
#define LIGHT_SENSOR_PIN_left 25
#define LIGHT_SENSOR_PIN_right 26
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define INPUT_VOLTAGE_SENSE_PIN 34
#define INPUT_CURRENT_SENSE_PIN 35
#define VOLTAGE_SCALE 7.911 // R1+R2 / R2 // ( 47K + 6.8K ) / 6.8K
#define CURRENT_SCALE 1.5 // R4+R5 / R5 // ( 1K + 2K ) / 2K
double mVperAmp = 66; //Sensityvit of the sensor // use 100 for 20A Module and 66 for 30A Module
double ACSoffset = 514; // Ideally it should be ( 0.1 x Vcc ) // measured value is 514mV
unsigned long last_time =0;
unsigned long current_time =0;
float power =0 ; // Power in Watt
float energy =0 ; // Emergy in Watt-Hour
/* Fill-in your Template ID (only if using Blynk.Cloud) */
//#define BLYNK_TEMPLATE_ID "YourTemplateID"
#define BLYNK_TEMPLATE_ID "TMPLRmVUzg1f"
#define BLYNK_DEVICE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "1GFT4wX1DQBYiU_xqLx51h6XAY21XP9e"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "ZTXCrUYxoBDkbIBOBU5QOWFWytZoxAcy";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
void setup(){
Serial.begin(9600);
horizontalServo.attach(27);
horizontalServo.write(160);
delay(3000);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
// deklarasi integer ldr dan membaca cahaya dari ldr
int ldrLeft = analogRead(LIGHT_SENSOR_PIN_left);
int ldrRight = analogRead(LIGHT_SENSOR_PIN_right);
int diffHorizontal = ldrLeft - ldrRight;// check the diffirence of left and right
if ( diffHorizontal <-1*tolerance || diffHorizontal > tolerance){ //check if the difference is tolerable else rotate the motor
if (ldrLeft > ldrRight){
servoh = servoh+2;
if (servoh > servohLimitHigh){servoh = servohLimitHigh;}
} else if (ldrLeft < ldrRight){
servoh = servoh-2;
if (servoh < servohLimitLow){servoh = servohLimitLow;}
} else if (ldrLeft = ldrRight){
// nothing
}
}
horizontalServo.write(servoh);
delay(40);
// read voltage and current
float voltage = abs(INPUT_VOLTAGE_SENSE_PIN) ;
float current = abs(INPUT_CURRENT_SENSE_PIN) ;
// Calculate power and energy
power = current * voltage ; // calculate power in Watt
last_time = current_time;
current_time = millis();
energy = energy + power *(( current_time -last_time) /3600000.0) ; // calculate power in Watt-Hour // 1 Hour = 60mins x 60 Secs x 1000 Milli Secs
// ================= Display Data on Blynk App ================================================
Blynk.run();
Blynk.virtualWrite(0, voltage ); // virtual pin 0
Blynk.virtualWrite(1, current ); // virtual pin 1
Blynk.virtualWrite(2, power); // virtual pin 2
Blynk.virtualWrite(3,energy/1000);// virtual pin 3
}