// #include "EEPROM.h"
//#include <Wire.h>
//#include "RTClib.h"
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
#include <ESP32Ping.h>
#include <ArduinoJson.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebSrv.h>
#include <AsyncElegantOTA.h>
#include <Arduino.h>
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <IRutils.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <FS.h>
#include <SD.h>
#include <TFT_eSPI.h>
#include "NotoSansBold15.h"
#pragma region Pin defenitions
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 160 // OLED display height, in pixels
#define VPIN_BUTTON_1 V1 // Virtual buttons for Blynk app
#define VPIN_BUTTON_2 V2
#define VPIN_BUTTON_3 V3
#define VPIN_BUTTON_4 V4
#define VPIN_BUTTON_C V5
// #define Display_LED 1
#define IR_RECV_PIN 35 // connected to GPIO pin 35 ( IR receiver pin)
const uint16_t kIrLedPin = 25; // GPIO pin 25 is transmitter(IR LED connected pin)
// #define kIrLedPin 25
#define ZeroWatt 2 // connected to GPIO pin 2
#define MainLight 27 // connected to GPIO pin 27
#define Fan_Low 26 // connected to GPIO pin 17
#define Fan_Mid 32 // connected to GPIO pin 32
#define Fan_Full 33 // connected to GPIO pin 33
#define Fan_On_Off 16 // connected to GPIO pin 16
#define IR_Status_Led 0 // connected to GPIO pin 0 (IR Receiver acknowledgement)
#define Buzzer 17 // connected to GPIO pin 33
#define ONE_WIRE_BUS 4 // Data wire is plugged into port 4
// #define MISO 19// #define SCK 18// #define MOSI 23// #define CS 5
#pragma endregion Pin defenitions
#pragma region Declarations and variables initialisation
char Time[] = "TIME:00:00:00";
char Date[] = "DATE:00/00/2000";
#define AA_FONT_SMALL NotoSansBold15
byte last_second, second_, minute_, hour_, day_, month_;
int year_;
const char *remote_host = "www.google.com";
const char *ssid = "Kichu_WiFi";
const char *pass = "86100069";
const char *auth = "ZQurhaoUeGFofmQTBuDW3VPA2TeLXWM7";
IPAddress local_IP(192, 168, 0, 199); // Set your Static IP address
IPAddress gateway(192, 168, 0, 1); // Set your Gateway IP address
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); // optional
IPAddress secondaryDNS(8, 8, 4, 4); // optional
const char *PARAM_INPUT_1 = "Hours";
const char *PARAM_INPUT_2 = "Minutes";
const char *PARAM_INPUT_3 = "AlarmFreq";
const char *PARAM_INPUT_4 = "Alarm_Flag";
const char *PARAM_INPUT_5 = "Lights";
const char *PARAM_INPUT_6 = "FanSpeed";
String Saved_Data = "5:30";
#define EEPROM_SIZE 32
int Repeat_Flag = 0;
int Alarm_Flag = 0;
int Hours = 1;
int Minutes = 30;
int ShDn_Hours = 0;
int ShDn_Minutes = 0;
bool Alarm_Active_flag = false;
bool Alarm_Armed = false;
bool RTC_Initialize_Error_flag = false;
bool Display_Initialize_Error_flag = false;
bool Alarm_Snoozed = false;
unsigned long last_Millis = 0; //
unsigned long Current_Millis = 0; //
unsigned long prev_Timer_Millis = 0; // store the last time the LED was updated
long Snooz_Interval = 0;
bool Alarm_Stop_Flagged = false; //
bool Show_Time = false;
bool isDisplayVisible = 1;
int Fan_speed = 0; // fan speed up/down variable
int CutOff_Temp = 20;
int Mode = 0; // Default mode is A/c, Fan and lights
int lastMode = 0; //
bool toggleState_1 = LOW; // variable to remember the toggle state for relay 1
bool toggleState_2 = LOW; // variable to remember the toggle state for relay 2
bool toggleState_3 = LOW; // variable to remember the toggle state for relay 3
bool wifiFlag = true;
bool Fan_Mid_status = LOW;
unsigned long prev_deb_Millis = 0;
unsigned long prev_disp_Millis = 0;
unsigned long current_Millis;
long interval;
bool debounce_waiting = false;
bool Display_Hold = false;
TFT_eSPI tft = TFT_eSPI(); // Initialize the TFT display
RTC_DS1307 RTC; // RTC instance initialization
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices // OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
// device address of the temp sensor DS18B20
DeviceAddress Sensor1 = {0x28, 0xFF, 0x65, 0x96, 0x62, 0x15, 0x02, 0x26};
AsyncWebServer server(80);
IRsend irsend(kIrLedPin);
IRrecv irrecv(IR_RECV_PIN);
decode_results results;
#pragma endregion Declarations and variables initialisation
void setup()
{
Serial.begin(115200); // initialize the serial communication for debugging
digitalWrite(15, HIGH); // TFT screen chip de-select
digitalWrite(5, HIGH); // SD card chips de-select
setup_SD_Card(); // further set up of SD card
setup_TFT_Display(); // initialize the display
pinMode(ZeroWatt, OUTPUT);
pinMode(MainLight, OUTPUT);
pinMode(Fan_On_Off, OUTPUT);
pinMode(Fan_Low, OUTPUT);
pinMode(Fan_Mid, OUTPUT);
pinMode(Fan_Full, OUTPUT);
pinMode(Buzzer, OUTPUT);
pinMode(IR_Status_Led, OUTPUT);
/* digitalWrite(IR_Status_Led, LOW);
// digitalWrite(Buzzer, HIGH);
// delay(500);
// digitalWrite(Buzzer, LOW);
// digitalWrite(4, HIGH);
// delay(800);
// digitalWrite(4, LOW);
*/
sensors.begin(); // start polling temp sensor
sensors.setResolution(9); // lowest resolution of 0.5° to make polling fast
irrecv.enableIRIn(); // Enabling IR sensor
irsend.begin(); // Start up the IR sender.
setup_wifi(); // setup the wifi network with static IP address
Blynk.config(auth); // initialize Blynk with authentication
delay(250);
if (!RTC.begin()) // initialize DS 1307 RTC clock module
{
Serial.println("Couldn't find RTC");
RTC_Initialize_Error_flag = true;
}
Get_NTP_Time(); // start NTP time clent and get time from NTP server
if (!EEPROM.begin(EEPROM_SIZE))
{
Serial.println("failed to init EEPROM");
}
Read_Time();
// setup_WebServer();
setup_Old_WebServer();
}
void loop()
{
if (Mode != lastMode)
{
switch (Mode)
{
case 0:
Serial.println("Mode: 0");
break;
case 1: // A/c related info
{
Serial.println("Mode: 1");
tft.fillScreen(0x001F); // YELLOW
String tmp = Check_Temp();
Display_Text("RoomTemp:" + tmp, 0);
Serial.println("Room Tmp:" + tmp);
lastMode = Mode;
}
break;
case 2: // Alarm info
{
// display.clearDisplay();
Serial.println("Mode: 2");
tft.fillScreen(0x001F); // YELLOW
String sTime1 = Check_Time();
Serial.println("Time= " + sTime1);
Display_Text(sTime1, 0);
String sDate1 = Check_Date();
Serial.println("Date =" + sDate1);
Display_Text(sDate1, 20);
Serial.println("Alarm set time=" + Saved_Data);
Display_Text(Saved_Data, 40);
lastMode = Mode;
}
break;
case 3: // Network status
{
// display.clearDisplay();
Serial.println("Mode: 3");
tft.fillScreen(0x001F); // YELLOW
Blynk_Status();
WiFi_Status();
Net_Status();
lastMode = Mode;
}
break;
default:
Serial.println("Mode: " + Mode);
break;
}
}
ir_remote(); // IR remote Control
Blynk.run(); // blynk control
Check_Activate_Buzzer();
current_Millis = millis();
if (current_Millis - prev_deb_Millis >= 150)
debounce_waiting = false;
if (current_Millis - prev_disp_Millis >= 5000)
Display_Hold = false;
if (current_Millis - prev_Timer_Millis >= 3000)
{
prev_Timer_Millis = current_Millis; // save the last time the LED was updated
}
// Check_Temp();
// Display_Time();
}