Untitled
unknown
plain_text
2 years ago
9.8 kB
14
Indexable
#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif9pt7b.h>
#include <DHT.h>
#include <ezBuzzer.h>
#include <ezButton.h>
#include <Stepper.h>
#include <Arduino.h>
#include <WiFi.h> // Thư viện WiFi để kết nối đến mạng WiFi
#include <Firebase_ESP_Client.h>
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
#define WIFI_SSID "Dam Nhau Khong"
#define WIFI_PASSWORD "00000000"
#define API_KEY "AIzaSyD9duipOozaL8On6HLOEkelZQd_A7sVbWA"
#define DATABASE_URL "https://g11smarthome-e2c2e-default-rtdb.firebaseio.com/"
const int stepsPerRevolution = 1000; // change this to fit the number of steps per revolution ULN2003 Motor Driver Pins
#define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters
#define BUTTON_PIN 23 // GPIO21 pin connected to button
// Variables will change:
int lastState = HIGH; // the previous state from the input pin
int currentState; // the current reading from the input pin
#define IN1 4
#define IN2 16
#define IN3 17
#define IN4 5
#define BUZZER_PIN 15
#define LED_PIN 2
#define AO_PIN 34 // ESP32's pin GPIO36 connected to AO pin of the MQ2 sensor
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define RELAY_PIN 35
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
#define DHT_SENSOR_PIN 19 // ESP32 pin GPIO21 connected to DHT11 sensor
#define DHT_SENSOR_TYPE DHT11
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
const long interval1 = 2000; // Khoảng thời gian cho cảm biến
const long interval2 = 2000; // Khoảng thời gian cho cảm biến
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] = {13, 27, 26, 25}; // GPIO18, GPIO5, GPIO17, GPIO16 connect to the row pins
byte pin_column[COLUMN_NUM] = {33, 32, 18, 0}; // GPIO4, GPIO0, GPIO2 connect to the column pins
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const String password_1 = "12345"; // change your password here
const String password_2 = "00000"; // change your password here
const String password_3 = "11111"; // change your password here
String input_password;
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
int count = 0;
bool signupOK = false;
void setup() {
Serial.begin(9600);
// set the speed at 5 rpm
myStepper.setSpeed(15);
dht_sensor.begin(); // initialize the DHT sensor
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("Man Hinh bi hong?"));
for (;;);
}
display.setFont(&FreeSerif9pt7b);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 20);
// Display static text
display.println("Nhap mat khau: ");
display.display();
}
void nhay(){
int t = 3;
while(t-->0){
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
delay(100);
}
}
void nhay1(){
int t = 20;
while(t-->0){
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
delay(100);
}
}
void actMoCua() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(1000);
}
void actDongCua(){
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(1000);
}
void turnOnFan(){
while(1){
digitalWrite(RELAY_PIN, HIGH); // turn on fan 10 seconds
delay(10000);
digitalWrite(RELAY_PIN, LOW); // turn off fan 10 seconds
delay(10000);
}
}
void DHT_sensor(){
float humi = dht_sensor.readHumidity();
float tempC = dht_sensor.readTemperature();
float tempF = dht_sensor.readTemperature(true);
if ( isnan(tempC) || isnan(tempF) || isnan(humi)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humi);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C ~ ");
Serial.print(tempF);
Serial.println("°F");
}
}
void connectFirebase(){
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)){
sendDataPrevMillis = millis();
// Write an Int number on the database path test/int
if (Firebase.RTDB.setInt(&fbdo, "test/int", count)){
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
count++;
// Write an Float number on the database path test/float
if (Firebase.RTDB.setFloat(&fbdo, "test/float", 0.01 + random(0,100))){
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
}
}
int t=1;
void loop() {
// connectFirebase();
unsigned long currentMillis = millis();
currentState = digitalRead(BUTTON_PIN);
if (lastState == HIGH && currentState == LOW && t == 1){
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
actMoCua();
t=2;
}
currentState = digitalRead(BUTTON_PIN);
if (lastState == HIGH && currentState == LOW && t == 2){
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
actDongCua();
t=1;
}
char key = keypad.getKey();
if(key == '#'&& t == 2) {
actDongCua();
t=1;
}
if (key){
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
delay(100);
if(key == '*') {
input_password = ""; // reset the input password
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 20);
// Display static text
display.println("Nhap mat khau: ");
display.display();
} else if(key == '#') {
if(input_password == password_1 || input_password == password_2 || input_password == password_3) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(1, 30);
display.println("The password is correct!");
digitalWrite(LED_PIN, HIGH);
display.display();
actMoCua();
nhay();
t=2;
while(1){
char key1 = keypad.getKey();
currentState = digitalRead(BUTTON_PIN);
if(key1=='#'){
actDongCua();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 20);
display.println("Nhap mat khau: ");
display.display();
t=1;
break;
}
if (lastState == HIGH && currentState == LOW && t == 2){
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
actDongCua();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 20);
display.println("Nhap mat khau: ");
display.display();
t=1;
break;
}
}
} else if(input_password.length() != 0) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 20);
display.println("The password is incorrect!");
display.display();
nhay1();
}
digitalWrite(LED_PIN, LOW);
input_password = ""; // reset the input password
} else {
input_password += key; // append new character to input password string
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 40);
display.println(input_password);
display.display();
}
}
if (currentMillis - previousMillis1 >= interval1) {
previousMillis1 = currentMillis;
DHT_sensor();
}
if (currentMillis - previousMillis2 >= interval2) {
previousMillis2 = currentMillis;
int gasValue = analogRead(AO_PIN);
if (isnan(gasValue)) {
Serial.println("Failed to read from gas sensor !");
return;
}
if(gasValue >= 1000){
ezBuzzer buzzer(BUZZER_PIN);
}
Serial.print("MQ2 sensor AO value: ");
Serial.println(gasValue);
}
}
Editor is loading...