1. 라이브러리 설치

https://github.com/Diaoul/arduino-ESP8266

1) 위 URL 접속 후, "download"버튼 클릭. 내려받은 zip 파일을 아두이노IDE 메뉴 "스케치 > 라이브러리 포함하기 > .zip 라이브러리 추가..." 클릭 후, 다운받은 zip파일을 추가함.

2) 예제, "Complete" 참조

 

 

PS. 아래 소스에서 wifi.read() 할 때, 다음 버퍼보다 큰 데이터가 수신되는 경우, 추가 코딩 필요함.

#include <SoftwareSerial.h>
#include "ESP8266.h"

#define DEBUG true

#define SSID "U+Net5B6F"
#define PASS "1000019118"
#define DST_IP "211.172.246.98" //baidu.com

SoftwareSerial esp8266Serial = SoftwareSerial(2, 3);
ESP8266 wifi = ESP8266(esp8266Serial);

void setup()
{
    Serial.begin(9600);

    // ESP8266
    esp8266Serial.begin(9600);
    wifi.begin();
    wifi.setTimeout(1000);

    /****************************************/
    /******       Basic commands       ******/
    /****************************************/
    // test
    Serial.print("test: ");
    Serial.println(getStatus(wifi.test()));

    // restart
    Serial.print("restart: ");
    Serial.println(getStatus(wifi.restart()));

    // getVersion
    char version[16] = {};
    Serial.print("getVersion: ");
    Serial.print(getStatus(wifi.getVersion(version, 16)));
    Serial.print(" : ");
    Serial.println(version);

    // setWifiMode
    ************************************/
    // joinAP
    Serial.print("joinAP: ");
    Serial.println(getStatus(wifi.joinAP(SSID, PASS)));
    
    // getIP
    Serial.println("getIP: ");
    sendData("AT+CIFSR\r\n", 1000, DEBUG); // wifi mode : 1:station,2:AP, 3:dual

    /****************************************/
    /******       TCP/IP commands      ******/
    /****************************************/
    //서버모드로 하기전에 다중접속모드를 on 해야, 서버시작시 오류가 발생하지 않음.
    sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);
    // createServer
    unsigned int port = 80;
    Serial.print("createServer: ");
    Serial.println(getStatus(wifi.createServer(port))); // port

}

void loop()
{
    /****************************************/
    /******        WiFi commands       ******/
    /****************************************/

    // read data
    unsigned int id;
    int length;
    int totalRead = 0;
    unsigned int bufferSize = 256;    
    char buffer[bufferSize] = {};

    if ((length = wifi.available()) > 0) {
      id = wifi.getId();
      if (length > 0) {
        do {        
          totalRead += wifi.read(buffer, bufferSize);
          Serial.print("Received ");
          Serial.print(totalRead);
          Serial.print("/");
          Serial.print(length);
          Serial.print(" bytes from client ");
          //Serial.print("from client ");
          Serial.print(id);
          Serial.println(": ");
          Serial.println((char*)buffer);
          wifi.flush();
        } while (totalRead < length);
      }
    }
}


void getConnectionStatue(ESP8266 wifi) {
    ESP8266ConnectionStatus connectionStatus;
    ESP8266Connection connections[5];
    unsigned int connectionCount;
    Serial.print("getConnectionStatus: ");
    Serial.print(getStatus(wifi.getConnectionStatus(connectionStatus, connections, connectionCount)));
    Serial.print(" : ");
    Serial.println(connectionCount);
    for (int i = 0; i < connectionCount; i++) {
      Serial.print(" - Connection: ");
      Serial.print(connections[i].id);
      Serial.print(" - ");
      Serial.print(getProtocol(connections[i].protocol));
      Serial.print(" - ");
      Serial.print(connections[i].ip);
      Serial.print(":");
      Serial.print(connections[i].port);
      Serial.print(" - ");
      Serial.println(getRole(connections[i].role));
    }
  delay(200);
}

String getStatus(bool status)
{
    if (status)
        return "OK";

    return "KO";
}

String getStatus(ESP8266CommandStatus status)
{
    switch (status) {
    case ESP8266_COMMAND_INVALID:
        return "INVALID";
        break;

    case ESP8266_COMMAND_TIMEOUT:
        return "TIMEOUT";
        break;

    case ESP8266_COMMAND_OK:
        return "OK";
        break;

    case ESP8266_COMMAND_NO_CHANGE:
        return "NO CHANGE";
        break;

    case ESP8266_COMMAND_ERROR:
        return "ERROR";
        break;

    case ESP8266_COMMAND_NO_LINK:
        return "NO LINK";
        break;

    case ESP8266_COMMAND_TOO_LONG:
        return "TOO LONG";
        break;

    case ESP8266_COMMAND_FAIL:
        return "FAIL";
        break;

    default:
        return "UNKNOWN COMMAND STATUS";
        break;
    }
}

String getRole(ESP8266Role role)
{
    switch (role) {
    case ESP8266_ROLE_CLIENT:
        return "CLIENT";
        break;

    case ESP8266_ROLE_SERVER:
        return "SERVER";
        break;

    default:
        return "UNKNOWN ROLE";
        break;
    }
}

String getProtocol(ESP8266Protocol protocol)
{
    switch (protocol) {
    case ESP8266_PROTOCOL_TCP:
        return "TCP";
        break;

    case ESP8266_PROTOCOL_UDP:
        return "UDP";
        break;

    default:
        return "UNKNOWN PROTOCOL";
        break;
    }
}

void sendData(String command, const int timeout, boolean debug) {
  String response = "";
  esp8266Serial.print(command);
  long int time = millis();

  while((time + timeout) > millis()) {
    while(esp8266Serial.available()) {
      char c = esp8266Serial.read();
      response += c;
    }
  }

  if(debug) {
    Serial.println(response);
  }
}
 
728x90

+ Recent posts