/* #########################################################################
#################### Esp8266 Serial to WiFi Monitor #####################
########################## WEMOS D1 MINI ################################
############################### for #####################################
################### ARDUINO NANO SMART GROW CONTROLLER ##################
################# ##########################
############################ |WWWW| #####################################
############################ \O_O/ ######################################
# CAN BE USED AS A GENERAL PURPOSE MONITOR FOR ANYTHING WITH A TX PIN ##
############# AFTER SOME MODIFICATIONS ####################
*/
/* #########################################################################
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"
// Set Port forward from your router to this Webserver at port 80
// to monitor it from anywhere**
// This server is not secure
// controlling things should NEVER be done using
// an UN-encrypted webserver like this
// It's only good for Monitoring
// Subscribe to my channel for updates for this sketch
// and other sketches related to this project
// because i'll add more things to it gradually
#########################################################################
#########################################################################
*/
#include <ESP8266WiFi.h> // Version 1.0.0
#include <SoftwareSerial.h> // Version 1.0.0
const char* ssid = "SQUANCHY01";
const char* password ="c137gsasotf";
char Time[9]; // Character Arrays or C-strings
char Day; // size should be the total number of Chars to be stored +1 for null char
char Date[3];
char Mon[3];
char Year[5];
char Status[13];
char Temp1[6];
char Temp2[6];
char Hum[6];
unsigned long Secs = 0;
bool newData = 0;
bool err =1;
bool Lsts =0;
bool Csts =0;
bool Hsts =0;
bool Fsts =0;
bool Ssts =0;
bool DHTerr=0;
bool RTCerr=0;
WiFiServer server(80);
SoftwareSerial swSer(D1, D2, false, 256); // "swSer can be changed it will be the name for the Software Serial
// Software Serial Library Enables Esp8266's GPIO D1 and D2
// to be used as Rx and Tx pins RESPECTIVELY
// ONLY Rx is used and is connected to the Tx of a 5V Arduino Nano
void setup() { // using a Voltage Divider circuit 1K and 2K Resistors
// And Ground of esp to Ground of Arduino
Serial.begin(115200); // This is the baud rate for ESP's Serial at USB which can be viewed in Arduino IDE Serial monitor
swSer.begin(9600); // This is the baud rate for esps Software serial at pins D1 and D2 so it can communicate with arduino
// it's slow because of Arduino Nano's slow speed as compared to Esp
// can change the name swSer from above
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.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Waiting for the IP Add...");
delay(5000);
Serial.println(WiFi.localIP());
}
/*##############################################################################################
###############################################################################################
################ SETUP END ####################################################################
###############################################################################################
*/
/*##############################################################################################
###############################################################################################
################ LOOP BEGINS ##################################################################
###############################################################################################
*/
void loop()
{
char data[53];
byte i = 0 ;
byte x = 0 ;
if (swSer.available() > 0 )
{
char Z = swSer.read();
if(Z=='Z') // match first Character to check "validity"
{ // and this letter 'Z' is not stored in the array below
newData=1;
Secs=millis()/1000;
for(i= 0; i< 52; )
{
data[i] = swSer.read();
i++;
data[i] = '\0';
}
for(i=0; i<52;)
{ // serial print to arduino ide monitor via usb,all the chars stored in data array
Serial.print("char number: "); // and print char number the INDEX number starting from zero
// the actual first char is the Letter "Z' which is not stored
Serial.println(i);
Serial.println(data[i]);
i++;
}
}
else
{
err=1;
}
}
//############# GET TIME #######################################################################
if(newData)
{
// get time from the data array
for( i = 0; i < 8; )
{
Time[i] = data[i];
i++;
}
Serial.print("Time :");
Serial.println(Time); // serial print all the chars stored in the time array
//############# GET DAY OF THE WEEK #############################################################
if( data[8]=='W' )
{
err=0;
Day = data[9];
Serial.print("Day :");
Serial.println(Day); // serial print all the chars stored in the day array
}
else
{
err=1;
}
//############# GET DATE #######################################################################
if(data[10]=='D')
{
err=0;
x=0;
for( i = 11; i < 13; )
{
Date[x] = data[i];
i++;
x++;
}
Serial.print("Date :");
Serial.println(Date); // serial print all the chars stored in the Date array
}
else
{
err=1;
}
//############# GET MONTH #######################################################################
if(data[13]=='M')
{
err=0;
x=0;
for( i =14 ; i < 16; )
{
Mon[x] = data[i];
i++;
x++;
}
Serial.print("Month :");
Serial.println(Mon); // serial print all the chars stored in the month / mon array
}
else
{
err=1;
}
//############# GET YEAR #######################################################################
if(data[16]=='Y')
{
err=0;
x=0;
for( i = 17; i < 21; )
{
Year[x] = data[i];
i++;
x++;
}
Serial.print("Year :");
Serial.println(Year); // serial print all the chars stored in the Year array
}
else
{
err=1;
}
//############# GET STATUS #######################################################################
if(data[21]=='L')
{
err=0;
x=0;
for( i = 22; i < 34; )
{
Status[x] = data[i];
i++;
x++;
}
Serial.print("Status :");
Serial.println(Status); // serial print all the chars stored in the Status array
}
else
{
err=1;
}
//############# GET T1 ###########################################################################
if(data[34]=='t')
{
err=0;
x=0;
for( i = 35; i < 40; )
{
Temp1[x] = data[i];
i++;
x++;
}
Serial.print("Temp 1 :");
Serial.println(Temp1); // serial print all the chars stored in the temperature / temp1 array
}
else
{
err=1;
}
//############# GET Hum #######################################################################
if(data[40]=='h')
{
err=0;
x=0;
for( i = 41; i < 46; )
{
Hum[x] = data[i];
i++;
x++;
}
Serial.print("Humidity :");
Serial.println(Hum); // serial print all the chars stored in the Humidity / Hum array
}
else
{
err=1;
}
//############# GET T2 #######################################################################
if(data[46]=='r')
{
err=0;
x=0;
for( i = 47; i < 52; )
{
Temp2[x] = data[i];
i++;
x++;
}
Serial.print("Temp2 :");
Serial.println(Temp2); // serial print all the chars stored in time array
}
else
{
err=1;
}
newData=0;
}
setStatus();
sendHtml();
if(millis()/1000 -Secs > 30 )
{
Serial.println("Resetting Arrays");
Serial.println(Secs);
ResetArrays();
Secs=millis()/1000;
}
}
//####################################################################################
//##################### END OF LOOP #######################################
//####################################################################################
void setStatus()
{
if(!err)
{
Lsts=Status[0]-'0';
Csts=Status[2]-'0';
Hsts=Status[4]-'0';
Fsts=Status[6]-'0';
Ssts=Status[8]-'0';
DHTerr=Status[10]-'0';
RTCerr=Status[11]-'0';
}
}
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='5'/></head><body><h1>Esp8266 Serial to Wifi Monitor Ver: 1.0</h1>");
client.println("<h2>>>>> ---- Time :");
client.println(Time);
client.println("--<<< >>> ---- Day : ");
client.println(Day);
client.println("--<<< >>> ---- Date : ");
client.println(Date);
client.println("-");
client.println(Mon);
client.println("-");
client.println(Year);
client.println("---- <<<<<");
client.println("</h2>");
client.println("<br />");
client.println("<h2>Temp 1 :");
client.println(Temp1);
client.println("\260C</h2>");
client.println("<h2>Temp 2 :");
client.println(Temp2);
client.println("\260C</h2>");
client.println("<h2>Humidity :");
client.println(Hum);
client.println("%</h2>");
client.println("<br />");
client.println("<h1>Current States of Switches</h1>");
client.println("<br />");
client.println("<h3>LIGHT:>>> ");
if(!err)
{
if(Lsts)
{
client.println("ON </h3>");
}
else
{
client.println("OFF </h3>");
}
}
else
{
client.println("UNKNOWN </h3>");
}
client.println("<h3>COOLER:>>>");
if(!err)
{
if(Csts)
{
client.println("ON </h3>");
}
else
{
client.println("OFF </h3>");
}
}
else
{
client.println("UNKNOWN </h3>");
}
client.println("<h3>HEATER:>>>");
if(!err)
{
if(Hsts)
{
client.println("ON </h3>");
}
else
{
client.println("OFF </h3>");
}
}
else
{
client.println("UNKNOWN </h3>");
}
client.println("<h3>FAN:>>>");
if(!err)
{
if(Fsts)
{
client.println("ON </h3>");
}
else
{
client.println("OFF </h3>");
}
}
else
{
client.println("UNKNOWN </h3>");
}
client.println("<h3>HUMIDIFIER:>>>");
if(!err)
{
if(Ssts)
{
client.println("ON </h3>");
}
else
{
client.println("OFF </h3>");
}
}
else
{
client.println("UNKNOWN </h3>");
}
client.println("<br />");
client.println("<h4>DHT Err:>>>");
if(!err)
{
if(DHTerr)
{
client.println("TRUE </h4>");
}
else
{
client.println("FALSE </h4>");
}
}
else
{
client.println("UNKNOWN </h4>");
}
client.println("<h4>RTC Err:>>>");
if(!err)
{
if(RTCerr)
{
client.println("TRUE </h4>");
}
else
{
client.println("FALSE </h4>");
}
}
else
{
client.println("UNKNOWN </h4>");
}
client.println("<br />");
client.println("<h4>Serial Read Error:>>>");
if(err)
{
client.println("TRUE ----OH JEEZ </h4>");
}
else
{
client.println("FALSE </h4>");
}
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.");
}
}
void ResetArrays()
{
//Serial.flush();
err=1;
// data[0] ='\0';
Time[0] ='\0'; // Character Arrays or C-strings
Day ='\0'; // size should be the total number of Chars to be stored +1 for null char
Date[0] ='\0';
Mon[0] ='\0';
Year[0] ='\0';
Status[0] ='\0';
Temp1[0] ='\0';
Temp2[0] ='\0';
Hum[0] ='\0';
}
Do-it-yourself,DIY,How-To,Electronics,Arduino,Esp,Raspberry-Pi,Programming,Hacking Stuff,Gardening,Photography,Hobby,Fun
Showing posts with label relay. Show all posts
Showing posts with label relay. Show all posts
Esp8266 Wemos D1 Mini Serial to Wifi Html WebServer Monitor for Arduino Smart Grow Box Controller [OUT-DATED]
Labels:
arduino,
automation,
controller,
cool,
dht,
esp8266,
green house,
html,
humidity,
monitor,
oled,
plant,
relay,
relays,
rtc,
serial,
thermistor,
timer,
webserver.grow box,
wifi
D.I.Y. Arduino Timer Auto Lights Switch using arduino Nano,Ds3231rtc,relays v1.0 [OUT-DATED]
//*****************************************************************************************************************
//*****************************************************************************************************************
//************<<<< Arduino Nano, DS3231 Real Time Clock & Two Relay Module Lights Timer V1.0 >>>>******************
//*****************************************************************************************************************
//*****************************************************************************************************************
//UPDATED ON DEC 2017
//*****************************************************************************************************************
//*********************<<<< PLEASE VISIT / SUBSCRIBE MY YOUTUBE CHANNEL >>>>***************************************
//*********************<<<< Youtube.com/CrazyGuyOfficial >>>>*****************************************
//*****************************************************************************************************************
//**********************//**********************//**********************
// PINS AND CONNECTIONS
//**********************//**********************//**********************
const byte lightone = 5; // Light one relay (LOW Level Relay)
const byte lighttwo = 6 ; // Light Two Relay
const byte lightthree = 4 ; // Status Led // 13 for arduino builtin LED
// DS3231 RTC "SDA to A4", "SCL to A5" "Vcc to 5V arduino pin Gnd to Gnd"
//**********************//**********************//**********************
// Timer ON/OFF Time Settings
//**********************//**********************//**********************
const int OnTime = 14 ; // Hour when Light will turn ON (24 hr format NO zeros before single digits)
const int OffTime = 8 ; // Hour when Light will turn OFF (24 hr format NO zeros before single digits)
//*****************************************************************************************************************
bool LightStatus = 0 ;
//*****************************************************************************************************************
// INCLUDE LIBRARIES
//*****************************************************************************************************************
#include <Time.h> // Time Manipulation
#include <Wire.h>
#include <DS1307RTC.h> // DS1307 RTC
#include <TimeAlarms.h> // time alarm library //Changed Number of alarms " dtNBR_ALARMS to 30 "
//"BY EDITING TIMEALARM.H FILE IN LIBRARIES FOLDER.
//increasing this number increases size of the sketch too
#define DS1307_ID 0x68 //Address of the RTC
//*****************************************************************************************************************
// SETUP BEGINS
//*****************************************************************************************************************
void setup() {
pinMode(lightone, OUTPUT);
pinMode(lighttwo, OUTPUT);
pinMode(lightthree, OUTPUT);
// THIS WILL PREVENT STARTUP RELAY TRIGGERING PROBLEM
digitalWrite (lightone, HIGH);
digitalWrite (lighttwo, HIGH); // turn the relay "OFF" and turning the pin "LOW" turns the relay "ON"
//**********************//**********************//***********************//**********************
//Check to see if the RTC is present.if yes then Set the ARDUINO's INTERNAL clock accordingly
//**********************//**********************//***********************//**********************
// when using timer library the Arduino's "internal clock " is used for getting "current time"
// Arduino's internal clock is NOT so accurate and gets off time so we use time from RTC (which IS
//accurate atleast for our use) and we sync Arduino's internal Clock with the time from RTC and we
//have to do this periodically (atleast once in every 24 hrs )so our tasks can be completed according
//to the correct time // we can put the sync command in the Loop and Alarm Functions too
Wire.beginTransmission(DS1307_ID);
Wire.write((uint8_t)0x00);
setSyncProvider(RTC.get); //Yes it did, Sync the time from the RTC
setSyncInterval(60);
Serial.begin(9600);
//***********************//**********************//***********************//**********************
// TIMER ALARM THAT RUNS OVER AND OVER AFTER SECONDS (used for status led)
//***********************//**********************//***********************//**********************
Alarm.timerRepeat(10, Repeats); // timer for every 10 seconds blinking LIGHT (status LED)
}
//*****************************************************************************************************************
// SETUP END
//*****************************************************************************************************************
//***********************//**********************//***********************//**********************
//************// Function Called When Repeating Timer alarm triggers //*******************
//***********************//**********************//***********************//**********************
void Repeats() {
Serial.println("******10 second timer******");
digitalWrite(lightthree, HIGH);
Alarm.delay(30);
digitalWrite(lightthree, LOW);
digitalClockDisplay(); // Calls the function to Display the Clock IN SERIAL MONITOR
if (LightStatus ==1) {
Serial.println ("< Lights ON >");
}
else {
Serial.println ("< Lights OFF >");
}
}
//*****************************************************************************************************************
// LOOP BEGINS
//*****************************************************************************************************************
void loop() {
Alarm.delay (2000);
if (hour() == 0 && minute() == 0 && second() == 0) // at 00:00:00 MIDNIGHT UPDATE the Arduino's INTERNAL Clock
{
setSyncProvider(RTC.get); //It's time, sync the Arduino's INTERNAL Clock to the RTC
}
//**********************************************************
// Turn Lights On/Off
//**********************************************************
if (OffTime > OnTime) { // Same Day
if (hour() >= OnTime && hour () <= OffTime - 1) {
// if current hour is equal or greater than ON time
// AND equal to or less than OFF time minus 1 then turn the
// lights ON (by turning pins LOW because of using LOW level Relays)
// Minus 1 from OFF time because we are not using Minutes here
// For Example turn off time is "16" (4 PM) the hour number will be "16" from
// 16:00 till 16:59 (4:00 till 4:59)
digitalWrite(lightone, LOW);
Alarm.delay (1000);
digitalWrite(lighttwo, LOW);
LightStatus = 1 ;
}
else {
digitalWrite(lightone, HIGH);
digitalWrite(lighttwo, HIGH);
LightStatus = 0 ;
}
}
//**********************************************************
if (OffTime < OnTime) { // Different Day
if (hour() >= OnTime || hour () <= OffTime - 1) {
// if current hour is equal or greater than ON time
// OR equal to or less than OFF time minus 1 then turn the
// lights ON (by turning pins LOW because of using LOW level Relays)
// Minus 1 from OFF time because we are not using Minutes here
// For Example turn off time is "16" (4 PM) the hour number will be "16" from
// 16:00 till 16:59 (4:00 till 4:59)
// Minutes,Seconds even day,date,year can be used by adding code
digitalWrite(lightone, LOW);
Alarm.delay (1000);
digitalWrite(lighttwo, LOW);
LightStatus = 1;
}
else {
digitalWrite(lightone, HIGH);
digitalWrite(lighttwo, HIGH);
LightStatus = 0 ;
}
}
//*****************************************************************************************************************
// DAILY TIMED ALARMS THAT TRIGGER ON SPECIFIED TIMES
// these alarms will trigger once and will do "stuff" mentioned in the alarm functions at the end of the loop
// IF Alarm function is used to Turn Pins ON/OFF it will but there's a Reset/Power Dwn/Up cycle it will not work after
// the Specified Alarm time has passed and pin will go back to it's initial state untill alarm changes it again
//*****************************************************************************************************************
Alarm.alarmRepeat(3, 0, 0, ONAlarm); // daily Alarm 1 an alarm on ""03:00:00"" in the morning
Alarm.alarmRepeat(4, 0, 0, ONAlarm); // daily Alarm 2 an alarm on ""04:00:00"" in the morning
Alarm.alarmRepeat(5, 0, 0, ON2Alarm); // daily Alarm 3
Alarm.alarmRepeat(6, 0, 0, ONAlarm); // daily Alarm 4
Alarm.alarmRepeat(7, 0, 0, ONAlarm); // daily Alarm 5
Alarm.alarmRepeat(8, 0, 0, ON2Alarm); // daily Alarm 6
Alarm.alarmRepeat(9, 0, 0, ONAlarm); // daily Alarm 7
Alarm.alarmRepeat(10, 0, 0, ONAlarm); // daily Alarm 8
Alarm.alarmRepeat(11, 0, 0, ON2Alarm); // daily Alarm 9
Alarm.alarmRepeat(12, 0, 0, ONAlarm); // daily Alarm 10
Alarm.alarmRepeat(13, 0, 0, ONAlarm); // daily Alarm 11
Alarm.alarmRepeat(14, 0, 0, ON2Alarm); // daily Alarm 12
Alarm.alarmRepeat(15, 0, 0, ONAlarm); // daily Alarm 13
Alarm.alarmRepeat(16, 0, 0, ONAlarm); // daily Alarm 14
Alarm.alarmRepeat(17, 0, 0, ON2Alarm); // daily Alarm 15
Alarm.alarmRepeat(18, 0, 0, ONAlarm); // daily Alarm 16
Alarm.alarmRepeat(19, 0, 0, ONAlarm); // daily Alarm 17
Alarm.alarmRepeat(20, 0, 0, ON2Alarm); // daily Alarm 18
Alarm.alarmRepeat(21, 0, 0, OFFAlarm); // daily Alarm 19
Alarm.alarmRepeat(22, 0, 0, OFFAlarm); // daily Alarm 20
Alarm.alarmRepeat(23, 0, 0, OFF2Alarm); // daily Alarm 21
Alarm.alarmRepeat(0, 0, 0, OFFAlarm); // daily Alarm 22
Alarm.alarmRepeat(1, 0, 0, OFFAlarm); // daily Alarm 23
Alarm.alarmRepeat(2, 0, 0, OFF2Alarm); // daily Alarm 24
}
//**********************//**********************//**********************
// LOOP END
//**********************//**********************//**********************
//**********************//**********************//**********************//**********************
//Functions called when daily time specific alarms (above in the loop) triggers:
//***********************************************//**********************//**********************
void ONAlarm() {
}
void ON2Alarm() {
setSyncProvider(RTC.get); //It's time, sync the Arduino's INTERNAL Clock to the RTC
Serial.println("TIME SYNCHRONIZED");
}
void OFFAlarm() {
}
void OFF2Alarm() {
setSyncProvider(RTC.get); //It's time, sync the Arduino's INTERNAL Clock to the RTC
Serial.println("TIME SYNCHRONIZED");
}
//**********************//**********************//**********************//**********************
//Function for digital clock display (in the SERIAL MONITOR )
//**********************//**********************//**********************//**********************
void digitalClockDisplay() { // digital clock display of the time 24hr format
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println("");
Serial.println(dayStr(weekday()));
Serial.print(monthStr(month()));
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.println(year());
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
//***********************************************//**********************//**********************
// Un comment the function below and comment the above only one can be used in the code or change names
/*void digitalClockDisplay() //// digital clock display of the time 12hr format
{
Serial.print(hourFormat12());
printDigits(minute());
printDigits(second());
Serial.println("");
Serial.println(dayStr(weekday()));
Serial.print(monthStr(month()));
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.println(year());
//Alarm.delay(1000);
}
void printDigits(int digits)
{
Serial.print(":");
if (digits < 10) {
Serial.print('0');
}
Serial.print(digits);
}
*/
Subscribe to:
Posts (Atom)