2.2.3.3. CODE của hệ thống
-----------------------------------------------------------------------------------------------
#include <ESP8266WiFi.h> #include "DHT.h"
#define BLYNK_PRINT Serial #include <BlynkSimpleEsp8266.h> #include <SimpleTimer.h> // Pin #define DHTPIN D5 #define den D7 #define quat D1 #define GAS A0
// Su dung cam bien DHT11 #define DHTTYPE DHT11 // Thiet lap DHT
DHT dht(DHTPIN, DHTTYPE);
char auth[] = "cc61900784154f54b05ea96e07c2dc1f"; //Token cua blynk // Thong so WiFi nha ban
const char* ssid = "ALO TEA"; const char* pass = "09091994";
SimpleTimer timer; // Su dung timer // Tao server
WiFiServer server(80); void sendSensor() {
float h = dht.readHumidity(); //Doc gia tri do am float t = dht.readTemperature(); //Doc gia tri nhiet do float g = analogRead(GAS) ;
float nguoi = digitalRead(D6);
// Gan du lieu vao bien virtual de hien thi len blynk // Chi nen gan 10 bien tro xuong
delay(10); Blynk.virtualWrite(A0, T); Blynk.virtualWrite(V0, h); Blynk.virtualWrite(V1, t); } void setup() { // Mo Serial Serial.begin(115200); delay(10);
Blynk.begin(auth, ssid, pass); // Ket noi voi blynk dht.begin(); // Khoi tao DHT
timer.setInterval(4000L, sendSensor); //4s doc cam bien 1 lan // Khoi tao DHT dht.begin(); pinMode(D7, OUTPUT); digitalWrite(D0, 0); pinMode(D1, OUTPUT); digitalWrite(D1, 0);
Serial.println(); Serial.println();
Serial.print("Ket noi toi mang "); Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) { delay(500);
Serial.print("."); }
Serial.println("");
Serial.println("Da ket noi WiFi");
// Khoi dong server server.begin();
Serial.println("Khoi dong Server"); // In ra dia chi IP Serial.println(WiFi.localIP()); } void loop() { float g = analogRead(GAS) ; float nguoi = digitalRead(D6); if (g>300){ digitalWrite(D8, HIGH); } if (nguoi == HIGH){ digitalWrite(D3,HIGH); }
Blynk.run(); // Chay Blynk timer.run(); // Chay SimpleTimer
// Kiem tra khi co client ket noi
WiFiClient client = server.available(); if (!client) {
return; }
// Doi client gui ket noi
Serial.println("Co mot client moi ket noi xem du lieu"); while(!client.available()){ delay(1); } // Doc do am float h = dht.readHumidity(); // Doc nhiet do o do C float t = dht.readTemperature(); // Doc yeu cau tu client
String req = client.readStringUntil('\r'); Serial.println(req);
client.flush();
// Kiem tra yeu cau la gi if (req.indexOf("/on") != -1){ digitalWrite(D0, 1); } else if (req.indexOf("/off") != -1) { digitalWrite(D0, 0); } if (req.indexOf("/on1") != -1){ digitalWrite(D1, 1); } else if (req.indexOf("/off1") != -1) { digitalWrite(D1, 0); }
// Chuan bi tao web de phan hoi
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; s += "<head>";
s += "<meta name=\"viewport\" content=\"width=device-width, initial- scale=1\">";
s += "<meta http-equiv=\"refresh\" content=\"60\" />";
s += "<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>"; s += "<link rel=\"stylesheet\"
href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">"; s += "<style>body{font-size: 24px;} .voffset {margin-top: 30px;}</style>"; s += "</head>";
s += "<h1>Dieu khien den</h1>"; s += "<div class=\"row\">";
s += "<div class=\"col-md-2\"><input class=\"btn btn-block btn-lg btn- primary\" type=\"button\" value=\"On\" onclick=\"on()\"></div>";
s += "<div class=\"col-md-2\"><input class=\"btn btn-block btn-lg btn- danger\" type=\"button\" value=\"Off\" onclick=\"off()\"></div>";
s += "</div></div>";
s += "<script>function on() {$.get(\"/on\");}</script>"; s += "<script>function off() {$.get(\"/off\");}</script>"; s += "<div class=\"container\">";
s += "<h1>Dieu khien quat</h1>"; s += "<div class=\"row\">";
s += "<div class=\"col-md-2\"><input class=\"btn btn-block btn-lg btn- primary\" type=\"button\" value=\"On1\" onclick=\"on1()\"></div>";
s += "<div class=\"col-md-2\"><input class=\"btn btn-block btn-lg btn- danger\" type=\"button\" value=\"Off1\" onclick=\"off1()\"></div>";
s += "</div></div>";
s += "<script>function on1() {$.get(\"/on1\");}</script>"; s += "<script>function off1() {$.get(\"/off1\");}</script>";
s += "<div class=\"container\">";
s += "<h1>Theo doi nhiet do va do am</h1>"; s += "<div class=\"row voffset\">";
s += "<div class=\"col-md-3\">Nhiet do: </div><div class=\"col-md-3\">" + String(t) + "</div>";
s += "<div class=\"col-md-3\">Do am: </div><div class=\"col-md-3\">" + String(h) + "</div>";
s += "</div>";
s += "<div class=\"container\">";
s += "<h1>Theo doi khi Gas va canh bao trom</h1>"; s += "<div class=\"row voffset\">";
s += "<div class=\"col-md-3\">GAS: </div><div class=\"col-md-3\">" + String(g) + "</div>";
s += "<div class=\"col-md-3\">Nguoi: </div><div class=\"col-md-3\">" + String(nguoi) + "</div>";
s += "</div>";
// Gui phan hoi toi client (o day la giao dien web) client.print(s);
Serial.println("Client da thoat");
// Sau khi nhan duoc thong tin thi se tu dong ngat ket noi }
------------------------------------------------------------------------------------------------
2.2.4. Thiết kế phần cứng 2.2.4.1. Sơ đồ nguyên lý 2.2.4.1. Sơ đồ nguyên lý
Khối chấp hành (khối relay)