#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>
//const int ledPin = 5;
//Time start Settings:
const int relay = 23;
const int relay2 = 26;
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.
#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 LED_PIN2 23
#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
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
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 dht(DHTPIN, DHTTYPE);
//const String command = "7898"; // change your password here
//String input_command;
//extern volatile unsigned long timer_millis;
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
// setup pin 5 as a digital output pin
pinMode (LED_PIN, OUTPUT);
pinMode(LED,OUTPUT);
pinMode(relay, OUTPUT);
pinMode(relay2, OUTPUT);
// ESP.restart(); /*ESP restart function*/
timeNow = millis()/1000;
dht.begin();
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(* resetFunc) (void) = 0; // declare reset fuction at address 0
void loop() {
char key = keypad.getKey(); /*Take input from keypad*/
// put your main code here, to run repeatedly:
timeNow = millis()/1000; // the number of milliseconds that have passed since boot
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;
//delay(500);{
//digitalWrite(LED,HIGH);
//delay(500);
//digitalWrite(LED,LOW);}
//read temperature and humidity
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
}
//Serial.println("Current Flowing");
// PARA MAPAIKAW ANG LED
if (t < 50) {
digitalWrite (LED_PIN, HIGH);
digitalWrite(relay, HIGH);
digitalWrite(relay2, LOW);
digitalWrite (LED, LOW);
display.setTextSize(1);
display.setCursor(27,70);
//display.println("TESTING!");
display.display();
} else if (t > 50 ) {
digitalWrite (LED_PIN, LOW);
digitalWrite (LED, HIGH);
digitalWrite(relay, LOW);
digitalWrite(relay2, HIGH);
}
if (t == 10) {
digitalWrite (LED_PIN1, HIGH);
display.setTextSize(1);
display.setCursor(27,70);
//display.println("TESTING ULI!");
display.display();
}else if (seconds > 10 ) {
digitalWrite (LED_PIN1, LOW);
}
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;
}
if (key) { /*If Key is pressed display the output*/
//Serial.print(key);
//display.clearDisplay();
display.setTextSize(1);
//display.setCursor(0,0);
//display.print(key);
display.display();
}
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
}
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 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();
}
Loading
ssd1306
ssd1306