컴퓨터지식나누기

Amazon Alexa 와 NodeMCU 및 릴레이 및 LCD 연결

겨울아찌 2017. 8. 8. 23:21

ESP8266 과 2구 릴레이, 그리고 LCD 까지 인터페이스...


LCD 디스플레이 라이브러리는 

https://github.com/marcoschwartz/LiquidCrystal_I2C


을 사용. (zip 받아서 libraries 디렉토리에 풀어 놓음.)


다른 라이브러리는 ESP8266 보드와 호환이 되지 않는다고 컴파일 되지 않음.


이 라이브러리도 호환되지 않을수 있다는 경고 메시지가 출력되지만, 정상적으로 동작함.






faxumoESP 프로그램의 소오스는 필요에 따라 수정했음.

수정한 내용은 GPIO 핀을 배정하고, LCD 디스플레이가 되도록 끼어 놓은 것.


==============


#include <Arduino.h>

#include <ESP8266WiFi.h>

#include <Wire.h> 

#include <LiquidCrystal_I2C.h>

#include "fauxmoESP.h"


//#include "credentials.h"

#define WIFI_SSID "WINCHILD-HOME 2.4"

#define WIFI_PASS "winchild6197"


#define SERIAL_BAUDRATE                 115200

#define LED                             2


/*

static const uint8_t D0 = 16;

static const uint8_t D1 = 5;

static const uint8_t D2 = 4;

static const uint8_t D3 = 0;

static const uint8_t D4 = 2;

static const uint8_t D5 = 14;

static const uint8_t D6 = 12;

static const uint8_t D7 = 13;

static const uint8_t D8 = 15;

static const uint8_t D9 = 3;

static const uint8_t D10 = 1;

*/

// Set Relay Pins

int relayOne = 14;  // D5

int relayTwo = 12;  // D6


LiquidCrystal_I2C lcd(0x27,20,4); 


fauxmoESP fauxmo;


// -----------------------------------------------------------------------------

// Wifi

// -----------------------------------------------------------------------------


void wifiSetup() {


   // Print a message to the LCD.

   lcd.init();                      // initialize the lcd 

   lcd.backlight();


    // Set WIFI module to STA mode

    WiFi.mode(WIFI_STA);


    // Connect

    Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);

    lcd.setCursor(0,0);  lcd.printf("[WIFI] %s ", WIFI_SSID); 

    WiFi.begin(WIFI_SSID, WIFI_PASS);


    // Wait

    lcd.setCursor(0,1);  

    while (WiFi.status() != WL_CONNECTED) {

        Serial.print(".");

        lcd.printf("."); 

        delay(100);

    }

    Serial.println();


    // Connected!

    Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());

    lcd.setCursor(0,0);

    lcd.printf("IP:%s   ", WiFi.localIP().toString().c_str());


}


void setup() {


    

    // Init serial port and clean garbage

    Serial.begin(SERIAL_BAUDRATE);

    Serial.println();

    Serial.println();


    // Wifi

    wifiSetup();


    // LED

    //pinMode(LED, OUTPUT);

    //digitalWrite(LED, HIGH);

    pinMode(relayOne, OUTPUT);

    digitalWrite(relayOne, HIGH);

    pinMode(relayTwo, OUTPUT);

    digitalWrite(relayTwo, HIGH);


    // Fauxmo

    fauxmo.addDevice("light one");

    fauxmo.addDevice("light two");

    //fauxmo.addDevice("light three");

    //fauxmo.addDevice("light four");


    // fauxmoESP 2.0.0 has changed the callback signature to add the device_id, this WARRANTY

    // it's easier to match devices to action without having to compare strings.

    fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {

        Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");

        if (device_id == 0) {

          digitalWrite(relayOne, !state);

          lcd.setCursor(0, 1);

          lcd.printf("#%d(%d)%d ", device_id, relayOne, state);

        }

        else if (device_id == 1) {

          digitalWrite(relayTwo, !state);

          lcd.setCursor(8, 1);

          lcd.printf("#%d(%d)%d ", device_id, relayTwo, state);

        }

       

    });


}


void loop() {


    // Since fauxmoESP 2.0 the library uses the "compatibility" mode by

    // default, this means that it uses WiFiUdp class instead of AsyncUDP.

    // The later requires the Arduino Core for ESP8266 staging version

    // whilst the former works fine with current stable 2.3.0 version.

    // But, since it's not "async" anymore we have to manually poll for UDP

    // packets

    fauxmo.handle();



    static unsigned long last = millis();

    if (millis() - last > 5000) {

        last = millis();

        Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());

    }


}


==================


GPIO D5 (핀14) 는 Light One 으로 배정하고, D6 (핀12) 는 Light Two 로 배정함.

LCD 디스플레이는 2x16 이라서 그것에 맞게 메시지 출력을 조정. 핀은 LCD 측의 SCL 은 D1, SDA 는 D2 와 연결.







에피소드가 있는데, 전등을 Light One, Light Two 이렇게 지정을 했는데, 내가 발음하는, Light Two 를 알렉사가 통 알아듣지 못함. 열댓번 명령을 내리다가... 원어 발음에 익숙한 아들을 불러서, 명령을 내려보니... 잘 알아 들음... 뮝미... ㅠ.ㅠ

뭐가 문제인가 분석해 보니, 발음하는데 있어서, Light 의 끝의 t 발음이 다음의 Two 와 충돌하는 문제였음. 앞의 Light 의 t 를 묵음으로 발음하니 잘 알아들음.


몇가지 고질적인 콩글리쉬를 개선하는 효과? ㅋㅋㅋ