#include <Keypad.h> /*Included keypad library*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <SPI.h>
#include <DHT.h>
#define DHTPIN 27
#define OLED_RESET 4
#define DHTTYPE DHT22
unsigned long timeNow = 0;
unsigned long timeLast = 0;
#define LED_PIN 4
#define LED_PIN1 2
#define ROW_NUM 4 /*Define keypad Rows*/
#define COLUMN_NUM 4 /*Define keypad Columns*/
#define LED 2
//oled screen
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//RELAYS:
const int relay = 23;
const int relay2 = 26;
//Time settings
int startingHour = 0; // set your starting hour here, not below at int hour. This ensures accurate daily correction of time
int seconds = 0;
int minutes = 0;
int hours = startingHour;
int days = 0;
//Accuracy settings
int dailyErrorFast = 0; // set the average number of milliseconds your microcontroller's time is fast on a daily basis
int dailyErrorBehind = 0; // set the average number of milliseconds your microcontroller's time is behind on a daily basis
int correctedToday = 1; // do not change this variable, one means that the time has already been corrected today for the error in your boards crystal. This is true for the first day because you just set the time when you uploaded the sketch.
// oled screen
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Keypad Declaration
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {21, 19, 18, 5}; /*Initialized ESP32 Pins for Rows*/
byte pin_column[COLUMN_NUM] = {12, 13, 14, 15}; /*Initialized ESP32 Pins for Columns*/
/*Function for keypad*/
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
// dht sensor
DHT dht(DHTPIN, DHTTYPE);
float t = dht.readTemperature();
float h = dht.readHumidity();
// sample code for command in keyboard
//const String command = "7898"; // change your password here
//String input_command;
//extern volatile unsigned long timer_millis;
// Function Declarations
void setup();
void setupPins();
void setupDisplay();
void checkTempHumi();
void switchRelays();
//void displayTempHumi();
void switchLed();
void updateClock();
void keypadcom();
void displayclock();
void loop();
void setup() {
Serial.begin(9600); /*Baud Rate for Serial Communication*/
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) // init done // Show image buffer on the display hardware.
//input_command.reserve(32); // maximum input characters is 33, change if needed
//setupDisplay();
setupPins();
dht.begin();
setupDisplay();
}
void loop() {
//displayclock();
displayclock();
display.clearDisplay();
checkTempHumi();
//setupDisplay();
updateClock();
setupPins();
//checkTempHumi();
switchRelays();
switchLed();
updateClock();
keypadcom();
}
void setupPins() {
pinMode (LED_PIN, OUTPUT);
pinMode(LED,OUTPUT);
pinMode(relay, OUTPUT);
pinMode(relay2, OUTPUT);
}
void setupDisplay() {
display.display(); //delay(2000);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 0);
//display.clearDisplay();
//delay(2000);
//display.clearDisplay();
display.setTextColor(WHITE);
// invert the display
display.invertDisplay(true);
display.invertDisplay(false);
display.display();
//display.clearDisplay();
}
void checkTempHumi(){
// float t = dht.readTemperature();
// float h = dht.readHumidity();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
}
// display temperature
display.setTextSize(1);
display.setCursor(10,0);
display.print("Temperature: ");
display.setTextSize(1);
display.setCursor(25,12);
display.print(t);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(1);
display.print("C");
// display humidity
display.setTextSize(1);
display.setCursor(10, 22);
display.print("Humidity: ");
display.setTextSize(1);
display.setCursor(25, 32);
display.print(h);
display.print(" %");
display.display();
}
void switchRelays() {
if (t < 50) {
digitalWrite(relay, HIGH);
digitalWrite(relay2, LOW);
} else if (t > 50) {
digitalWrite(relay, LOW);
digitalWrite(relay2, HIGH);
}
}
//void displayTempHumi() {
//}
void switchLed(){
if (t < 50) {
digitalWrite (LED_PIN, HIGH);
digitalWrite (LED, LOW);
} else if (t > 50 ) {
digitalWrite (LED_PIN, LOW);
digitalWrite (LED, HIGH);
}
}
void updateClock() {
timeNow = millis()/1000;
seconds = timeNow - timeLast;//the number of seconds that have passed since the last time 60 seconds was reached.
unsigned long currentMillis = millis();
unsigned long ET = millis() - timeNow;
int elapsedTime = ET/1000;
if (seconds == 60) {
timeLast = timeNow;
minutes = minutes + 1;
}
//if one minute has passed, start counting milliseconds from zero again and add one minute to the clock.
if (minutes == 60){
minutes = 0;
hours = hours + 1;
}
// if one hour has passed, start counting minutes from zero and add one hour to the clock
if (hours == 24){
hours = 0;
days = days + 1;
}
//if 24 hours have passed , add one day
if (hours ==(24 - startingHour) && correctedToday == 0){
delay(dailyErrorFast*1000);
seconds = seconds + dailyErrorBehind;
correctedToday = 1;
}
//every time 24 hours have passed since the initial starting time and it has not been reset this day before, add milliseconds or delay the progran with some milliseconds.
//Change these varialbes according to the error of your board.
// The only way to find out how far off your boards internal clock is, is by uploading this sketch at exactly the same time as the real time, letting it run for a few days
// and then determine how many seconds slow/fast your boards internal clock is on a daily average. (24 hours).
if (hours == 24 - startingHour + 2) {
correctedToday = 0;
}
}
void keypadcom() {
char key = keypad.getKey(); /*Take input from keypad*/
if (key) {
//Serial.println(key);
if (key == '*') {
//ESP.restart();
exit(0);
//startTime = millis()/1000;
//erial.println(elapsedTime/1000);
//ESP.restart(); // clear input password
} else if (key == '#') {
ESP.restart();
// if (command == input_command) {
//Serial.println("test1");
// DO YOUR WORK HERE
} //else {
//Serial.println("test2");
// }
// input_command = ""; // clear input command
// } else {
// input_command += key; // append new character to input password string
}
}
void displayclock() {
//display.clearDisplay();
display.setTextSize(1);
display.setCursor(10,41);
display.println("Running Time:");
// timer
display.setTextSize(1);
display.setCursor(27,50);
display.println(days);
display.setTextSize(1);
display.setCursor(37, 50);
display.println("-");
display.setTextSize(1);
display.setCursor(47, 50);
display.println(hours);
display.setTextSize(1);
display.setCursor(57, 50);
display.println(":");
display.setTextSize(1);
display.setCursor(67, 50);
display.println(minutes);
display.setTextSize(1);
display.setCursor(77, 50);
display.println(":");
display.setTextSize(1);
display.setCursor(87,50);
display.println(seconds);
display.display();
}