ESP8266
Arduino WiFi 사용법을
검색하면
엄청난 정보가 나오는데 ...
정작 몇가지를
정확히 맞추지 않으면
제대로 동작하지 않는다
때문에 지금까지 시험한
내용을 토대로 검증된
정보를 정리 해 놓는다
1. 결선 : FDTI 보드로 PC와 연결하는 방법이다
같은 방식으로 ESP8266 보드와 ARDUINO(FDTI보드대신)를 연결한다.
아두이노는 5V, ESP8266은 3.3V 이라서 ESP8266의 RX측에
전압을 맞추는 저항을 이용해서 연결한다
TX -- RX, RX -- TX
2. ESP8266을 독자로 동작하기 :
아두이노 없이 독자로 WiFi 서버로 동작한다, ARDUINO IDE에서 ESP8266 보드로
연결하고 스케치 넣어서 컴파일후 업로드...
3. ESP8266과 아두이노를 시리얼로 연결해서 제어하는 법 (다운은 아래 화일받을것)
// ----------------------------------------------------------------------------------------- // http://deneb21.tistory.com/274 // ----------------------------------------------------------------------------------------- //SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3. // This means that you need to connect the TX line from the esp to the Arduino's pin 2 // and the RX line from the esp to the Arduino's pin 3 // ----------------------------------------------------------------------------------------- #include "U8glib.h" #define DEBUG true U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI int cnt; int apple=0; int led22 = 22; long int count = 0; String LocalIP = "", LEDstatus = ""; boolean LEDON=false; // ----------------------------------------------------------------------------------------- String getLocalIP(String rawText){ String inData = "Couldn't get IP adress"; if (rawText.indexOf("STAIP") != -1) { inData = rawText.substring(rawText.indexOf("STAIP")+7,rawText.indexOf("STAMAC") - 10); } u8g.firstPage(); do{ u8g.setPrintPos(0,20); u8g.print("ESP WebServer"); u8g.setPrintPos(0,40); u8g.print(inData); }while(u8g.nextPage()); Serial.println(inData); return inData; } // ----------------------------------------------------------------------------------------- void setup() { pinMode(led22, OUTPUT); digitalWrite(led22, LOW); Serial.begin(115200); Serial1.begin(115200); // your esp's baud rate might be different if ( u8g.getMode() == U8G_MODE_BW ) u8g.setColorIndex(1); // pixel on u8g.setFont(u8g_font_unifont); u8g.firstPage(); do{ u8g.setPrintPos(0,20); u8g.print("ESP WebServer"); u8g.setPrintPos(0,40); u8g.print(LocalIP); }while(u8g.nextPage()); sendData("AT+RST\r\n",2000,DEBUG); // reset module delay(200); sendData("AT+GMR\r\n",2000,DEBUG); // check version sendData("AT+CWMODE=3\r\n",1000,DEBUG); // configure as access point (working mode: AP+STA) sendData("AT+CWLAP\r\n",3000,DEBUG); // list available access points sendData("AT+CWJAP=\"\",\" \"\r\n",5000,DEBUG); // join the access point delay(200); LocalIP = getLocalIP(sendData("AT+CIFSR\r\n",1000,DEBUG)); // get ip address delay(500); sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections delay(200); sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80 interrupts(); } // ----------------------------------------------------------------------------------------- //ISR(USART1_RX_vect){ //} // ----------------------------------------------------------------------------------------- void loop() { if(Serial1.available()) { // check if the esp is sending a message if(Serial1.find("+IPD,")) { delay(1000); // wait for the serial buffer to fill up (read all the serial data) // get the connection id so that we can then disconnect // the ASCII decimal value and 0 (the first decimal number) starts at 48 int connectionId = Serial1.read()-48; // subtract 48 because the read() function returns Serial1.find("pin="); // advance cursor to "pin=" // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10 int pinNumber = (Serial1.read()-48)*10; // get second number, then add to the first number pinNumber += (Serial1.read()-48); pinNumber += 10; // to use pin 22 as this WEB send from No 11 ... digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin Serial1.flush(); // make close command String closeCommand = "AT+CIPCLOSE="; closeCommand+=connectionId; // append connection id closeCommand+="\r\n"; sendData(closeCommand,1000,DEBUG); // close connection if(!digitalRead(pinNumber)) LEDstatus = " LED ON "; else LEDstatus = " LED OFF "; Data2Web(LEDstatus); } } if(LEDON){ //Data2Web(); LEDON = !LEDON; } count++; if(count > 1000000){ LEDON = !LEDON; count = 0; } } // ----------------------------------------------------------------------------------------- // in order to work this function, you need to know // AT+CIPMUX=0 //call just one time at initialization // AT+CIPSERVER=0,turn off server on port 80 // ----------------------------------------------------------------------------------------- void Data2Web(String info){ // TCP connection String cmd = "AT+CIPSTART=4,\"TCP\",\""; // multi so use channel 4 cmd += "192.168.100.8"; // Web server IP cmd += "\",80\r\n"; delay(500); Serial1.print(cmd); Serial.print(cmd); delay(500); if(Serial1.find("ERROR")){ Serial.println("AT+CIPSTART error"); return; } // prepare GET string to send - page to get - esp8266.php String getStr = "GET /tmp/esp8266.php?apples="; // String to display getStr += String(apple++); getStr += info; getStr += " from ESPv4\r\n\r\n"; cmd = "AT+CIPSEND=4,"; // multi so use channel 4 cmd += String(getStr.length()); // send data length Serial1.println(cmd); Serial.println(cmd); delay(200); // need some delay for '>' if(Serial1.find(">")){ Serial1.print(getStr); Serial.print(getStr); }else{ Serial1.print("AT+CIPCLOSE=4\r\n"); // alert user Serial.println("AT+CIPCLOSE=4"); } } // ----------------------------------------------------------------------------------------- /* * Name: sendData * Description: Function used to send data to ESP8266. * Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no) * Returns: The response from the esp8266 (if there is a reponse) */ String sendData(String command, const int timeout, boolean debug) { String response = ""; Serial1.print(command); // send the read character to the esp8266 long int time = millis(); while( (time+timeout) > millis()) { while(Serial1.available()) { // The esp has data so display its output to the serial window char c = Serial1.read(); // read the next character. response+=c; } } if(debug) { Serial.print(response); } return response; } // ----------------------------------------------------------------------------------------- void getReply(int wait){ int tempPos = 0; LocalIP = ""; long int time = millis(); while( (time + wait) > millis()){ while(Serial1.available()){ char c = Serial1.read(); Serial.print(c); if (tempPos < 500){ //reply[tempPos] = c; LocalIP += c; tempPos++; } } //reply[tempPos] = 0; } if (DEBUG) { Serial.println(LocalIP); u8g.firstPage(); do{ u8g.setPrintPos(0,40); u8g.print(LocalIP); }while(u8g.nextPage()); } } // -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
'IoT_ESP8266' 카테고리의 다른 글
ESP8266 - ESP-01 : 온도및 습도 모니터링 OLED/Web (0) | 2018.12.30 |
---|---|
ESP-01 with OLED (1) | 2018.12.30 |
Arduino IDE 이용 ESP8266 웹서버 (0) | 2018.08.14 |
IoT - 사물인터넷 기초 자료 (0) | 2017.01.30 |
ESP8266 과 서버와 통신 (Synology) (0) | 2017.01.27 |