ย
๋ณธ ๋ฌธ์๋ ์๋์ด๋ ธ ๊ฐ๋ฐํ๊ฒฝ(IDE)๋ฅผ ์ด์ฉํด์ ESP8266 ๋ชจ๋์ ๋ค๋ฃจ๋ ์์ ๋ค์ ํฌํจํ๊ณ ์์ต๋๋ค.
ESP8266 Arduino IDE ๋ ESP8266 ๋ชจ๋์ ํ์จ์ด๋ฅผ ์ ์ํ๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ ๋๋ค.ย ESP8266 Arduino IDE ๊ฐ๋ฐํ๊ฒฝ์ ์ค์นํ๋ ๋ฐฉ๋ฒ์ ์๋ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ธ์.
ESP8266์ ์ํ ๋ค์ํ ์์ ๊ฐ ์ด๋ฏธ ๊ฐ์ถ์ด์ ธ ์์ต๋๋ค. [Arduino IDE โ File โ Examples โ ESP8266xxx] ์์ ๋ค์ ๋ณด๋ฉด ๋ค์ํ ๊ธฐ๋ฅ๋ค์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ด ๋์ต๋๋ค. ์ฌ๊ธฐ์ ์๊ฐํ๋ ์์ ๋ NodeMCU V1 (ESP12E ๊ธฐ๋ฐ) ๋ณด๋์์ ๋์ ํ ์คํธ๋ฅผ ํ ์์ ๋ค์ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์๊ฐ๋ฅผ ์ํด ์์ ์ฝ๋์ ์ฝ๊ฐ์ ๋ณํ์ด ๊ฐํด์ก์ต๋๋ค.
NodeMCU ๋ณด๋๋ ์๋์ ๊ฐ์ด ํ์ด ๋ฐฐ์น๋์ด ์์ผ๋ฉฐ, ํ์จ์ด ์ ๋ก๋๋ฅผ ์ํด ํ์ํ FTDI ๋ชจ๋์ ๋ด์ฅํ๊ณ ์์ด์ PC์ USB ์ฐ๊ฒฐ๋ง ํ๋ฉด ์ฌ์ฉํ ์ ์์ต๋๋ค.
ย
ย
Blink (Digital Output)
ย
๊ฐ์ฅ ์ฝ๊ณ ๋จ์ํ LED ์ผ๊ธฐ ์์ ์ ๋๋ค. ์์ค์ฝ๋๋ ์๋ ๋งํฌ์์ ๋ณด์ค ์ ์์ต๋๋ค.
LED ๋ ์ ํญ๊ณผ ํจ๊ป NodeMCU ๋ณด๋์ GPIO14 (D5) ํ์ ์ฐ๊ฒฐํ๋ฉด ๋ฉ๋๋ค.
int LED = 14; // Use D5, GPIO14 void setup() { pinMode(LED, OUTPUT); // Initialize the LED pin as an output } // the loop function runs over and over again forever void loop() { digitalWrite(LED, LOW); // Turn the LED delay(1000); // Wait for a seconds digitalWrite(LED, HIGH); // Turn the LED delay(1000); // Wait for a seconds }
์ฝ๋๋ฅผ ๋ณด์๋ฉด ์์ค ์ ์๋ฏ, ํ ๋ฒํธ ๋นผ๊ณ ์๋์ด๋ ธ ์ค์ผ์น์ ์์ ๋์ผํฉ๋๋ค. pinMode() ํจ์๋ฅผ ์ด์ฉํด ํน์ ํ์ ์ด๊ธฐํ ํด์คฌ์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ digitalWrite ํจ์๋ก 3V ์ถ๋ ฅ์ on/off ํด์ค๋๋ค.
์๋์ด๋ ธ ๊ฐ๋ฐํ๊ฒฝ์ด ESP8266 ๋ชจ๋์ ์ง์ํ๋๋ก ๋ง๋ ๋ชฉ์ ์ด ๋ฐ๋ก ์ด๊ฒ๋๋ค. ์ ์ธ๊ณ์ ์ผ๋ก ๊ฐ์ฅ ๋ณดํธํ๋ ๊ฐ๋ฐ๋ฐฉ๋ฒ์ ESP8266 ์๋ ์ ์ฉํจ์ผ๋ก์จ ๊ธฐ์กด์ ์กด์ฌํ๋ ์ ๋ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ธ ์ ์๊ฒ๋ ํด์ฃผ๊ณ , ๋ณด๋ค ์ฝ๊ฒ ESP8266 ํ์จ์ด ์ ์์ ์์ํ๋๋ก ๋์์ค๋๋ค.
์๋์ด๋ ธ์ ์ฌ์ฉ๋ฒ์ ๋์ผํ์ง๋ง ํ ๊ฐ์ง ์ฃผ์ํ ์ ์ด ์์ต๋๋ค. setup() ํจ์์ดํ loop() ํจ์๋ ๋ฌดํ ๋ฐ๋ณต๋ฉ๋๋ค. ์ด๋ loop() ํจ์๊ฐ ํ๋ฒ ๋๊น์ง ์คํ๋๊ณ ๋ค์ loop() ํจ์๊ฐ ์คํ๋๊ธฐ์ ESP8266 ์ ๋ณต์กํ WiFi ํต์ ๊ธฐ๋ฅ๋ค์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ESP8266 core๊ฐ ์คํ๋ฉ๋๋ค. ๋ฐ๋ผ์ย loop() ํจ์์์ ๋๋ฌด ๋ณต์กํ ์์ ์ ์ฒ๋ฆฌํด๋ฒ๋ฆฌ๋ฉด ESP8266 core๊ฐ ์ถฉ๋ถํ ์๊ธฐ ์ญํ ์ ํ ์ ์์ต๋๋ค. ๊ฐ๊ธ์ loop()๋ฅผ ๋นจ๋ฆฌ ์ฒ๋ฆฌํ๊ณ ์ ์ด๊ถ์ ๋๊ฒจ์ค์ผ ํฉ๋๋ค.
๊ฐ์ด๋์์๋ loop() ์์ ์ฌ์ฉ์ ๋ฃจํด์ด 50ms ์ด๋ด์ ๋๋๊ธฐ๋ฅผ ๊ถ์ฅํฉ๋๋ค. ๋ง์ฝ ์ด ์ด์์ ์๊ฐ์ ์ํ ๊ฒฝ์ฐ ์ค๊ฐ์ delay() ํจ์๋ก ์ฝ๊ฐ์ ์๊ฐ ๊ฐ๊ฒฉ์ ์ฃผ๋ ๊ฒ์ด ์ข์ต๋๋ค. delay() ๋ ์ฌ์ฉ์์ ๋ฃจํด์ ์ ์ ๋ฉ์ถ๊ณ ESP8266 core๊ฐ ๋์ํ ์ ์๋๋ก ํด์ค๋๋ค. (yield() ํจ์๋ ๊ฐ์ ์ญํ ์ ํ๋๋ฏ ํฉ๋๋ค.)
ย
ย
Button (Digital Input)
ย
digitalWrite() ํจ์๋ฅผ ์ด์ฉํด์ LED ์ถ๋ ฅ์ ์ ์ดํด๋ดค์ผ๋ ์ด๋ฒ์๋ digitalRead() ํจ์๋ฅผ ์ด์ฉํด์ ๋์งํธ ์ ๋ ฅ์ ๋ฐ์๋ณด๊ฒ ์ต๋๋ค. ๋ฒํผ์ ์ฐ๊ฒฐํด์ ํด๋ฆญ์ ์ธ์ํ ๋ค, LED๋ฅผ on/off ์ํค๋ ์์ ์ ๋๋ค.
๋ฒํผ์ GPIO4(D2) ํ์ pull-down ์ ํญ์ ์ด์ฉํด ์ฐ๊ฒฐํฉ๋๋ค.
- ESP8266 GPIO14 ==> ๋ฒํผ ๋ค๋ฆฌ 1
- ESP8266 3V ==> ๋ฒํผ ๋ค๋ฆฌ 2
- ESP8266 GNDย ==> 10K ์ ํญ ==> ๋ฒํผ ๋ค๋ฆฌ 2
์์ค์ฝ๋๋ ์๋์ ๊ฐ์ต๋๋ค.
int LED = 14; // Use D5, GPIO14 int BUTTON = 4; // Use D2, GPIO4 void setup() { Serial.begin(115200); pinMode(LED, OUTPUT); // Initialize the LED pin as an output pinMode(BUTTON, INPUT); // Initialize the BUTTON pin as an input } // the loop function runs over and over again forever void loop() { boolean buttonPressed = digitalRead(BUTTON); Serial.println(buttonPressed); digitalWrite(LED, buttonPressed); delay(100); // Wait for a while }
setup() ์ด๊ธฐํ ํจ์์์ ๋ฒํผ ์ ๋ ฅ์ ๋ฐ๊ธฐ ์ํด GPIO 4๋ฒ ํ์ INPUT ๋ชจ๋๋ก ์ด๊ธฐํ ํ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ loop() ๋ฐ๋ณตํจ์์์๋ digitalRead() ํจ์๋ก ๋ฒํผ ์ํ๋ฅผ ์ฝ์ต๋๋ค. ๊ฐ์ HIGH(=true, 3V, ๋ฒํผ ํด๋ฆญ) ๋๋ LOW(=false, 0V, GND, ๋ฒํผ ๋ฆด๋ฆฌ์ฆ) ๊ฐ์ผ๋ก ๋ค์ด์ต๋๋ค. ๋ฐ๋ผ์ ์ด ๊ฐ์ ๊ทธ๋๋ก digitalWrite() ํจ์์ ์ ๋ฌํด์ฃผ๋ฉด ๋ฒํผ์ ๋๋ฅผ๋๋ง๋ค LED๊ฐ ์ผ์ง๋๋ค.
ย
ย
Potentiometerย (ANALOG INPUT)
ย
์ด๋ฒ์๋ ์๋ ๋ก๊ทธ ์ ๋ ฅ์ ๋ฐ๋ ๋ฐฉ๋ฒ์ ๋๋ค. ์์ค์ฝ๋๋ ์๋ ๋งํฌ์ ์์ต๋๋ค.
- https://github.com/godstale/ESP8266_Arduino_IDE_Example/blob/master/example/AnalogInput_Potentiometer/AnalogInput_Potentiometer.ino
์๋ ๋ก๊ทธ ์ ๋ ฅ์ ํ ์คํธ ํ๊ธฐ ์ํด์ ํฌํ ์ ๋ฏธํฐ๋ฅผ ์ฌ์ฉํ์ต๋๋ค. ํฌํ ์ ๋ฏธํฐ๋ฅผย ADC0(A0) ์๋ ๋ก๊ทธ ํ์ ์ฐ๊ฒฐํ๋ฉด ๋ฉ๋๋ค.
- ํฌํ ์ ๋ฏธํฐ 1 ํ ==> ESP8266 3V
- ํฌํ ์ ๋ฏธํฐ 2ย ํ (๊ฐ์ด๋ฐ ํ) ==> ESP8266 ADC0(A0)
- ํฌํ ์ ๋ฏธํฐ 3ย ํ ==> ESP8266ย GND
๊ทธ๋ฆฌ๊ณ ์์ค์ฝ๋๋ฅผ ์ฌ๋ฆฌ๋ฉด ์๋ฆฌ์ผ ํต์ ์ผ๋ก ํฌํ ์ ๋ฏธํฐ์์ ์ฝ์ ์๋ ๋ก๊ทธ ์ ๋ ฅ๊ฐ์ ์ถ๋ ฅํด์ค๋๋ค.
void setup() { Serial.begin(115200); } // the loop function runs over and over again forever void loop() { int analog = analogRead(A0); Serial.println(analog); delay(100); // Wait for a while }
์๋์ด๋ ธ์ ๋์ผํฉ๋๋ค. analogRead() ํจ์๋ก ์๋ ๋ก๊ทธ ํ์ ์ ์ ๋ณํ๋ฅผ ์ฝ์ ์ ์์ต๋๋ค. ๋จ, ESP8266 ๋ชจ๋์ ์๋ ๋ก๊ทธ ์ ๋ ฅ ํ์ด ํ๋(A0, ADC0) ๋ฟ์ ๋๋ค.
์์ค์์๋ ์๋ฆฌ์ผ ํต์ ์ ์ํด Serial.begin() ์ผ๋ก ํต์ ์ด๊ธฐํ๋ฅผ ํด์คฌ์ต๋๋ค. ์๋์ด๋ ธ์ฒ๋ผ Serial.print(), println(), available(), read() ํจ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ย
ย
LED DIMMINGย (PWM โ ANALOG OUTPUT)
ย
์ด๋ฒ ์์ ์์๋ PWM์ ์ด์ฉํด์ LED ๋ฐ๊ธฐ๋ฅผ ์กฐ์ ํด ๋ณผ๊ฒ๋๋ค. ์์ ์์ ์์ ํฌํ ์ ๋ฏธํฐ๋ฅผ ์ฐ๊ฒฐํ์ผ๋ ํฌํ ์ ๋ฏธํฐ๋ก LED ๋ฐ๊ธฐ ์กฐ์ ์ด ๊ฐ๋ฅํ๋๋ก ํ ๊ฒ๋๋ค. ์์ค์ฝ๋๋ ์๋์์ ๊ตฌํ ์ ์์ต๋๋ค.
์์ ์ฌ์ฉํ LED, ํฌํ ์ ๋ฏธํฐ๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ์๋ฉด ๋ฉ๋๋ค.
int LED = 14; // Use D5, GPIO14 void setup() { Serial.begin(115200); pinMode(LED, OUTPUT); // Initialize the LED pin as an output } // the loop function runs over and over again forever void loop() { int analog = analogRead(A0); Serial.println(analog); analogWrite(LED, analog/4); delay(100); // Wait for a while }
PWM ์ฌ์ฉ์ ๊ฐ๋จํฉ๋๋ค. setup() ์ด๊ธฐํ ํจ์์์ pinMode()๋ก LED ํ์ OUTPUT ๋ชจ๋๋ก ์ด๊ธฐํ ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ analogWrite() ํจ์๋ก ๊ฐ์ ์จ์ฃผ๋ฉด ๋ฉ๋๋ค. (0~255)
ํฌํ ์ ๋ฏธํฐ์ ๊ฐ์ ์ฝ๋ analogRead() ๋ฒ์๊ฐ 0~1023์ด๋ 4๋ก ๋๋ ์ค ๊ฐ์ analogWrite() ํจ์์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
ย
ย
๊ธฐํ ์์
ย
์์์ ์๊ฐํ ๊ฐ๋จํ ์์ ์ธ์๋ ๋ค์ํ ์ผ์ ์ฌ์ฉ์ ์ํ ์๋์ด๋ ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ์ฌ์ฉํ ์ ์์ต๋๋ค.ย ์๋์ด๋ ธ ์ ์ด๋ฅผ ์ํด ๋ ์ง์คํธ๋ฆฌ๋ฅผ ๊ฑด๋๋ฆฌ์ง ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ฒฝ์ฐ ESP8266์์๋ ๋ฌธ์ ์์ด ์ฌ์ฉ ๊ฐ๋ฅํ ๊ฒ์ผ๋ก ์๋ ค์ ธ ์์ต๋๋ค. ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ESP8266์์๋ ์ฌ์ฉ ๊ฐ๋ฅํ (ํ ์คํธ ๋) ํธํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค.
- Adafruit_ILI9341 โ Port of the Adafruit ILI9341 for the ESP8266
- arduinoVNC โ VNC Client for Arduino
- arduinoWebSockets โ WebSocket Server and Client compatible with ESP8266 (RFC6455)
- aREST โ REST API handler library.
- Blynk โ easy IoT framework for Makers (check out the Kickstarter page).
- DallasTemperature
- DHT-sensor-library โ Arduino library for the DHT11/DHT22 temperature and humidity sensors. Download latest v1.1.1 library and no changes are necessary. Older versions should initialize DHT as follows:
DHT dht(DHTPIN, DHTTYPE, 15)
- Encoder โ Arduino library for rotary encoders. Version 1.4 supports ESP8266.
- NeoPixel โ Adafruitโs NeoPixel library, now with support for the ESP8266 (use version 1.0.2 or higher from Arduinoโs library manager).
- NeoPixelBus โ Arduino NeoPixel library compatible with ESP8266. Use the โDmaDrivenโ or โUartDrivenโ branches for ESP8266. Includes HSL color support and more.
- PubSubClient โ MQTT library by @Imroy.
- RTC โ Arduino Library for Ds1307 & Ds3231 compatible with ESP8266.
- Souliss, Smart Home โ Framework for Smart Home based on Arduino, Android and openHAB.
- ST7735 โ Adafruitโs ST7735 library modified to be compatible with ESP8266. Just make sure to modify the pins in the examples as they are still AVR specific.
- Task โ Arduino Nonpreemptive multitasking library. While similiar to the included Ticker library in the functionality provided, this library was meant for cross Arduino compatibility.
- UTFT-ESP8266 โ UTFT display library with support for ESP8266. Only serial interface (SPI) displays are supported for now (no 8-bit parallel mode, etc). Also includes support for the hardware SPI controller of the ESP8266.
- WiFiManager โ WiFi Connection manager with web captive portal. If it canโt connect, it starts AP mode and a configuration portal so you can choose and enter WiFi credentials.
- OneWire โ Library for Dallas/Maxim 1-Wire Chips.
- Adafruit-PCD8544-Nokia-5110-LCD-Library โ Port of the Adafruit PCD8544 โ library for the ESP8266.
ย
์ธํฐ๋ท์ ๊ฒ์ํด ๋ณด๋ฉด ์ด ์ธ์๋ ์ฃผ์ํ ์ผ์๋ค์ ์์ ๋ฅผ ๊ตฌํ์ค ์ ์์ต๋๋ค.
ย
ย
์ฐธ๊ณ ์๋ฃ
ย
ย