//*****************************************************************************************************************
//*****************************************************************************************************************
//************<<<< Arduino Nano, DS3231 Real Time Clock & Six Relay Module Lights Timer V3.0 >>>>******************
//*****************************************************************************************************************
//*****************************************************************************************************************
/*
Sketch uses 5722 bytes (18%) of program storage space. Maximum is 30720 bytes.
Global variables use 286 bytes (13%) of dynamic memory, leaving 1762 bytes for local variables. Maximum is 2048 bytes.
************** UPDATED ON Oct 13 2018 *************
*/
// no oled version
// six lights/relay control each with two on/off routines
// for oled version
//*****************************************************************************************************************
//*********************<<<< PLEASE VISIT / SUBSCRIBE MY YOUTUBE CHANNEL >>>>***************************************
// comment for more info
//*********************<<<< Youtube.com/CrazyGuyOfficial >>>>*****************************************
//*****************************************************************************************************************
//*****************************************************************************************************************
// INCLUDE LIBRARIES
//*****************************************************************************************************************
#include <Time.h> // Time Manipulation
#include <Wire.h>
#include <DS1307RTC.h> // DS1307 RTC
//**********************//**********************//**********************
// PINS AND CONNECTIONS
//**********************//**********************//**********************
const byte lightBlink = 3 ;
const byte lightOne = 5 ;
const byte lightTwo = 6 ;
const byte lightThree = 4 ;
const byte lightFour = 7 ;
const byte lightFive = 2 ;
const byte lightSix = 8 ;
// DS3231 RTC "SDA to A4", "SCL to A5" "Vcc to 5V arduino pin Gnd to Gnd"
// Add more names/pins for more relays
//##################################################
// Light Timer ON/OFF Time Settings DEFAULT
//##################################################
byte OnOne1 = 14 ; // Hour when Light 1 will turn ON (24 hr format NO zeros before single digits)
byte OffOne1 = 8 ; // Hour when Light 1 will turn OFF (24 hr format NO zeros before single digits)
byte OnOne2 = 14 ; // Hour when Light 1 will turn ON (24 hr format NO zeros before single digits)
byte OffOne2 = 8 ; // Hour when Light 1 will turn OFF (24 hr format NO zeros before single digits)
byte OnTwo1 = 20 ;
byte OffTwo1 = 8 ;
byte OnTwo2 = 20 ;
byte OffTwo2 = 8 ;
byte OnThree1 = 14 ;
byte OffThree1 = 8 ;
byte OnThree2 = 14 ;
byte OffThree2 = 8 ;
byte OnFour1 = 14 ;
byte OffFour1 = 8 ;
byte OnFour2 = 14 ;
byte OffFour2 = 8 ;
byte OnFive1 = 14 ;
byte OffFive1 = 8 ;
byte OnFive2 = 14 ;
byte OffFive2 = 8 ;
byte OnSix1 = 14 ;
byte OffSix1 = 8 ;
byte OnSix2 = 14 ;
byte OffSix2 = 8 ;
//*****************************************************************************************************************
bool RTCerr = 0;
bool LightOneSts = 0 ;
bool LightTwoSts = 0 ;
bool LightThreeSts = 0 ;
bool LightFourSts = 0 ;
bool LightFiveSts = 0 ;
bool LightSixSts = 0 ;
bool ppOne1 = 0 ;
bool ppOne2 = 0 ;
bool ppTwo1 = 0 ;
bool ppTwo2 = 0 ;
bool ppThree1 = 0 ;
bool ppThree2 = 0 ;
bool ppFour1 = 0 ;
bool ppFour2 = 0 ;
bool ppFive1 = 0 ;
bool ppFive2 = 0 ;
bool ppSix1 = 0 ;
bool ppSix2 = 0 ;
unsigned long SecsOne = 0;
//##################
//## RTC ADDRESS ##
//##################
#define DS1307_ID 0x68 // I2c Address of the RTC
byte zero = 0x00; //workaround for issue #527
//#################################
//*****************************************************************************************************************
// SETUP BEGINS
//*****************************************************************************************************************
void setup()
{
digitalWrite(lightOne, HIGH);
digitalWrite(lightTwo, HIGH); // turn the relay "OFF" and turning the pin "LOW" turns the relay "ON"
digitalWrite(lightThree, HIGH);
digitalWrite(lightFour, HIGH);
digitalWrite(lightFive, HIGH); // turn the relay "OFF" and turning the pin "LOW" turns the relay "ON"
digitalWrite(lightSix, HIGH);
pinMode(lightBlink, OUTPUT);
pinMode(lightOne, OUTPUT);
pinMode(lightTwo, OUTPUT);
pinMode(lightThree, OUTPUT);
pinMode(lightFour, OUTPUT);
pinMode(lightFive, OUTPUT);
pinMode(lightSix, OUTPUT);
//**********************//**********************//***********************//**********************
//Check to see if the RTC is present.if yes then Set the ARDUINO's INTERNAL clock accordingly
//**********************//**********************//***********************//**********************
Wire.beginTransmission(DS1307_ID);
Wire.write((uint8_t)0x00);
setSyncProvider(RTC.get); //Yes it did, Sync the time from the RTC
setSyncInterval(20);
if (timeStatus() != timeSet)
RTCerr = 1;
else
RTCerr = 0;
Delay15S();
}
//*****************************************************************************************************************
// SETUP END
//*****************************************************************************************************************
void Delay15S()
{
delay(1500);
}
//*****************************************************************************************************************
// LOOP BEGINS
//*****************************************************************************************************************
void loop()
{
//#####################################
//####### LIGHTs ON/OFF ###############
//#####################################
//########## LIGHT ONE PP1##########################
if (OffOne1 > OnOne1)
{
if (hour() >= OnOne1 && hour () <= OffOne1 - 1)
{
TurnLightOneOn1();
}
else
{
TurnLightOneOff1();
}
}
//**********************************************************
if (OffOne1 < OnOne1 )
{
if (hour() >= OnOne1 || hour () <= OffOne1 - 1)
{
TurnLightOneOn1();
}
else
{
TurnLightOneOff1();
}
}
//########## LIGHT ONE PP2 ##########################
if (OffOne2 > OnOne2)
{
if (hour() >= OnOne2 && hour () <= OffOne2 - 1)
{
TurnLightOneOn2();
}
else
{
TurnLightOneOff2();
}
}
//**********************************************************
if (OffOne2 < OnOne2 )
{
if (hour() >= OnOne2 || hour () <= OffOne2 - 1)
{
TurnLightOneOn2();
}
else
{
TurnLightOneOff2();
}
}
//####################################################################
//########## LIGHT TWO pp1 ##########################
if (OffTwo1 > OnTwo1)
{
if (hour() >= OnTwo1 && hour () <= OffTwo1 - 1)
{
TurnLightTwoOn1();
}
else
{
TurnLightTwoOff1();
}
}
//**********************************************************
if (OffTwo1 < OnTwo1)
{
if (hour() >= OnTwo1 || hour () <= OffTwo1 - 1)
{
TurnLightTwoOn1();
}
else
{
TurnLightTwoOff1();
}
}
//########## LIGHT TWO pp2 ##########################
if (OffTwo2 > OnTwo2)
{
if (hour() >= OnTwo2 && hour () <= OffTwo2 - 1)
{
TurnLightTwoOn2();
}
else
{
TurnLightTwoOff2();
}
}
//**********************************************************
if (OffTwo2 < OnTwo2)
{
if (hour() >= OnTwo2 || hour () <= OffTwo2 - 1)
{
TurnLightTwoOn2();
}
else
{
TurnLightTwoOff2();
}
}
//####################################################################
//########## LIGHT THREE pp 1 ##########################
if (OffThree1 > OnThree1)
{
if (hour() >= OnThree1 && hour () <= OffThree1 - 1)
{
TurnLightThreeOn1();
}
else
{
TurnLightThreeOff1();
}
}
//**********************************************************
if (OffThree1 < OnThree1)
{
if (hour() >= OnThree1 || hour () <= OffThree1 - 1)
{
TurnLightThreeOn1();
}
else
{
TurnLightThreeOff1();
}
}
//########## LIGHT THREE pp 2 ##########################
if (OffThree2 > OnThree2)
{
if (hour() >= OnThree2 && hour () <= OffThree2 - 1)
{
TurnLightThreeOn2();
}
else
{
TurnLightThreeOff2();
}
}
//**********************************************************
if (OffThree2 < OnThree2)
{
if (hour() >= OnThree2 || hour () <= OffThree2 - 1)
{
TurnLightThreeOn2();
}
else
{
TurnLightThreeOff2();
}
}
//####################################################################
//########## LIGHT FOUR pp1 ##########################
if (OffFour1 > OnFour1)
{
if (hour() >= OnFour1 && hour () <= OffFour1 - 1)
{
TurnLightFourOn1();
}
else
{
TurnLightFourOff1();
}
}
//**********************************************************
if (OffFour1 < OnFour1)
{
if (hour() >= OnFour1 || hour () <= OffFour1 - 1)
{
TurnLightFourOn1();
}
else
{
TurnLightFourOff1();
}
}
//########## LIGHT FOUR pp2 ##########################
if (OffFour2 > OnFour2)
{
if (hour() >= OnFour2 && hour () <= OffFour2 - 1)
{
TurnLightFourOn2();
}
else
{
TurnLightFourOff2();
}
}
//**********************************************************
if (OffFour2 < OnFour2)
{
if (hour() >= OnFour2 || hour () <= OffFour2 - 1)
{
TurnLightFourOn2();
}
else
{
TurnLightFourOff2();
}
}
//####################################################################
//########## LIGHT FIVE pp1 ##########################
if (OffFive1 > OnFive1)
{
if (hour() >= OnFive1 && hour () <= OffFive1 - 1)
{
TurnLightFiveOn1();
}
else
{
TurnLightFiveOff1();
}
}
//**********************************************************
if (OffFive1 < OnFive1)
{
if (hour() >= OnFive1 || hour () <= OffFive1 - 1)
{
TurnLightFiveOn1();
}
else
{
TurnLightFiveOff1();
}
}
//########## LIGHT FIVE pp2 ##########################
if (OffFive2 > OnFive2)
{
if (hour() >= OnFive2 && hour () <= OffFive2 - 1)
{
TurnLightFiveOn2();
}
else
{
TurnLightFiveOff2();
}
}
//**********************************************************
if (OffFive2 < OnFive2)
{
if (hour() >= OnFive2 || hour () <= OffFive2 - 1)
{
TurnLightFiveOn2();
}
else
{
TurnLightFiveOff2();
}
}
//####################################################################
//########## LIGHT SIX pp1 ##########################
if (OffSix1 > OnSix1)
{
if (hour() >= OnSix1 && hour () <= OffSix1 - 1)
{
TurnLightSixOn1();
}
else
{
TurnLightSixOff1();
}
}
//**********************************************************
if (OffSix1 < OnSix1)
{
if (hour() >= OnSix1 || hour () <= OffSix1 - 1)
{
TurnLightSixOn1();
}
else
{
TurnLightSixOff1();
}
}
//########## LIGHT SIX pp2 ##########################
if (OffSix2 > OnSix2)
{
if (hour() >= OnSix2 && hour () <= OffSix2 - 1)
{
TurnLightSixOn2();
}
else
{
TurnLightSixOff2();
}
}
//**********************************************************
if (OffSix2 < OnSix2)
{
if (hour() >= OnSix2 || hour () <= OffSix2 - 1)
{
TurnLightSixOn2();
}
else
{
TurnLightSixOff2();
}
}
//####################################################################
//#####################################
// Blink Led And Call RTC TEST
//#####################################
if (millis() / 1000 - SecsOne > 10 ) //
{
digitalWrite (lightBlink, HIGH); // blink status LED
rtctest();
SecsOne = millis() / 1000; // then reset the timer
}
}
//**********************//**********************//**********************
// LOOP END
//**********************//**********************//**********************
void TurnLightOneOn1()
{
if (!LightOneSts)
{
digitalWrite(lightOne, LOW);
LightOneSts = 1 ;
Delay15S();
} ppOne1 = 1 ;
}
void TurnLightOneOff1()
{
if (!ppOne2)
{
digitalWrite(lightOne, HIGH);
LightOneSts = 0 ;
}
ppOne1 = 0 ;
}
void TurnLightOneOn2()
{
if (!LightOneSts)
{
digitalWrite(lightOne, LOW);
LightOneSts = 1 ;
Delay15S();
} ppOne2 = 1 ;
}
void TurnLightOneOff2()
{
if (!ppOne1)
{
digitalWrite(lightOne, HIGH);
LightOneSts = 0 ;
}
ppOne2 = 0 ;
}
void TurnLightTwoOn1()
{
if (!LightTwoSts)
{
digitalWrite(lightTwo, LOW);
LightTwoSts = 1 ;
Delay15S();
} ppTwo1 = 1 ;
}
void TurnLightTwoOff1()
{
if (!ppTwo2)
{
digitalWrite(lightTwo, HIGH);
LightTwoSts = 0 ;
}
ppTwo1 = 0 ;
}
void TurnLightTwoOn2()
{
if (!LightTwoSts)
{
digitalWrite(lightTwo, LOW);
LightTwoSts = 1 ;
Delay15S();
} ppTwo2 = 1 ;
}
void TurnLightTwoOff2()
{
if (!ppTwo1)
{
digitalWrite(lightTwo, HIGH);
LightTwoSts = 0 ;
}
ppTwo2 = 0 ;
}
void TurnLightThreeOn1()
{
if (!LightThreeSts)
{
digitalWrite(lightThree, LOW);
LightThreeSts = 1 ;
Delay15S();
} ppThree1 = 1 ;
}
void TurnLightThreeOff1()
{
if (!ppThree2)
{
digitalWrite(lightThree, HIGH);
LightThreeSts = 0 ;
}
ppThree1 = 0 ;
}
void TurnLightThreeOn2()
{
if (!LightThreeSts)
{
digitalWrite(lightThree, LOW);
LightThreeSts = 1 ;
Delay15S();
} ppThree2 = 1 ;
}
void TurnLightThreeOff2()
{
if (!ppThree1)
{
digitalWrite(lightThree, HIGH);
LightThreeSts = 0 ;
}
ppThree2 = 0 ;
}
void TurnLightFourOn1()
{
if (!LightFourSts)
{
digitalWrite(lightFour, LOW);
LightFourSts = 1 ;
Delay15S();
}
ppFour1 = 1;
}
void TurnLightFourOff1()
{
if (!ppFour2)
{
digitalWrite(lightFour, HIGH);
LightFourSts = 0 ;
}
ppFour1 = 0;
}
void TurnLightFourOn2()
{
if (!LightFourSts)
{
digitalWrite(lightFour, LOW);
LightFourSts = 1 ;
Delay15S();
} ppFour2 = 1;
}
void TurnLightFourOff2()
{
if (!ppFour1)
{
digitalWrite(lightFour, HIGH);
LightFourSts = 0 ;
}
ppFour2 = 0;
}
void TurnLightFiveOn1()
{
if (!LightFiveSts)
{
digitalWrite(lightFive, LOW);
LightFiveSts = 1 ;
Delay15S();
} ppFive1 = 1;
}
void TurnLightFiveOff1()
{
if (!ppFive2)
{
digitalWrite(lightFive, HIGH);
LightFiveSts = 0 ;
}
ppFive1 = 0;
}
void TurnLightFiveOn2()
{
if (!LightFiveSts)
{
digitalWrite(lightFive, LOW);
LightFiveSts = 1 ;
Delay15S();
} ppFive2 = 1;
}
void TurnLightFiveOff2()
{
if (!ppFive1)
{
digitalWrite(lightFive, HIGH);
LightFiveSts = 0 ;
}
ppFive2 = 0;
}
void TurnLightSixOn1()
{
if (!LightSixSts)
{
digitalWrite(lightSix, LOW);
LightSixSts = 1 ;
Delay15S();
}
ppSix1 = 1;
}
void TurnLightSixOff1()
{
if (!ppSix2)
{
digitalWrite(lightSix, HIGH);
LightSixSts = 0 ;
}
ppSix1 = 0;
}
void TurnLightSixOn2()
{
if (!LightSixSts)
{
digitalWrite(lightSix, LOW);
LightSixSts = 1 ;
Delay15S();
} ppSix2 = 1;
}
void TurnLightSixOff2()
{
if (!ppSix1)
{
digitalWrite(lightSix, HIGH);
LightSixSts = 0 ;
}
ppSix2 = 0;
}
//#####################
// RTC Test ##########
//#####################
void rtctest()
{
if (Wire.endTransmission() == 0) //Did the RTC respond?
{
RTCerr = 0; // set RTC error status boolean to false
digitalWrite (lightBlink, LOW); // blink status LED
}
else
{
RTCerr = 1;
digitalWrite (lightBlink, HIGH);
}
}
Do-it-yourself,DIY,How-To,Electronics,Arduino,Esp,Raspberry-Pi,Programming,Hacking Stuff,Gardening,Photography,Hobby,Fun
Showing posts with label control. Show all posts
Showing posts with label control. Show all posts
Arduino Grow Light Timer Six Switch/Relay Dual on/off Control V3.0 no oled/button version
Labels:
arduino,
auto switch,
control,
diy,
do it yourself,
ds3231,
grow,
light on off,
nano,
rtc,
timer
ESP8266 Battery Voltmeter / Charge Controller ShutDown Reset Pi UPS Back Up Supply[OUT-DATED]
//################################/ //####################################################################
//##############################/ //#################################################_________ #########
//#############################/ //=================================================//////////##########
//####################//Esp8266 Battery Manager ------------------ // //######@@##
//########################## WEMOS D1 MINI ##############----- // O O O //#######@@##
//###############################// for------### ===============================//<<<<<<<//########@@##
//###################//UPS Back UP Power Supply for ######################################################
//#################### RASPBERRY PI 3 / SECURITY CAMERA PROJECT ########################################
//############################/ |WWWW| /##################################################################
//###########################/ \O_O/ ####################################################################
//# CAN BE USED AS A GENERAL PURPOSE BATTERY MONITOR / CHARGING ##########################################
//############# AFTER SOME MODIFICATIONS ############# NI-CD / LEAD ACID #################################
//#################################################### NOT LI-ION ########################################
//###################
///// OUTDATED //// Watch :https://www.youtube.com/watch?v=GTEyUZAViSA
//###################
// ********************************************************************************************
// ***** IMPORTANT : rest of the details will be posted later
// ********************************************************************************************
// #########################################################################
// #### I'll improve it as i learn more
// #########################################################################
// PLEASE LIKE AND SUBSCRIBE
// MY YOUTUBE CHANNEL
// YOUTUBE.COM/CRAZYGUYOFFICIAL
// Visit Blog for Sketch Updates and more Sketches and "things"
// #########################################################################
#include <ESP8266WiFi.h>
//#################################
const char* ssid = "wubbadlubba";
const char* password ="dubdub420";
WiFiServer server(80);
//#################################
//### Charge Voltage Settings #####
//#################################
float LoadOFF = 6.50 ; // Voltage at which Load will Turn OFF
float LoadON = 10.00 ; // Voltage at which Load will Turn ON
float ChargeOFF = 12.00 ; // Cut OFF Voltage at which Charging will Turn OFF
float ChargeON = 9.60 ; // Voltage at which Charging will Turn ON
//###########################################
//### Voltage to ShutDown For Raspberry pi ##
//###########################################
float PiOFF = 7.50 ; // Voltage at which pi shutdown pin will Turn ON
// telling the Pi to ShutDown
//#############################################
//###### PINS for Sensing and Switching #######
//#############################################
byte ChrgS = 2; // D4 // Charging Switch Pin // using an NPN transistor and P-channel Mosfet
byte LoadS = 5; // D1 // Load ON / OFF Pin
byte PowerS = 4; // D2 // Power Sense to check if power is available or not
//##############################################
//### Pin for Rapberry pi Shutdown / Reset ####
//##############################################
byte StfdPin = 12 ; // D6 // Pin For signaling Pi to ShutDown // pi will use Gpio to read
byte PiStsPin = 13 ; // D7 // Pin to see Pi's status // pi will use gpio to send
byte PiRst = 14 ; // D5 // pin to reset pi using NPN transistor connected to RUN pad/pin on pi
//######################################
//### ADC pin & Resistors Values #######
//######################################
float R1 = 47000.00; // resistance of R1 (47 K)
float R2 = 6650.00; // resistance of R2 (6.65 K)
byte analInput = A0; // ADC pin
//##############################################
//#### Variables used for Voltage Calculation ##
//##############################################
float Vout = 0.00;
float Vin = 0.00;
int Val = 0;
//#################################
//### Variables for Timers ########
//#################################
unsigned long Secs = 0; // Secs Counter used to Turn OFF Charging
int Time1 = 250; // Time in Seconds after which the Charging will Turn off
// after reaching Cut off voltage
unsigned long SecsTwo = 0; // Secs Counter two used to Turn ON Load
byte Time2 = 250; // Time in Seconds after which the Load will Turn On
// after reaching Load Turn On Voltage
byte Time3 = 250; // Time in Seconds after which the Pi Will be Reset
//*******************************************************************************************
//** IMPORTANT *** For Larger Values Change Variable type to "int Time" instead of byte **
//*******************************************************************************************
//################################
bool Charging = 0 ; // don't change these values
bool LoadConnect = 0 ; // manually
bool Power = 0 ;
bool PiUp = 0 ;
//################################
//###############################################################################################
//###############################################################################################
//################ SETUP START ##################################################################
//###############################################################################################
//###############################################################################################
void setup()
{
//**** OUTPUT PINS *****
pinMode(StfdPin,OUTPUT); // Shutdown Signal Pin for Pi
pinMode(PiRst, OUTPUT); // Reset Pin for Pi
pinMode(ChrgS, OUTPUT); // Charging ON / OFF Pin
pinMode(LoadS, OUTPUT); // Load ON / OFF Pin
digitalWrite (ChrgS,HIGH); // Charging ON // npn transistor and P-channel mosfet
Charging = 1 ;
digitalWrite (StfdPin,LOW); // Keep Shutdown signal pin OFF
digitalWrite (LoadS,LOW); // Keep LOAD OFF
LoadConnect = 0 ;
digitalWrite(PiRst, LOW); // Keep Pi Reset Pin OFF
//**** INPUT PINS *****
pinMode(analInput, INPUT); // Adc analog read pin //voltage divider circuit for battery voltage
pinMode(PiStsPin, INPUT) ; // Pin for Reading Pi Status using Pi's GPIO HIGH/LOW State
pinMode(PowerS, INPUT) ; // Pin for Sensing Power from Charger / Adapter using npn transistor
// and a voltage divider circuit
Power =0;
//##### Serial & WIFI Stuff ###########
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println(".");
}
Serial.println("WiFi connected");
server.begin();
Serial.println("Waiting for the IP ..");
delay(5000);
Serial.println(WiFi.localIP());
}
//###############################################################################################
//###############################################################################################
//################ SETUP END ####################################################################
//###############################################################################################
//###############################################################################################
//##############################################################################################
//##############################################################################################
//################ Function for Voltage Calculation ############################################
//##############################################################################################
//##############################################################################################
void VoltageCalc()
{
delay(1000);
Val = analogRead(analInput); // reads Analog input using A0
delay(1000); //for maintaining the speed of the output in serial monitor
Vout = (Val * 3.40/ 1024.00); // formula for calculating voltage out from the Divider circuit
Vin = Vout / (R2 / (R1 + R2)); // formula for calculating Battery voltage
if (Vin < 0.25) // set Voltage to Zero if reading less than .50 V
{
Vin = 0.00;
}
Serial.println("################");
Serial.println("Battery Voltage: ");
Serial.println(Vin);
}
//###############################################################################################
//###############################################################################################
//################ LOOP BEGINS ##################################################################
//###############################################################################################
//###############################################################################################
void loop()
{
//delay(1000); // for maintaining the speed of the output in serial monitor
VoltageCalc(); // Calculate Battery Voltage
//##########################################
//## NO POWER ### TURN LOAD OFF ### Pi OFF #
//##########################################
if (!Power) // **** IF Power is NOT Available ****
{
digitalWrite(ChrgS,HIGH); // Turn ON Charging
Charging = 1 ;
if(LoadConnect) // and Load is Connected
{
PiUp = !digitalRead(PiStsPin); // Set Pi's Status
if(Vin <= PiOFF && PiUp) // IF Voltage is less than PiOFF // and pi is running
{
Serial.println("it has been an Honour Pi");
digitalWrite(StfdPin,HIGH); // Turn Signal Pin HIGH so, Pi can shutdown properly
}
if(Vin <= LoadOFF && !PiUp) // if Voltage is less than LoadOFF
{
Serial.println ("Turned Load OFF "); // Turn Load OFF
digitalWrite(LoadS, LOW);
LoadConnect = 0;
}
}
Secs = SecsTwo = millis()/1000; // Keep BOTH Timers Updated while no power
Power = !digitalRead(PowerS); // Check for Power Availability
}
//#################################################
//## YES POWER ## TURN CHARGING / LOAD ON / OFF ###
//#################################################
//#################################
else // **** IF Power is Available *****
{ //#################################
//#################################
//##### TURN CHARGING OFF #########
//#################################
Power = !digitalRead(PowerS); // Check for Power // the "!"/not inverts the output High means OFF Low means ON
if (Charging) // IF charging is ON
{
if(Vin >= ChargeOFF) //**** IF Voltage has Reached Cut off/ ChargeOFF Value ****
{
Serial.println ("CHARGE OFF Voltage Reached");
Serial.println ("will turn off charging after time up" );
if(millis()/1000 -Secs > Time1 ) // Seconds After which charging will turn off
{
Serial.println ("Turned Charging OFF ");
digitalWrite(ChrgS, LOW); // Turn OFF Charging
Charging = 0;
}
}
else // if charging is ON but voltage has not reached Cut OFF
{
Secs = millis()/1000; // Keep Timer1 updated
Serial.println("Battery is not fully charged yet ");
}
}
//##########################################
//######### TURN CHARGING ON ###############
//##########################################
else // Power is Available but Charging is OFF
{
if(Vin <= ChargeON ) // and if Voltage has dropped to ChargeON value
{
Serial.println ("Turned Charging ON ");
digitalWrite(ChrgS, HIGH); // Turn Charging ON
Charging = 1 ;
}
Secs = millis()/1000; // update timer 1 // keep updating it while charging is off
}
//##########################################
//######### TURN LOAD ON ###################
//##########################################
//###################################
if(!LoadConnect) // Power is Available but Load is OFF
{ //###################################
if(Vin >= LoadON) // if Voltage is above Load ON
{
if(millis()/1000 -SecsTwo > Time2 ) // Seconds After the Load ON Voltage is reached
{
Serial.println ("Turned Load ON ");
digitalWrite(LoadS, HIGH); // Turn Load ON
digitalWrite(StfdPin,LOW); // Keep Signal Pin OFF
LoadConnect = 1 ;
}
}
else
{
Serial.println("Waiting for Battery to Reach Load ON Voltage");
SecsTwo = millis()/1000; // Keep Timer Two Updated while Voltage Lower than Load ON
}
}
//#################################################
else // else if Power is Available and Load is Connected
{ //#################################################
PiUp = !digitalRead(PiStsPin); // Set Pi's Status
digitalWrite(StfdPin,LOW); // Turn Signal Pin LOW
if(!PiUp && Vin >= PiOFF) // IF Pi is NOT UP then
{
Serial.println("Pi signal ON Trying to Reset Pi");
if(millis()/1000 -SecsTwo > Time3) // Count till Time 3 // maybe it's booting up
{
Serial.println("Giving a Shock to Pi ");
digitalWrite(PiRst,HIGH); // Turn PiRst Pin ON
delay(500);
digitalWrite(PiRst,LOW); // Turn PiRst Pin OFF
SecsTwo = millis()/1000;
}
}
else // if power available and load connected and pi is up
{
SecsTwo = millis()/1000; // Keep Timer Two Updated while Load ON and PI is UP
digitalWrite(StfdPin,LOW); // keep signal pin off
Serial.println("OK ");
}
}
}
//##################################
//##### SERIAL PRINT STATUS ########
//##################################
if (Power)
{
Serial.println("POWER IS AVAILABLE");
if (Charging)
{
Serial.println ("Charging ON");
}
else
{
Serial.println ("Charging OFF");
}
}
else
{
Serial.println("NO POWER AVAILABLE");
}
if (LoadConnect)
{
Serial.println ("Load ON");
if(PiUp)
{
Serial.println("Pi is ON");
}
else
{
Serial.println("Pi Is OFF");
}
}
else
{
Serial.println ("Load OFF");
}
Serial.println("################");
//##################################
//### HTML WebPage #################
//##################################
sendHtml();
}
//####################################################################################
//##################### END OF LOOP ##################################################
//####################################################################################
//#################################
//#### Function for HTML ##########
//#################################
void sendHtml()
{
WiFiClient client = server.available();
if (client)
{
Serial.println("New client");
boolean blank_line = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (c == '\n' && blank_line)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><meta http-equiv='refresh' content='2'/></head><body><h1>ESP8266 Battery Manager Ver: 1.0</h1>");
client.println("<h2>AnalogIN :");
client.println(Val);
client.println("</h2>");
client.println("<h2>Vout :");
client.println(Vout);
client.println("V</h2>");
client.println("<h2>Battery Voltage :");
client.println(Vin);
client.println("V</h2>");
client.println("<br />");
client.println("<h1>Current States </h1>");
client.println("<br />");
client.println("<h3>Charging:>>> ");
if(Power)
{
if(Charging)
{
client.println("ON </h3>");
}
else
{
client.println("OFF </h3>");
}
}
else
{
client.println("NO POWER </h3>");
}
client.println("<h3>Load:>>>");
if(LoadConnect)
{
client.println("ON </h3>");
client.println("<h3>Pi Status:>>>");
if(PiUp)
{
client.println("ON </h3>");
}
else
{
client.println("OFF </h3>");
}
}
else
{
client.println("OFF </h3>");
}
client.println("<br />");
client.println("<br />");
client.println("<br />");
char LinkOne[]= "https://youtube.com/crazyguyofficial";
client.println("<h3><a href=");
client.print(LinkOne);
client.print(">CHANNEL</a></h3>");
char LinkTwo[]= "https://amkdiyprojects.blogspot.com";
client.println("<h3><a href=");
client.print(LinkTwo);
client.print(">BLOG</a></h3>");
client.println("</body></html>");
break;
}
if (c == '\n')
{
blank_line = true;
}
else if (c != '\r')
{
blank_line = false;
}
}
}
delay(1);
client.stop(); // closing the client connection
Serial.println("Client disconnected.");
}
}
Subscribe to:
Posts (Atom)