Code

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

#include <WiFi.

h>
#include <HCSR04.h>
#include "SSD1306.h"
#include <DHT.h>
#include <Wire.h>
#include <FastLED.h>
#include <ESP32Servo.h>

#define led1 12
#define led2 13
#define led3 3
#define led4 19
#define beep 26
#define LED_PIN 32
#define NUM_LEDS 30

Servo myservo;

CRGB leds[NUM_LEDS];

#define but 0
#define gas 23
#define Sensor_Light 5
#define Sensor_Pir 4
#define Trig 17
#define Echo 22
#define DHTPIN 27
#define DHTTYPE DHT11 // DHT 11

char hum[ 16 ] = { 0 };
char temp[ 16 ] = { 0 };

DHT dht(DHTPIN, DHTTYPE);

SSD1306 display(0x3c, 13, 14);


const char* ssid = "ESP32";
const char* password = "xin5nghinn";

WiFiServer server(80);

void setup()
{
dht.begin();
Serial.begin(9600); //Default Baud Rate for NodeMCU
delay(10);
pinMode(Sensor_Pir, INPUT);
pinMode(Sensor_Light, INPUT);
pinMode(but,INPUT_PULLUP);
pinMode(gas,INPUT);

pinMode(Echo,INPUT);
pinMode(Trig,OUTPUT);

myservo.attach(18);

pinMode(led1, OUTPUT); // Connect Relay to NodeMCU's D4 Pin


pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(beep, OUTPUT);

digitalWrite(led1, 0);
digitalWrite(led2, 0);
digitalWrite(led3, 0);
digitalWrite(led4, 0);

digitalWrite(Sensor_Light, 0);

FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

display.init();
display.flipScreenVertically();
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.display();

// ket noi mang Wifi

Serial.println();
Serial.println();
Serial.print("Connecting to ");
display.drawString(0,0,"Connecting to");
display.display();
Serial.println(ssid);
display.drawString(0,18,"ssid");
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
display.clear();
display.drawString(0,18,".");
display.display();
}
Serial.println("");
Serial.println("WiFi connected");
display.drawString(0,36," Wifi connected");
display.display();
// Start the server
server.begin();
Serial.println("Server started");
display.clear();
display.drawString(0,36," Sever started");
display.display();
// hien thi IP address
Serial.println("IP Adress:");
display.clear();
display.drawString(5,0,"IP Adress :");
//display.display();
Serial.println(WiFi.localIP());
//display.clear();
display.drawString(5,18," "+ String(WiFi.localIP()));
display.display();
delay(5000);
}
void loop()
{
float t = dht.readTemperature(); //Đoc nhiet do

Serial.print(F("Nhiet do: ")); //Hien thi len Serisl


Serial.print(t);
Serial.print(F("°C"));
Serial.println(F(" "));
display.clear();
display.drawString(0,18,"Nhiet do: " + String(t)+ "°C"); //Hien thi len OLED

float h = dht.readHumidity(); //Doc do am

Serial.print(F("Do am: ")); //Hien thi len Serial


Serial.print(h);
Serial.print(F("%"));
Serial.println(F(" "));

display.drawString(0,36, "Do am: " + String(h)+ "%"); //Hien thi len OLED
display.display();
display.drawString(0, 0, "Nhom 2 - K56DTVT");
display.display();
unsigned long duration; // biến đo thời gian
int distance; // biến lưu khoảng cách

/* Phát xung từ chân trig */


digitalWrite(Trig,0); // tắt chân trig
delayMicroseconds(2);
digitalWrite(Trig,1); // phát xung từ chân trig
delayMicroseconds(5); // xung có độ dài 5 microSeconds
digitalWrite(Trig,0); // tắt chân trig

/* Tính toán thời gian */


// Đo độ rộng xung HIGH ở chân echo.
duration = pulseIn(Echo,HIGH);
// Tính khoảng cách đến vật.
distance = int(duration/2/29.412);

/* In kết quả ra Serial Monitor */


Serial.print("khoang cach: ");
Serial.print(distance);
Serial.println("cm");
delay(200);
if (distance <= 5)
{
myservo.write(0);
for (int i = 0; i <= 29; i++)
{
leds[i] = CRGB ( 0, 0, 255);
FastLED.show();
delay(40);
}
for (int i = 29; i >= 0; i--)
{
leds[i] = CRGB ( 255, 0, 0);
FastLED.show();
delay(40);
}
for (int i = 0; i <= 29; i++)
{
leds[i] = CRGB ( 0, 255, 0);
FastLED.show();
delay(40);
}
for (int i = 0; i <= 29; i++)
{
leds[i] = CRGB ( 255, 255, 0);
FastLED.show();
delay(40);
}
for (int i = 29; i >= 0; i--)
{
leds[i] = CRGB ( 0, 255, 255);
FastLED.show();
delay(40);
}
delay(200);
}
else
{
myservo.write(90);
for (int i = 29; i >= 0; i--)
{
leds[i] = CRGB ( 0, 0, 0);
FastLED.show();
delay(40);
}
}

{
int irValue1 = digitalRead(Sensor_Light);
if (irValue1 == LOW)
{
digitalWrite (led2, LOW);
}
else
{
digitalWrite (led2, HIGH);
}
}
// Cảm biến hồng ngoại
{
int irValue = digitalRead(Sensor_Pir);
if (irValue == LOW)
{
myservo.write(90);
delay(100);
}
else
{
myservo.write(0);
delay(5000);
}

}
// Check if a client has connected
WiFiClient client = server.available();
if (!client)
{
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
int val;
if (req.indexOf("/gpio/0") != -1)
val = 0;
else if (req.indexOf("/gpio/1") != -1)
val = 1;
else
{
Serial.println("invalid request");
client.stop();
return;
}

// Set GPIO2 according to the request


digitalWrite(19, val);
client.flush();

String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\


n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>";
}

You might also like