두개의 LED를 스마트폰에서 On, Off제어 합니다.
회로구성
준비물 : nodeMCU, LED 2개, 330옴 (2개) 점프선
A : LED 내부 작은 조각은 +입니다.
B : 저항 330옴 무극성으로 -라인에 연결합니다.
C : LED+라인과 D1핀을 연결합니다.
D : GND(-)을 -줄에 연결합니다.
E : 330 옴 저항을 연결합니다.
F : LED 작은 조각이 위로 향하도록 배치합니다.ㅈ
G :LED와 D2를 연결합니다.
아래 코드를 실행하고, 스마트폰, PC에서 시리얼 포트에 출력된 IP로 접속합니다.
#include <ESP8266WiFi.h>
const char* ssid = "IOT22";
const char* password = "zzz19540410";
WiFiServer server(80);
String header;
String D1State = "OFF";
String D2State = "OFF";
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
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.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
String currentLine = "";
currentTime = millis();
previousTime = currentTime;
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("GET /5/ON") >= 0) {
D1State = "ON";
digitalWrite(5, HIGH);
} else if (header.indexOf("GET /5/OFF") >= 0) {
D1State = "OFF";
digitalWrite(5, LOW);
}else if (header.indexOf("GET /4/ON") >= 0) {
D2State = "ON";
digitalWrite(4, HIGH);
} else if (header.indexOf("GET /4/OFF") >= 0) {
D2State = "OFF";
digitalWrite(4, LOW);
}
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #FF1064; border: none; color: white; padding: 10px 30px;");
client.println("text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #000000;}</style></head>");
client.println("<body><h1>ESP8266 Web Server</h1>");
client.println("<p>D1 : " + D1State + "</p>");
if (D1State == "OFF") {
client.println("<p><a href=\"/5/ON\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/5/OFF\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("<p>D2 : " + D2State + "</p>");
if (D2State == "OFF") {
client.println("<p><a href=\"/4/ON\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/4/OFF\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
'코딩 놀이' 카테고리의 다른 글
tag [ align] (0) | 2021.03.09 |
---|---|
HTML 버튼 (0) | 2021.03.08 |
Button(HTML) (0) | 2021.03.08 |
HTML 언어 구조 (0) | 2021.03.07 |
서버와 스마트폰 (0) | 2021.03.07 |