ESP8266 - 코드 구조 | ESP8266 - Code Structure

준비물

1×ESP8266 NodeMCU Amazon
1×Micro USB Cable Amazon
1×(Optional) 5V Power Adapter for ESP8266 Amazon
1×(Optional) ESP8266 Screw Terminal Adapter Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

기본 구조

ESP8266 코드는 두 부분으로 구성되어 있습니다: 설정 코드와 루프 코드입니다. 설정 코드는 프로그램이 시작될 때 한 번 실행됩니다. 루프 코드는 프로그램이 중지될 때까지 계속해서 실행됩니다.

설치 코드

setup() 함수 안의 코드는 전원이 켜지거나 리셋된 직후 실행됩니다. 이 코드는 한 번만 실행되며 변수 초기화, 핀 모드 설정, 라이브러리 사용 시작 등을 위해 사용됩니다.

루프 코드

loop() 함수에서의 코드는 셋업 코드 바로 다음에 실행됩니다. 이것은 반복적으로, 무한히 실행되며, 애플리케이션의 주요 작업을 수행하는 데 사용됩니다.

예시

void setup() { // 여기에 설정 코드를 넣으세요. 한 번 실행됩니다: Serial.begin(9600); Serial.println("This is setup code"); } void loop() { // 여기에 주요 코드를 넣으세요. 반복적으로 실행됩니다: Serial.println("This is loop code"); delay(1000); }

사용 방법

Arduino IDE에서 ESP8266을 시작하는 방법은 다음과 같습니다:

  • ESP8266을 처음 사용하는 경우, Arduino IDE에서 ESP8266 환경 설정 방법 튜토리얼을 확인하세요.
  • 다이어그램에 표시된 대로 구성 요소를 연결하세요.
  • USB 케이블을 사용하여 컴퓨터에 ESP8266 보드를 연결하세요.
  • 컴퓨터에서 Arduino IDE를 엽니다.
  • 올바른 ESP8266 보드를 선택하세요, 예를 들어 (NodeMCU 1.0 (ESP-12E Module)) 및 해당 COM 포트.
  • 코드를 복사하고 Arduino IDE로 엽니다.
  • IDE에서 Upload 버튼을 클릭하여 코드를 ESP8266으로 전송하세요.
  • 시리얼 모니터를 엽니다.
  • 시리얼 모니터에서 출력을 확인하세요.
COM6
Send
This is setup code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

“이것은 설정 코드입니다”가 단 한 번만 출력되는 것을 관찰할 수 있지만, “이것은 반복 코드입니다”가 여러 번 출력됩니다. 이는 설정 코드는 한 번 실행되고, 반복 코드는 반복적으로 실행된다는 것을 의미합니다. 설정 코드가 먼저 실행됩니다.

※ NOTE THAT:

setup()loop() 함수는 반드시 ESP8266 코드에 포함되어야 합니다. 포함되지 않을 경우 오류가 발생합니다.

선택적 부품

: 스케치 전체에서 사용할 수 있는 변수를 선언하는 데 사용됩니다.

ESP8266 스케치는 설치 및 루프 코드 외에 다음 섹션들로 구성될 수 있습니다:

  • Block comment: generally used to include details about the author, wiring instructions, license etc. which are ignored by ESP8266.
  • Libraries inclusion: used to include libraries in the sketch.
  • Constant definition: used to define constants.
  • Global variables declaration: used to declare variables which can be accessed throughout the sketch.
/* * 이 ESP8266 NodeMCU 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP8266 NodeMCU 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp8266/esp8266-code-structure */ #include <Servo.h> #include <LiquidCrystal.h> #define MAX_COUNT 180 Servo servo; LiquidCrystal lcd(3, 4, 5, 6, 7, 8); int loop_count = 0; void setup() { Serial.begin(9600); lcd.begin(16, 2); servo.attach(9); Serial.println("This is setup code"); // 이것은 설정 코드입니다 } void loop() { loop_count++; Serial.print("This is loop code, count: "); // 이것은 반복 코드입니다, count: Serial.println(loop_count); lcd.print("Hello World!"); servo.write(loop_count); if(loop_count >= MAX_COUNT) loop_count = 0; delay(1000); }

사용 방법

  • 도표에서 보여지는 대로 부품을 연결하세요.
  • USB 케이블을 사용하여 ESP8266 보드를 컴퓨터에 연결하세요.
  • 컴퓨터에서 Arduino IDE를 열세요.
  • 올바른 ESP8266 보드(예: NodeMCU 1.0 (ESP-12E 모듈))와 해당 COM 포트를 선택하세요.
  • 코드를 복사하고 Arduino IDE로 열세요.
  • IDE에서 Upload 버튼을 클릭하여 코드를 ESP8266으로 전송하세요.
  • 시리얼 모니터를 열세요.
  • 시리얼 모니터에서 출력을 확인하세요.
COM6
Send
This is setup code This is loop code, count: 1 This is loop code, count: 2 This is loop code, count: 3 This is loop code, count: 4 This is loop code, count: 5 This is loop code, count: 6 This is loop code, count: 7
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

현재 우리는 코드를 줄마다 이해할 필요가 없습니다. 우리는 단지 코드 구조에 익숙해져야 합니다. 줄마다의 코드는 이후 튜토리얼에서 설명될 것입니다.

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!