1
2
3
4
#include <LiquidCrystal_I2C.h> // For LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD
const int currentPin = A0; // Pin for current sensor
int sensitivity = 66; // Sensitivity of the current sensor
double currentValue = 0; // Variable to store current value
double voltageValue = 0; // Variable to store voltage value
void setup() {
lcd.init();
lcd.backlight(); // Turn on the LCD backlight
lcd.clear(); // Clear the LCD screen
Serial.begin(9600); // Begin Serial communication for debugging
lcd.setCursor(0, 0);
lcd.print("Current Sensor");
lcd.setCursor(0, 1);
lcd.print("with Arduino");
delay(2000); // Wait 2 seconds before starting
}
void loop() {
int adcValue = analogRead(currentPin); // Read the analog value from the current sensor
double adcVoltage = (adcValue / 1024.0) * 5000; // Convert ADC value to voltage (in millivolts)
currentValue = ((adcVoltage - 2500) / sensitivity); // Calculate the current in amps
// Display the ADC value and current on the Serial Monitor
Serial.print("Raw Sensor Value = ");
Serial.print(adcValue);
Serial.print("\t Voltage(mV) = ");
Serial.print(adcVoltage, 3);
Serial.print("\t Current = ");
Serial.println(currentValue, 3);
delay(1000); // Wait 1 second before repeating
}
void displayCurrentOnLCD(int adcValue, double adcVoltage, double currentValue) {
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print("ADC Value = ");
lcd.setCursor(12, 0);
lcd.print(adcValue); // Display the raw ADC value
delay(2000); // Wait 2 seconds
lcd.setCursor(0, 0);
lcd.print("Voltage (mV) = ");
lcd.setCursor(13, 0);
lcd.print(adcVoltage, 1); // Display the voltage in millivolts
delay(2000); // Wait 2 seconds
lcd.setCursor(0, 0);
lcd.print("Current = ");
lcd.setCursor(10, 0);
lcd.print(currentValue, 2); // Display the current in amps
lcd.setCursor(14, 0);
lcd.print("A"); // Append unit 'A' for amps
}
void loop() {
int adcValue = analogRead(currentPin); // Read current sensor
double adcVoltage = (adcValue / 1024.0) * 5000; // Convert to voltage
currentValue = ((adcVoltage - 2500) / sensitivity); // Convert to current
// Display current data on LCD
displayCurrentOnLCD(adcValue, adcVoltage, currentValue);
delay(1000); // Wait 1 second before next reading
}
#include <WiFi.h> // Required for connecting to WiFi for smart plug control
#include <HTTPClient.h> // For sending HTTP requests to control smart plugs
#include <LiquidCrystal_I2C.h> // For LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD
const char* ssid = "Your_SSID"; // WiFi credentials
const char* password = "Your_PASSWORD";
const char* smartPlugIP = "http://192.168.x.x/control"; // Smart plug IP
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
// Initialize WiFi connection for smart plug control
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
lcd.setCursor(0, 0);
lcd.print("Solar Smart Home");
delay(2000);
}
const int currentPin = A0; // Current sensor pin
const int voltagePin = A1; // Voltage sensor pin
double currentValue = 0; // Variable to store current value
double voltageValue = 0; // Variable to store voltage value
void loop() {
readCurrent();
readVoltage();
displayDataOnLCD(); // Show data on LCD
controlSmartPlug(); // Control smart plug based on energy use
delay(5000); // Wait for 5 seconds before the next loop
}
/*
Function: Read current value from current sensor
*/
void readCurrent() {
int currentRaw = analogRead(currentPin);
currentValue = (currentRaw * (5.0 / 1023.0)) - 2.5; // Convert to Amps
currentValue = currentValue * 30; // Adjust for ACS712 sensor
Serial.print("Current: ");
Serial.println(currentValue);
}
/*
Function: Read voltage from voltage sensor
*/
void readVoltage() {
int voltageRaw = analogRead(voltagePin);
voltageValue = (voltageRaw * (5.0 / 1023.0)) * 240; // Convert to Volts
Serial.print("Voltage: ");
Serial.println(voltageValue);
}
/*
Function: Display sensor data on LCD
*/
void displayDataOnLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Curr: ");
lcd.print(currentValue, 2);
lcd.print(" A");
lcd.setCursor(0, 1);
lcd.print("Volt: ");
lcd.print(voltageValue, 2);
lcd.print(" V");
}
int solarInputPin = A3; // Solar panel input pin
double solarVoltage = 0; // Variable to store solar voltage
/*
Function: Read solar panel voltage
*/
void readSolarPower() {
solarVoltage = analogRead(solarInputPin) * (5.0 / 1023.0); // Convert to volts
Serial.print("Solar Voltage: ");
Serial.println(solarVoltage);
lcd.setCursor(0, 2);
lcd.print("Solar: ");
lcd.print(solarVoltage, 2);
lcd.print(" V");
}
/*
Function: Control smart plug based on energy usage
*/
void controlSmartPlug() {
HTTPClient http;
if (currentValue > 10) { // Example threshold for turning off the appliance
// Send request to turn off the smart plug
http.begin(smartPlugIP); // Specify the URL
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("{\"state\":\"OFF\"}");
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(response);
} else {
Serial.print("Error in sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
Serial.println("Smart Plug turned OFF");
} else {
// Send request to turn on the smart plug
http.begin(smartPlugIP);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("{\"state\":\"ON\"}");
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(response);
} else {
Serial.print("Error in sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
Serial.println("Smart Plug turned ON");
}
}
void loop() {
readCurrent();
readVoltage();
readSolarPower(); // Read solar panel voltage
displayDataOnLCD(); // Show data on LCD
controlSmartPlug(); // Control smart plug based on energy use
delay(5000); // Wait for 5 seconds before the next loop
}
1
2
3
#include <LiquidCrystal_I2C.h> // For LCD display
#include <WiFi.h> // For WiFi connection to control smart switches
#include <HTTPClient.h> // For sending HTTP requests to smart switches
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD
const int solarPin = A0; // Pin for solar panel voltage input
const int batteryPin = A1; // Pin for battery voltage input
const char* ssid = "Your_SSID"; // WiFi credentials
const char* password = "Your_PASSWORD";
const char* smartSwitchIP = "http://192.168.x.x/switch"; // IP of smart switch
double solarVoltage = 0; // To store solar panel voltage
double batteryVoltage = 0; // To store battery voltage
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(115200);
lcd.setCursor(0, 0);
lcd.print("Smart Solar Home");
delay(2000);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Read voltages from solar panel and battery
solarVoltage = readVoltage(solarPin, 100);
batteryVoltage = readVoltage(batteryPin, 12);
// Display readings and control smart switch
displayPowerDataOnLCD();
controlSmartSwitch();
delay(5000); // Wait 5 seconds before repeating
}
/*
Function: Read voltage from a specified pin
*/
double readVoltage(int pin, double scaleFactor) {
int rawValue = analogRead(pin);
double voltage = (rawValue * (5.0 / 1023.0)) * scaleFactor;
Serial.println(voltage);
return voltage;
}
/*
Function: Display solar and battery voltages on the LCD
*/
void displayPowerDataOnLCD() {
lcd.clear();
lcd.print("Solar V: "); lcd.print(solarVoltage, 2);
lcd.setCursor(0, 1);
lcd.print("Battery V: "); lcd.print(batteryVoltage, 2);
}
/*
Function: Display power data on LCD
*/
void displayPowerDataOnLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Solar V: ");
lcd.print(solarVoltage, 2); // Display solar voltage on the LCD
lcd.setCursor(0, 1);
lcd.print("Battery V: ");
lcd.print(batteryVoltage, 2); // Display battery voltage on the LCD
}
/*
Function: Control smart switch based on energy status
*/
void controlSmartSwitch() {
HTTPClient http;
String switchState = batteryVoltage > 11.5 ? "ON" : "OFF"; // Condition to turn on/off
http.begin(smartSwitchIP);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("{\"state\":\"" + switchState + "\"}");
if (httpResponseCode > 0) {
Serial.println("Smart switch turned " + switchState);
} else {
Serial.print("Error turning " + switchState + ": ");
Serial.println(httpResponseCode);
}
http.end();
}
void loop() {
readSolarVoltage(); // Monitor solar panel
readBatteryVoltage(); // Monitor battery status
displayPowerDataOnLCD(); // Show values on LCD
controlSmartSwitch(); // Control appliances based on energy status
delay(5000); // 5-second delay between readings
}
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
// LCD and sensor setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int solarPin = A0;
const int batteryPin = A1;
double solarVoltage = 0;
double batteryVoltage = 0;
// WiFi setup
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
const char* smartSwitchIP = "http://192.168.x.x/switch"; // Example IP for smart switch
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(115200);
// WiFi connection
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
lcd.setCursor(0, 0);
lcd.print("AI Solar System");
delay(2000);
}
void loop() {
// Read voltages from solar panel and battery
solarVoltage = readVoltage(solarPin, 100); // Solar panel scaling factor
batteryVoltage = readVoltage(batteryPin, 12); // Battery scaling factor
// Display the readings on the LCD
displayPowerDataOnLCD();
// AI-driven smart switch control based on energy status
controlSmartSwitch();
delay(5000);
}
/*
Function: Read voltage from a specified pin
*/
double readVoltage(int pin, double scaleFactor) {
int rawValue = analogRead(pin);
return (rawValue * (5.0 / 1023.0)) * scaleFactor;
}
/*
Function: Control smart switch using AI-based prediction
*/
void controlSmartSwitch() {
HTTPClient http;
// Example AI logic to predict energy demand
if (batteryVoltage > 11.5) {
// Predicts high energy availability, turns on appliances
http.begin(smartSwitchIP);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("{\"state\":\"ON\"}");
if (httpResponseCode > 0) {
Serial.println("Smart switch turned ON");
} else {
Serial.print("Error turning ON: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
// Predicts low energy availability, turns off appliances
http.begin(smartSwitchIP);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("{\"state\":\"OFF\"}");
if (httpResponseCode > 0) {
Serial.println("Smart switch turned OFF");
} else {
Serial.print("Error turning OFF: ");
Serial.println(httpResponseCode);
}
http.end();
}
}
/*
Function: Display solar and battery voltages on LCD
*/
void displayPowerDataOnLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Solar V: ");
lcd.print(solarVoltage, 2); // Display solar voltage
lcd.setCursor(0, 1);
lcd.print("Battery V: ");
lcd.print(batteryVoltage, 2); // Display battery voltage
}
void loop() {
solarVoltage = readVoltage(solarPin, 100);
batteryVoltage = readVoltage(batteryPin, 12);
displayPowerDataOnLCD();
controlSmartSwitch(); // Use AI to control appliances
delay(5000);
}