Điều khiển thiết bị điện bằng Wifi ( ESP8266 -12E) và App MIT Invertor
Phần mền cần chuẩn bị
- Phần mềm IDE.
Phần cứng cần chuẩn bị:
- 1 ESP8266 12 HOẶC ESP8266 12E.
- 1 Breadboard.
- 1 Điện thoại sử dụng hệ điều hành android.
- Dây test board.
- 4 Led.
- 4 Trở 220 Ohm.
Lắp mạch:
Nạp chương trình cho esp8266V12E.
Trước khi nạp chương trình cho esp8266V12E bằng arduino IDE các bạn cần cài thêm thư viện ESP8266 cho IDE nhé xem tại links sau nhé.
https://tdhshop.com.vn/lap-trinh-esp8266-bang-phan-mem-arduino-ide-1
Code chương trình:
#include <ESP8266WiFi.h>
const char* ssid = "Truong Phi"; //Tên wifi nhà mình
const char* password = "xxxxxxxxx"; // pass
int R1=D1;
int R2=D2;
int R3=D3;
int R4=D4;
WiFiServer server(80); //port is 80.
void setup() {
Serial.begin(115200);
delay(10);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(R3, OUTPUT);
pinMode(R4, OUTPUT);
digitalWrite(R1,HIGH);
digitalWrite(R2,HIGH);
digitalWrite(R3,HIGH);
digitalWrite(R4,HIGH);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP()); //Gets the WiFi shield's IP address and Print the IP address of serial monitor
Serial.println("/");
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("new client");
while(!client.available()){
delay(1);
}
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
if (request.indexOf("/Relay1On") != -1) {
digitalWrite(R1,LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 1 is ON");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/Relay1Off") != -1) {
digitalWrite(R1, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 1 is OFF");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/Relay2On") != -1) {
digitalWrite(R2,LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 2 is ON");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/Relay2Off") != -1) {
digitalWrite(R2, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 2 is OFF");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/Relay3On") != -1) {
digitalWrite(R3,LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 3 is ON");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/Relay3Off") != -1) {
digitalWrite(R3, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 3 is OFF");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/Relay4On") != -1) {
digitalWrite(R4,LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 4 is ON");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/Relay4Off") != -1) {
digitalWrite(R4, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 4 is OFF");
client.println("</html>");
client.stop();
delay(1);
}
}
Sau khi nạp code cho ESP xong các bạn mở cổng serial monitor lên nhé để biết được địa chỉ ip của esp nhé:
App cho điện thoại:
Các bạn tải app tại link sau nhé.
https://drive.google.com/open?id=1t6tubf8ASqQ9JOcGVc3Hgj65xdhgszPp
Facebook: https://www.facebook.com/profile.php?id=100010302510283
Sđt: 0395304802
Email: tdhshop1991@gmail.com
Bình luận(1)