'LED'에 해당되는 글 1건

  1. 2020.11.15 NodeMCU(ESP-12E/ESP8266) LED Control Server Programming
반응형

NodeMCU(ESP-12E/ESP8266)를 이용해 LED를 제어하는 서버를 만들어 보자.


2020/03/10 - [Raspberry Pi & Arduino] - How to program ESP32-CAM using Arduino UNO - 아두이노로 ESP32-CAM 프로그래밍 하기


NodeMCU를 준비한다.


ESP8266모듈이 내장된 NodeMCU를 사용하기 위해 'Preferences - Additional Boards ManagerURLs:'에 http://arduino.esp8266.com/stable/package_esp8266com_index.json을 입력한다.


Boards Manager에서 esp를 검색하고 esp8266을 설치한다.


Tools - Board에서 NodeMCU 1.0 (ESP-12E Module)을 선택한다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <ESP8266WiFi.h>
 
const char* ssid = "Your ssid";
const char* password = "Your password";
 
WiFiServer server(80);
WiFiClient client;
String request;
int status = LOW;
 
void setup() {
  Serial.begin(9600);
 
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
 
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
  // Tells the server to begin listening for incoming connections.
 
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected!!");
  server.begin();
  Serial.println("Server started.");
 
  Serial.print("Use this URL to connect to the server: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
 
void loop() {    
  client = server.available();
  // Gets a client that is connected to the server and has data available for reading.
  // The connection persists when the returned client object goes out of scope;
  // you can close it by calling client.stop().
  if (!client) {
    delay(100);
    
    return;
  }
  
  Serial.println("New client connected.");    
  
  while (!client.available()) {
    // Returns the number of bytes available for reading (that is, the amount of data
    // that has been written to the client by the server it is connected to).
    delay(100);
  }
 
  request = client.readStringUntil('\r');
  Serial.print("Request from the client: ");
  Serial.println(request);
  client.flush();
 
  if (request.indexOf("LED=ON"!= -1) {
    digitalWrite(LED_BUILTIN, HIGH);
    status = HIGH;
  } else if (request.indexOf("LED=OFF"!= -1) {
    digitalWrite(LED_BUILTIN, LOW);
    status = LOW;
  }
 
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("");
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.print("LED is turned ");
  if (status)
    client.print("On");
  else
    client.print("Off");
  client.println("<br><br>");
  client.println("<a href=\"/LED=ON\" title=\"Turn the LED on\"><button>Turn on</button></a>");
  client.println("<a href=\"/LED=OFF\" title=\"Turn the LED off\"><button>Turn off</button></a>");
  client.println("</html>");  
 
  //client.stop();
  // client.stop()을 호출하면 제대로 작동하지 않는다.
  Serial.println("Client disconnected!!");
  delay(100);
}


위 소스를 컴파일하고 NodeMCU에 업로드한다.


시리얼 모니터를 확인하면 와이파이에 연결되고 URL이 표시된다.


스마트폰이나 컴퓨터로 URL에 접속하면 위와 같은 화면이 표시된다.


'Turn on'버튼을 클릭하면 서버로 URL/LED=ON 요청을 보내고 서버에서는 LED=ON 문자열을 감지해 NodeMCU의 built-in LED를 켠다. (실제 built-in LED의 동작은 반대로 된다. LED가 꺼진다.)


'Turn off'버튼을 클릭하면 서버로 URL/LED=OFF 요청을 보내고 서버에서는 LED=OFF 문자열을 감지해 NodeMCU의 built-in LED를 끈다. (실제 built-in LED의 동작은 반대로 된다. LED가 켜진다.)


반응형
Posted by J-sean
: