ESP8266으로 웹서버 동작
필요한 자료는 ...
ESP8266 Web Server with Arduino IDE
ESP8266 플래시 하기
0. ESP8266-01 을 사용할 경우 GPIO5/GPIO4는 --> GPIO0/GPIO2로 변경한다
1. 플래시 업로딩시 (그냥 업로딩이 안되 고생해씀)
아두이노 IDE와 연결되고 USB-TTL 어댑터는 연결한 상태에서
GPIO0핀은 GND로 연결
이상태에서 ESP8266의 전원을 껏다 켠다
그런 2번 내용 처럼 플래시 업로딩이 된다
2. 플래시 업로딩 완료후
GPIO0핀은 VCC로 잠시 연결해서 리셋시킴.
3. 만일 컴파일시 ***** 에러 나는 경우 아두이노IDE에서 ESP8266 삭제하고 재설치 하면 됨
컴파일 후 플래시 업로딩 화면....
Build options changed, rebuilding all Archiving built core (caching) in: C:\Users\TURB..\AppData\Local\Temp\arduino_cache_461323\core\core_esp8266_esp8266_generic_CpuFrequency_80,VTable_flash,ResetMethod_ck,CrystalFreq_26,FlashFreq_40,FlashMode_qio,FlashSize_512K0,led_2,LwIPVariant_v2mss536,Debug_Disabled,DebugLevel_None____,FlashErase_none,UploadSpeed_115200_f66568eceb7f82e996c1ecda33990dd5.a Sketch uses 265048 bytes (53%) of program storage space. Maximum is 499696 bytes. Global variables use 30172 bytes (36%) of dynamic memory, leaving 51748 bytes for local variables. Maximum is 81920 bytes. C:\Users\tu...\AppData\Local\Arduino15\packages\esp8266\tools\esptool\0.4.13/esptool.exe -vv -cd ck -cb 115200 -cp COM3 -ca 0x00000 -cf C:\Users\TURBOD~1\AppData\Local\Temp\arduino_build_940840/ESP8266_Web_Server.ino.bin esptool v0.4.13 - (c) 2014 Ch. Klippelsetting board to ck setting baudrate from 115200 to 115200 setting port from COM1 to COM3 setting address from 0x00000000 to 0x00000000 espcomm_upload_file espcomm_upload_mem setting serial port timeouts to 1000 ms opening bootloader resetting board trying to connect flush start setting serial port timeouts to 1 ms setting serial port timeouts to 1000 ms flush complete espcomm_send_command: sending command header espcomm_send_command: sending command payload read 0, requested 1 trying to connect flush start setting serial port timeouts to 1 ms setting serial port timeouts to 1000 ms flush complete espcomm_send_command: sending command header espcomm_send_command: sending command payload espcomm_send_command: receiving 2 bytes of data espcomm_send_command: receiving 2 bytes of data espcomm_send_command: receiving 2 bytes of data espcomm_send_command: receiving 2 bytes of data espcomm_send_command: receiving 2 bytes of data espcomm_send_command: receiving 2 bytes of data espcomm_send_command: receiving 2 bytes of data espcomm_send_command: receiving 2 bytes of data Uploading 269200 bytes from C:\Users\TUR...\AppData\Local\Temp\arduino_build_940840/ESP8266_Web_Server.ino.bin to flash at 0x00000000 erasing flash size: 041b90 address: 000000 first_sector_index: 0 total_sector_count: 66 head_sector_count: 16 adjusted_sector_count: 50 erase_size: 032000 espcomm_send_command: sending command header espcomm_send_command: sending command payload setting serial port timeouts to 15000 ms setting serial port timeouts to 1000 ms espcomm_send_command: receiving 2 bytes of data writing flash ....................................................... [ 30% ] ....................................................... [ 60% ] ....................................................... [ 91% ] ....................... [ 100% ] starting app without reboot espcomm_send_command: sending command header espcomm_send_command: sending command payload espcomm_send_command: receiving 2 bytes of data closing bootloader flush start setting serial port timeouts to 1 ms setting serial port timeouts to 1000 ms flush complete
코드는 다음과 같다
출처 : ESP8266 Web Server with Arduino IDE
#include// Replace with your network credentials const char* ssid = "iptimexxxxx"; const char* password = "xxxxxxxxxxx"; // Set web server port number to 80 WiFiServer server(80); // Variable to store the HTTP request String header; // Auxiliar variables to store the current output state String output5State = "off"; String output4State = "off"; // Assign output variables to GPIO pins const int output5 = 2; const int output4 = 0; void setup() { Serial.begin(115200); // Initialize the output variables as outputs pinMode(output5, OUTPUT); pinMode(output4, OUTPUT); // Set outputs to LOW digitalWrite(output5, LOW); digitalWrite(output4, LOW); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } void loop(){ WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // turns the GPIOs on and off if (header.indexOf("GET /5/on") >= 0) { Serial.println("GPIO 5 on"); output5State = "on"; digitalWrite(output5, HIGH); } else if (header.indexOf("GET /5/off") >= 0) { Serial.println("GPIO 5 off"); output5State = "off"; digitalWrite(output5, LOW); } else if (header.indexOf("GET /4/on") >= 0) { Serial.println("GPIO 4 on"); output4State = "on"; digitalWrite(output4, HIGH); } else if (header.indexOf("GET /4/off") >= 0) { Serial.println("GPIO 4 off"); output4State = "off"; digitalWrite(output4, LOW); } // Display the HTML web page client.println(""); client.println(""); client.println(""); // CSS to style the on/off buttons // Feel free to change the background-color and font-size attributes to fit your preferences client.println(""); // Web Page Heading client.println(" ESP8266 Web Server
"); // Display current state, and ON/OFF buttons for GPIO 5 client.println("GPIO 5 - State " + output5State + "
"); // If the output5State is off, it displays the ON button if (output5State=="off") { client.println(""); } else { client.println(""); } // Display current state, and ON/OFF buttons for GPIO 4 client.println("GPIO 4 - State " + output4State + "
"); // If the output4State is off, it displays the ON button if (output4State=="off") { client.println(""); } else { client.println(""); } client.println(""); // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } }
'IoT_ESP8266' 카테고리의 다른 글
ESP8266 - ESP-01 : 온도및 습도 모니터링 OLED/Web (0) | 2018.12.30 |
---|---|
ESP-01 with OLED (1) | 2018.12.30 |
IoT - 사물인터넷 기초 자료 (0) | 2017.01.30 |
ESP8266 과 서버와 통신 (Synology) (0) | 2017.01.27 |
ESP8266 -Arduino WiFi 사용법 (0) | 2017.01.27 |