/*
참고 사이트 :
http://makewith.co/page/project/1004/story/2363/ // 무선으로 LED 켜기
http://inboony.tistory.com/42 // ESP8266 모듈에서 데이터 보내기
*/
#include <SoftwareSerial.h>
#include <ESP8266.h>
#include <ESP8266Client.h>
#define DEBUG true
#define TO (1000)
int arduRx=2,arduTx=3;
SoftwareSerial esp8266(arduRx,arduTx); //RX는 2번 핀, TX는 3번 핀
ESP8266 wifi(esp8266); // esp8266 모듈을 사용하기 위한 객체 wifi를 시리얼 통신 객체를 사용하여 생성
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // PC-아두이노 간 시리얼 연결
esp8266.begin(9600); // 아두이노-ESP8266 모듈간 연결
wifi.begin(); // ESP8266 제어 시리얼 통신 연결
/* ESP8266 설정 AT Command 이용*/
sendData("AT+RST\r\n", TO*2, DEBUG); //reset module
sendData("AT+CWMODE=1\r\n", TO, DEBUG); // Client mode
sendData("AT+CWJAP=\"<ssid>\",\"<password>\"\r\n", TO*5, DEBUG); //사용할 공유기 설정. SSID와 비밀번호는 지웠습니다.
sendData("AT+CIPMUX=1\r\n", TO, DEBUG); //multiple connections 설정
// sendData("AT+CIPSERVER=1,7777\r\n", TO, DEBUG); // 공유기와의 연결 포트 번호를 7777번으로 설정. default 포트는 333.
sendData("AT+CIFSR\r\n", TO, DEBUG); // 부여받은 IP 번호 확인
// 웹서버로 TCP 연결 하는 부분. 웹 서버 아이피와 포트 여기서 수정할 것. 웹서버와의 link ID는 1번
Serial.println("# Trying to Connect Web Server...");
while(sendData("AT+CIPSTART=1,\"TCP\",\"<webserver IP>\",<webserver port>\r\n",TO*5,DEBUG)[54]=='E') {
Serial.println("# ReTrying to Connect Web Server After Close All Connection");
sendData("AT+CIPCLOSE=5\r\n", TO, DEBUG); // 모든 연결 강제종료
sendData("AT+CIPMUX=1\r\n", TO, DEBUG); // multiple connections 설정
delay(TO*3);
}
Serial.println("# Connecting is complete.\n-----------loop function start------------");
}
void loop() {
short temp=99, humi=80;
esp8266.print("AT+CIPSEND=1,100\r\n");
delay(TO/2);
esp8266.print("GET /ForESP/ESPReceiver.jsp?id=1&temp=");
esp8266.print(temp); // dummy data
esp8266.print(" HTTP/1.1\r\n"); // dummy data
delay(TO*5);
}
/* 아두이노-ESP8266 간 통신 함수*/
String sendData(String command, const int timeout, boolean debug){
String response = "";
esp8266.print(command); //command를 ESP8266에 보냄
long int time=millis();
while((time+timeout)>millis()){
while(esp8266.available()){
/*esp가 가진 데이터를 시리얼 모니터에 출력하기 위함*/
char c=esp8266.read(); //다음 문자를 읽어옴
response+=c;
}
}
if(debug){
Serial.print(response);
}
return response;
}
|