릴레이 모듈의 문제인지 esp-01의 문제인지 arduinoOTA.h를 사용하면 정상 작동이 안되어서 사용하지 않음
gpio3가 rx핀이므로 디버깅에 사용하면 input으로 이용할 수 없으므로 시리얼 프린트 기능을 못하게 막아둔다.
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#define DEBUG //주석해제하면 시리얼 프린트 사용가능함
#ifdef DEBUG //Serial.print가 rx 핀을 사용하므로 디버거할 경우가 아니면 사용하지 않아야 rx핀이 정상 작동한다.
#define Serial_p(x) Serial.println(x)
#define Serial_p2(x,y) Serial.println(x,y)
#define Serial_p3(x) Serial.print(x)
#define Serial_b(x) Serial.begin(x)
#define Serial_f(x,y) Serial.printf (x,y)
#else
#define Serial_p(x)
#define Serial_f(x)
#define Serial_b(x)
#define Serial_p2(x)
#define Serial_p3(x)
#endif
const char* ssid = "ssid"; // fill in here your router or wifi SSID
const char* password = "암호*"; // fill in here your router or wifi password
#define RELAY 0 // relay connected to GPIO0
ESP8266WebServer server(80);
void handleRoot(); // function prototypes for HTTP handlers
void handleRelayON();
void handleRelayOFF();
void handleNotFound();
void handleSetting();
String pwrstat = "OFF";
#define sw_pin 3 //button switch connected to esp01 gpio3 -> rx
int state = 0;
unsigned long start_time = 0;
unsigned long ptt_time = 60000;//60*1000
String trigger = "off";
void setup()
{
Serial_b(115200); // must be same baudrate with the Serial Monitor
pinMode(RELAY,OUTPUT);
digitalWrite(RELAY, HIGH); //,no일때, HIGH하면 안통함 nc 일때 low하면 통함
pinMode(sw_pin, INPUT);
// Connect to WiFi network
Serial_p();
Serial_p();
Serial_p3("Connecting to ");
Serial_p(ssid);
// 고정 IP 설정 부분
IPAddress ip(192, x, x, x); // 사용할 IP 주소
IPAddress gateway(192, x, x, 1); // 게이트웨이 주소
IPAddress subnet(255, 255, 255, 0); // 서브넷 주소
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial_p3(".");
}
Serial_p("");
Serial_p("WiFi connected");
server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
server.on("/relayon", HTTP_GET, handleRelayON); // on이면 도어락 열림
server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
server.on("/time_setting.cgi", HTTP_GET, handleSetting);
// Start the server
server.begin();
Serial_p("Server started");
// Print the IP address
Serial_p3("Use this URL to connect: ");
Serial_p3(WiFi.localIP());
Serial_p("/");
}
void loop()
{
server.handleClient();
//버튼 인식 부분, 눌러지면 ptt_time 에 지정된 시간 경과 후 릴레이에 신호를 보내 on/off함
state = digitalRead(sw_pin);
if (state == LOW) {
Serial_p("button pressed");
start_time = millis();
trigger = "on";
}
//버튼 누른 후 시간 경과 후 처리할 루틴
if (((millis() - start_time) > ptt_time) && (trigger == "on")) {
Serial_p("time condition passed");
Serial_p("RELAY=ON");
trigger = "off";
digitalWrite(RELAY, LOW); //릴레이 회로 닫힘, 도어락 열림 신호
printhtml("ON");
pwrstat = "ON";
delay(1000);
digitalWrite(RELAY, HIGH); //릴레이 회로 열림, 도어락 열림 신호 중지
}
delay(1);
}
void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
server.send(200, "text/html","RLY " + pwrstat + ", time "+ String(ptt_time)+"ms");// "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>");
}
void handleSetting(){
Serial_p("server.argName(0):" + server.argName(0));
if (server.argName(0) == "time") { //http://192.xxx.xxx.xxx/time_setting.cgi?time=60000
Serial_p(server.argName(0));
// printhtml(datetime);
ptt_time = server.arg(0).toInt(); //setting time
server.send(200, "text/html","RLY " + pwrstat + ", time "+ String(ptt_time)+"ms");
}
}
void handleRelayON() { //도어락 열림
Serial_p("RELAY=ON");
digitalWrite(RELAY,LOW); //릴레이 회로 닫힘, 도어락 열림 신호
printhtml("ON&OFF");
pwrstat = "OFF";
delay(1000);
digitalWrite(RELAY,HIGH); //릴레이 회로 열림, 도어락 열림 신호 중지
}
void handleNotFound(){
server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}
void printhtml(String stat){
String message = "<html>\n";
message += "<head><title>ESP8266 RELAY Control</title></head>\n";
message += "Relay is now: \n";
message += stat;
message += "\n</html>\n";
server.send(200, "text/html", message);
}
'diy > esp8266' 카테고리의 다른 글
esp-12 gpio핀 정리 (1) | 2023.11.13 |
---|---|
esp8266과 안드로이드 서버 통신하기 (0) | 2022.12.01 |
esp-01 릴레이를 이용한 도어락 열기 1 (0) | 2022.11.22 |
voc센서(GSBT11-P110, 공기질 측정기) 만들기 2 (0) | 2022.09.28 |
voc센서(GSBT11-P110, 공기질 측정기) 만들기 1 (0) | 2022.09.27 |