nodemcu는 wifi 기능이 포함된 mcu이다. 그래서 다양한 방법으로 웹에 접속이 가능하다. nodemcu를 서버처럼 동작시킬 수도 있고, AP(access point)처럼 동작시킬수도 있다. 그러며 기본적으로 제공하고 있는 예제로 wifi server 동작을 실행해보자.
아두이노에는 기본 예제가 있어 접근하기가 쉽다. 난 이 부분 때문에 아두이노를 정말 좋아한다. 잘 모르겠으면 우선 예제를 눌러서 기본적으로 동작을 시켜보면 된다. 그러면, 아두이노 - 예제에서 아래의 HelloServer를 열어보자.
소스는 아래와 같다. 코드가 길어서 어려워 보이지만, 어렵지 않다. 수정할 부분은 단지, define STASSID 부분과 defie STAPSK 부분이다. STASSID는 집에 설치된 무선 wifi의 ssid 이름을 "정확히, 대소문자 구분하여" 적으면 되고, STAPSK는 wifi 비번을 입력하면 된다. 이 부분은 개인정보이므로... 삭제하여 아래와 같이 표기하였다.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "iptime2.4G"
#define STAPSK "---------"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
const int led = 9;
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!");
digitalWrite(led, 0);
}
void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.on("/gif", []() {
static const uint8_t gif[] PROGMEM = {
0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d,
0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c,
0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b
};
char gif_colored[sizeof(gif)];
memcpy_P(gif_colored, gif, sizeof(gif));
// Set the background to a random set of colors
gif_colored[16] = millis() % 256;
gif_colored[17] = millis() % 256;
gif_colored[18] = millis() % 256;
server.send(200, "image/gif", gif_colored, sizeof(gif_colored));
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
}
소스코드를 수정하였으면, 업로드를 실행해보자. 아래와 같이 동작하면 정상적으로 진행되고 있는 것이다.
시리얼 포트 모니터링 화면을 열어보면, 아래와 같이 192.168.0.74 에 접속되었다고 나온다.
결과는 아래와 같다.
우선 http://192.168.0.74 에 접속해 보자. 나는 iptime 제품을 사용하므로 ip가 192.168.0.xxx으로 시작한다.
아래와 같이 "hello from esp8266!"으로 정상동작함을 알 수 있다.
다음은 http://192.168.0.74/inline 에 접속해보자. "this works as well"로 잘 동작함을 알 수 있다. 위의 소스코드에 보면, 대략 90줄 근터에 server.on("/inline ~~ 로 시작하는 부분에 있는 코드로, 정상동작을 하였다.
그리고, GIF로 올려진 코드도 가능하다. 주소창에 http://192.168.0.74/gif 를 접속해 보자.
아래와 같이 간단하게 만들어진 GIF 그림이 나옴을 알 수 있다. 아래는 정지화면이지만, 실제로 접속하면 GIF 파일로 움직이게 됨을 알 수 있다.
마지막으로 잘못 접속하게 되면 어떻게 되는지 확인하자. 예를들어, http://192.168.0.74/giff로 입력을 하면 해당 주소에는 작성된 code가 없으므로 아래와 같이 file not found가 뜨고 그런 부분은 없다고 표현이 된다. 이 부분의 코드가 handleNotFound() function이다.
다음에 더 재밌는 것을 해보자. 이건 순수히 나의 데이터 확보를 위해서 하는 것이다. 내가 진행했던 일들에 대해 기록하기 위한 것이므로, 질문이 있어도 빠른 답변을 하지 못 할 수도 있다.
'Micro Control Unit' 카테고리의 다른 글
NodeMcu V3 Lolin을 시작하다. (0) | 2021.09.22 |
---|---|
Wemos D1 mini - 미세먼지 센서 (1) | 2021.09.11 |
댓글