ESP-01에 부착된 LED를
원격으로 접속해서
온 오프 하기 ...
WiFi에 접속하기
작동하는 웹 페이지
버튼을 누르면 LED가 제어된다
ESP8266을 SoftAP로 작동시키면
접속주소는 주로 192.168.4.1 임
// ============================================================================================ // AP mode : ESP가 서버가 되어 고유IP 갖는다. 다른 기기에서 라우터 거치지 않고 직접 연결 // ============================================================================================ // 참조 사이트 // http://circuits4you.com/2016/12/16/esp8266-web-server-ap/ // https://circuits4you.com/2016/05/19/iot-based-home-automation-project/ // https://circuits4you.com/2018/02/05/esp8266-arduino-wifi-web-server-led-on-off-control/ // https://blog.naver.com/twophase/220709172472 // ============================================================================================ #include#include #include #include "HomePage.h" // ============================================================================================ //SSID and Password to your ESP Access Point const char* ssid = "ESPWebServer"; // WiFI 목록에 나타나는 이름 const char* password = "12345678"; const int LED_PIN = 2; // LED of ESP-01 connect at GPIO 2 String StatusLED, Temperature, Humidity; // ============================================================================================ ESP8266WebServer server(80); // Server on port 80 // ============================================================================================ // This rutine is exicuted when you open its IP in browser // ============================================================================================ void FunctionRoot() { String s = MAIN_page; Temperature = digitalRead(0); // nothing connected, just dumy Humidity = analogRead(A0); // ditto s.replace("@@TEMP@@", Temperature); // only update at Home reflash s.replace("@@HUMID@@", Humidity); s.replace("@@LED@@", StatusLED); server.send(200, "text/html", s); Serial.println("Home page refreshed"); } // ============================================================================================ // 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=="LED ON") { // Change Load-1 State as per request StatusLED="LED ON"; digitalWrite(LED_PIN, LOW); // IR to (built in BLUE LED) TxD = GPIO 1 Serial.println("LED ON"); } if(t_state=="LED OFF") { StatusLED="LED OFF"; digitalWrite(LED_PIN, HIGH); Serial.println("LED Off"); } server.sendHeader("Location", "/"); // This Line Keeps It on Same Page server.send(302, "text/plain", "Updated -- Press Back Button"); delay(100); } // ============================================================================================ void setup(void){ Serial.begin(115200); Serial.println(""); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, HIGH); WiFi.mode(WIFI_AP); // Only Access point (AP mode) WiFi.softAP(ssid, password); // Start HOTspot removing password will disable security IPAddress myIP = WiFi.softAPIP(); // Get IP address Serial.print("HotSpt IP:"); Serial.println(myIP); server.on("/", FunctionRoot); // Which routine to handle at root location server.on("/form", FunctionForm); // called only when button pressed server.begin(); // Start server Serial.println("HTTP server started"); } // ============================================================================================ void loop(void){ server.handleClient(); // Handle client requests } // ============================================================================================
아두이노 IDE에 탭으로 추가. 화일명에 주의! "HomePage.h"
AP 주소로 접속한 장치에 보여지는 웹페이지 소스임
// ============================================================================================ // handled by FunctionForm() // ============================================================================================ const char MAIN_page[] PROGMEM = R"=====( <HTML> <TITLE> REMOTE IR ON/OFF CONTROL - Fixed IP </TITLE> <BODY> <FONT size=7></FONT> <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="LED ON" name=submit> <INPUT TYPE=SUBMIT VALUE="LED OFF" name=submit></TD> <TD>@@LED@@</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' 카테고리의 다른 글
ESP-01 : 서버와 데이터 주고 받기 정리 (0) | 2019.01.14 |
---|---|
jQuery 사용하기 (0) | 2019.01.13 |
ESP8266 - SPIFFS 이해 및 사용 (0) | 2019.01.03 |
ESp-01 : 원격 TV ON/OFF 리모콘 제어 (1) | 2019.01.02 |
ESP-01 : 서버 LED ON/OFF - 고정 IP (0) | 2019.01.01 |