인터넷에 있는

IoT중에서 

ESP-01을 이용하여 ...

모듈에 서버를 실행하고

원격으로 웹페이지에 

연결해서 

LG TV를 온 오프하는 프로그램


다른 BLINKY 앱을 사용하는 것이 아닌

순수한 ESP-01만을 이용하여 제어함.

공유기만 설정하면 집 밖에서 원격 제어도

가능할 듯 ...

일단 집안에서 공유기로 연결된 네트워크에서

실행됨을 확인한 작품...

ESP-01 핀 사용현황

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

GPIO 0/2    = OLED

TxD(GPIO 1) = DHT11 (TxD 핀 공유라 아직 온도/습도 표시에 가끔 문제 있음)

RxD(GPIO 3) = IRLED 

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

하드웨어 세팅


실행화면 캡쳐 : 핸드폰에서 실행중 ...


// ============================================================================================ // IOT based home automation project - FIXED IP Version : IR control // https://circuits4you.com/2016/05/19/iot-based-home-automation-project/ // ============================================================================================ // Fixed IP : https://github.com/esp8266/Arduino/issues/1959 // ============================================================================================ // ESP-01 pin usage : GPIO 0/2 - OLED, RxD(GPIO 3) = DHT, TxD(GPIO 1) = IR LED --> NOT OK // ESP-01 pin usage : GPIO 0/2 - OLED, TxD(GPIO 1) = DHT, RxD(GPIO 3) = IR LED --> OK // ============================================================================================ #include #include #include #include "HomePage.h" #include // Only needed for Arduino 1.6.5 and earlier #include "SSD1306.h" // alias for #include "SSD1306Wire.h" #include #define DHTPIN 1 // IR to (built in BLUE LED) TxD = GPIO 1 #define DHTTYPE DHT11 // DHT 11 const int IRLED = 3; // RxD --> GPIO 3 int counter = 0; int countDHT = 0; String StatusPWR, StatusVOL, StatusCH, Temperature, Humidity, myLocalIP; // ============================================================================================ int SignalVOLUP[] ={9000,-4500,600,-550,600,-550,600,-1700,600,-550,600,-550,600,-550,600,-550,600,-550,600,-1700,600,-1700,600,-550,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-550,600,-1700,600,-550,600,-550,600,-550,600,-550,600,-550,600,-550,600,-1700,600,-550,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,0}; int SignalVOLDN[] ={9000,-4500,600,-550,600,-550,600,-1700,600,-550,600,-550,600,-550,600,-550,600,-550,600,-1700,600,-1700,600,-550,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-550,600,-550,600,-550,600,-550,600,-550,600,-550,600,-550,600,-550,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,0}; int SignalCHUP[] ={9000,-4500,600,-550,600,-550,600,-1700,600,-550,600,-550,600,-550,600,-550,600,-550,600,-1700,600,-1700,600,-550,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-750,600,-550,600,-550,600,-550,600,-550,600,-550,600,-550,600,-550,600,-1500,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,0}; int SignalCHDN[] ={9000,-4500,600,-550,600,-550,600,-1700,600,-550,600,-550,600,-550,600,-550,600,-550,600,-1700,600,-1700,600,-550,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-550,600,-550,600,-550,600,-550,600,-550,600,-550,600,-550,600,-550,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,0}; int SignalPOWER[] ={9000,-4500,600,-550,600,-550,600,-1700,600,-550,600,-550,600,-550,600,-550,600,-550,600,-1700,600,-1700,600,-550,600,-1700,600,-1700,600,-1700,600,-1700,600,-1700,600,-550,600,-550,600,-550,600,-1700,600,-550,600,-550,600,-550,600,-550,600,-1700,600,-1700,600,-1700,600,-550,600,-1700,600,-1700,600,-1700,600,-1700,600,0}; // ============================================================================================ const char *ssid = "iptimexxxx"; // Set these to your desired credentials. const char *password = "xxxxxxxx"; ESP8266WebServer server(80); // server on port 80 IPAddress myIP; SSD1306 display(0x3c, 0, 2); // Init OLED - ESP-01 : GPIO 0 = SDA, GPIO 2 = SCL DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino // ============================================================================================ void FunctionRoot() { // handles main page 192.168.4.1 String s = MAIN_page; s.replace("@@TEMP@@", Temperature); // only update at Home reflash s.replace("@@HUMID@@", Humidity); s.replace("@@POWER@@", StatusPWR); s.replace("@@VOLUME@@", StatusVOL); s.replace("@@CHANNEL@@", StatusCH); server.send(200, "text/html", s); } // ============================================================================================ // The TEXTs should be matched to those what in 'HomePage.h' !!!! // ============================================================================================ void FunctionForm() { // Handle Set Date/Time Settings String t_state = server.arg("submit"); // Temperature = String(t); // it is already set in 'displayTempHumid' if(t_state=="POWER ON") { // Change Load-1 State as per request StatusPWR="POWER ON"; IRsend(IRLED, SignalPOWER); // built in BLUE = TxD --> GPIO 1 } if(t_state=="POWER OFF") { StatusPWR="POWER OFF"; IRsend(IRLED, SignalPOWER); // built in BLUE = TxD --> GPIO 1 } if(t_state=="VOL UP") { StatusVOL="VOL UP"; IRsend(IRLED, SignalVOLUP); // built in BLUE = TxD --> GPIO 1 } if(t_state=="VOL DOWN") { StatusVOL="VOL DOWN"; IRsend(IRLED, SignalVOLDN); // built in BLUE = TxD --> GPIO 1 } if(t_state=="CH UP") { StatusCH="CH UP"; IRsend(IRLED, SignalCHUP); // built in BLUE = TxD --> GPIO 1 } if(t_state=="CH DOWN") { StatusCH="CH DOWN"; IRsend(IRLED, SignalCHDN); // built in BLUE = TxD --> GPIO 1 } server.sendHeader("Location", "/"); // This Line Keeps It on Same Page server.send(302, "text/plain", "Updated-- Press Back Button"); delay(500); } // ============================================================================================ void displayTempHumid(){ // Reading temp or humidity takes about 250 ms! delay(1); countDHT++; if(countDHT < 2500) return; // DHT needs more than 2 sec countDHT = 0; //pinMode(IRLED, INPUT); // for DHT11 //digitalWrite(IRLED, HIGH); // should be 'HIGH' to read DHT11 or Error float h = dht.readHumidity(); float t = dht.readTemperature(); // Read temperature as Celsius float f = dht.readTemperature(true); // Read temperature as Fahrenheit if (isnan(h) || isnan(t) || isnan(f)){ // Check if any reads failed and exit to try again //display.clear(); // clearing the display display.drawString(0, 48, "Failed DHT " + String(counter++) + "<>"); display.display(); //Serial.println("Failed DHT"); return; } Temperature = String(t); // do for only valid values Humidity = String(h); //Serial.println(""); //Serial.println("IP: " + myLocalIP); // IP address assigned to your ESP //Serial.println("Humidity: " + String(h) + " Temp: " + String(t)); display.clear(); display.drawString(0, 0, "Server up !! " + String(counter++) + "<>"); display.drawString(0, 16, "IP: " + myLocalIP); display.drawString(0, 24, "Humidity: " + String(h) + "%\t"); display.drawString(0, 32, "Temp: " + Temperature + "C"); display.drawString(0, 40, "Temp: " + String(f) + "F"); display.display(); } // ============================================================================================ // https://blog.naver.com/twophase/220781909314 // ============================================================================================ void IRsend(int IRpin, int IRsignal[]) { // send IR signal int i=0; pinMode(IRpin, OUTPUT); do { if (IRsignal[i]>0) { // Do only for 'HIGH' signal int duration = IRsignal[i]; while (duration > 0) { // if 'HIGH', send '1' with 38KHz digitalWrite(IRpin, HIGH); // making 38KHz signal delayMicroseconds(10); digitalWrite(IRpin, LOW); delayMicroseconds(10); duration -= 26; // 38 kHz = 26.3 uSec } } else { delayMicroseconds(abs(IRsignal[i])); // just keep high - logic '0' } i++; } while (IRsignal[i] != 0); } // ============================================================================================ void setup() { // Power on setup //delay(1000); //Serial.begin(115200); // Arduino monitor - coz TxD is used for IRLED dht.begin(); // initialize dht display.init(); // Initi UI will init the display too. display.flipScreenVertically(); display.setFont(ArialMT_Plain_10); // small size font display.setTextAlignment(TEXT_ALIGN_LEFT); display.clear(); display.drawString(0, 0, "Server Init ..."); display.display(); IPAddress ip(192, 168, xxx, xxx); // NETWORK: Static IP details... IPAddress gateway(192, 168, xxx, 1); IPAddress subnet(255, 255, 255, 0); WiFi.config(ip, gateway, subnet); // Static IP Setup Info Here... WiFi.begin(ssid, password); // Connect to WiFi : orginal code not work while (WiFi.status() != WL_CONNECTED) { delay(500); //Serial.print("."); } myLocalIP = WiFi.localIP().toString(); // Tool-> Debug mode/port --> Disable !!! //Serial.println(""); //Serial.println("IP: " + myLocalIP); // IP address assigned to your ESP //Serial.println(WiFi.localIP()); server.on("/", FunctionRoot); // called when Home reflash server.on("/form", FunctionForm); // called only when button pressed server.begin(); // Server starting ... display.clear(); display.drawString(0, 0, "Server up !!"); display.drawString(0, 16, "IP: " + myLocalIP); display.display(); //pinMode(IRLED, INPUT); // for DHT11, change to OUTPUT for IRLED //digitalWrite(IRLED, HIGH); } // ============================================================================================ void loop() { // Main Program Loop server.handleClient(); displayTempHumid(); // DHT11 needs more than 2 sec - done inside } // ============================================================================================ // ESP8266 보드 설치하기 // https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/installing-the-esp8266-arduino-addon // To begin, we’ll need to update the board manager with a custom URL. Open up Arduino, // then go to the Preferences (File > Preferences). Then, towards the bottom of the window, // copy this URL into the “Additional Board Manager URLs” text box: // // Hit OK. Then navigate to the Board Manager by going to Tools > Boards > Boards Manager. // There should be a couple new entries in addition to the standard Arduino boards. // Look for esp8266. Click on that entry, then select Install. // SSD1306 Lib for ESP8266 // https://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/ // https://github.com/squix78/esp8266-oled-ssd1306/archive/master.zip // unzip to : C:\Users\turbocrazy\Documents\Arduino\libraries // other lib not tested // https://github.com/ThingPulse/esp8266-oled-ssd1306 // ============================================================================================


HomePage.h

// ============================================================================================
// handled by FunctionForm() 
// ============================================================================================
const char MAIN_page[] PROGMEM = R"=====(
<HTML>
<TITLE>
REMOTE IR ON/OFF CONTROL - Fixed IP
</TITLE>
<BODY>
<CENTER>
<FORM method="post" action="/form">
<TABLE>
<TR><TD colspan=2><B>IoT Based Home Automation Test</B></TD></TR>
<TR><TD>Temp: @@TEMP@@ C</TD><TD></TD></TR>
<TR><TD>Humidity: @@HUMID@@ %</TD><TD>Load Status</TD></TR>

<TR><TD>
<INPUT TYPE=SUBMIT VALUE="POWER ON" name=submit>
<INPUT TYPE=SUBMIT VALUE="POWER OFF" name=submit></TD>
<TD>@@POWER@@</TD></TR>
<TR><TD>
<INPUT TYPE=SUBMIT VALUE="VOL UP" name=submit>
<INPUT TYPE=SUBMIT VALUE="VOL DOWN" name=submit></TD>
<TD>@@VOLUME@@</TD></TR>
<TR><TD>
<INPUT TYPE=SUBMIT VALUE="CH UP" name=submit>
<INPUT TYPE=SUBMIT VALUE="CH DOWN" name=submit></TD>
<TD>@@CHANNEL@@</TD></TR>

<TR><TD colspan=2><B><CENTER><A href = "https://diyworld.tistory.com">diyworld.tistory.com</a></CENTER></B></TD></TR>
</TABLE>
</FORM>
</CENTER>
</BODY>
</HTML>
)=====";
// ============================================================================================ 



'IoT_ESP8266' 카테고리의 다른 글

ESP8266 - AP mode로 LED 원격제어  (0) 2019.01.08
ESP8266 - SPIFFS 이해 및 사용  (0) 2019.01.03
ESP-01 : 서버 LED ON/OFF - 고정 IP  (0) 2019.01.01
ESP-01 : 서버로 LED ON/OFF  (0) 2019.01.01
ESP-8266 : 서버 - LED/Relay 제어  (0) 2019.01.01
블로그 이미지

DIYworld

,