내가 사는 곳은 시골입니다.
두 식구가 먹을 수 있는 만큼의 달걀을 위해 기르고 있습니다.
닭장은 외부 동물의 공격에 준비가 철저해야 합니다. 그래서 잠자는 곳과 생활 공간을 따로 분리하여 잠자는 곳은 쥐라도 들어가지 못하도록 완벽하게 지어어야 합니다.
자동문은 하루에 두번 저녁에 닫고, 아침에 열어주도록 합니다.
몇개월 전부터 현제 사용중에 있습니다.
계속 업그래이드 하고 있습니다.
준비물:
WiFi
ESP8266(esp-01 ) 8핀으로 WiFi 모듈입니다.
esp-01 아덥터입니다. GP0, GP2에 릴레이 스위치를 연결하여 220V 싱크로 모터를 연결합니다.
브레드 보드에 연결 합니다.
아두이노 우노를 이용합니다.TX,Rx,RES, END, 5V 를 사용합니다.
arduino uno |
Esp-01 |
Rx |
Rx |
|
Tx |
Tx |
|
RES |
END |
|
VCC |
CHPD |
|
VCC |
VCC |
|
END |
GP0 |
팜 웨어 할 때만 접속하고, 후에 실할 할 때는 릴레이를 연결한다. |
|
RST |
reset 할 때 사용하므로 연결선 한쪽 비워둔다. END에 살짝 접속하여 땐다 |
현재 저의 닭장에서 사용 중인 모터입니다. 아마도 길이가 40cm 정도 됩니다. 우연한 기회에 공짜로 소유하게 되었습니다.
전기 레인지에 회전판 모터입니다. CW, CCW양방향으로 회전하는 모터입니다. 속도는 느리지만 토크는 좋습니다. 이번 실제로 이용해 볼 계획입니다.
구입한다 해도 몇 천원 정도입니다.
코딩 편집 중에는 GI0,Gi2 단자에 LED를 연결하여 동작 상태를 검사합니다.
ntp server 접속
esp01-01
릴레이 스위치 두개
스위치 두개
모터
문
이용 할 소스코드입니다.
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// Replace with your network credentials
const char *ssid = "skyiptime7209";
const char *password = "skylife209";
byte close=18; //닫는 시간 저녁
byte open =6; // 여는 시간 아침
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "kr.pool.ntp.org"); // 인터넷으로 사용 할 사이트
String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
void setup() {
Serial.begin(115200);
pinMode(0,1);
pinMode(2,1);
digitalWrite(0,0);
digitalWrite(2,0);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
timeClient.begin();
timeClient.setTimeOffset(32400); // 9x3600초
//timeClient.setTimeOffset(0);
}
void loop() {
timeClient.update();
time_t epochTime = timeClient.getEpochTime(); //년월일 class
Serial.print("Epoch Time: ");
Serial.println(epochTime);
String formattedTime = timeClient.getFormattedTime(); // 시간, 요일 class
Serial.print("Formatted Time: ");
Serial.println(formattedTime);
int currentHour = timeClient.getHours();
Serial.print("Hour: ");
currentHour=currentHour;
Serial.println(currentHour);
int currentMinute = timeClient.getMinutes();
Serial.print("Minutes: ");
Serial.println(currentMinute);
int currentSecond = timeClient.getSeconds();
Serial.print("Seconds: ");
Serial.println(currentSecond);
String weekDay = weekDays[timeClient.getDay()];
Serial.print("Week Day: ");
Serial.println(weekDay);
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
Serial.print("Month day: ");
Serial.println(monthDay);
int currentMonth = ptm->tm_mon+1;
Serial.print("Month: ");
Serial.println(currentMonth);
String currentMonthName = months[currentMonth-1];
Serial.print("Month name: ");
Serial.println(currentMonthName);
int currentYear = ptm->tm_year+1900;
Serial.print("Year: ");
Serial.println(currentYear);
//Print complete date:
String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
Serial.print("Current date: ");
Serial.println(currentDate);
Serial.println("");
delay(2000);
// 설정 시간이되면 문을 열고 닫는 시간 // 실제 사용시는
currentHour됨
if(currentSecond==close){
digitalWrite(0, 1);
delay(10000); //열릴 때까지 10초간 대기
} else {
digitalWrite(0, 0); //접속되었던 릴레이가 원상 복귀됨
}
if(currentSecond==open){
digitalWrite(2, 1);
delay(10000); // 닫히는 시간 10초간 대기
} else {
digitalWrite(2, 0); //접속되었던 릴레이가 원상 복귀됨
}
}
*문을 열고 닫히는 시간은 10초 뿐이다. 거의 24시간 동안 릴레이가 접속된다면 발생열로 인해 장애를 일으킨다. 그러므로 충분한 동작을 시간을 적용하고 원상복귀를 해주어 장치의 보호를 해주기 위함이다.
출력 결과 입니다.
이용 할 항목은 시간과 달입니다. 계절에 따라 해가 뜨고 지는 시간에 맞추어 주기 위함입니다.
Epoch Time: 1736208508
Formatted Time: 00:08:28
Hour: 0
Minutes: 8
Seconds: 28
Week Day: Tuesday
Month day: 7
Month: 1
Month name: January
Year: 2025
Current date: 2025-1-7
자동문은 아침에 열고, 저녁에 닫은 하루에 두 번만 동작합니다.
그러므로 간단하게 분초 없이 시간만 이용합니다. 그리고 월마다 해가 뜨고 지는 시간이 다르므로 거기에 맞게 설정합니다.
우리나라 평균 일출, 일몰 시간입니다.
닭은 어두어지면 스스로 집에 들어 갑니다. 1월은 06시에 열어 주고, 일몰때는 18시에 닫아 주면 됩니다. 집 주위에 큰산이 이 있어 저녁에 빨리 어두어진다면 적절하게 설정하면 됩니다.
지금 저의 집에서 사용 중인 시간은 06열고 18시에 닫습니다.
날짜
|
일출시간
|
일몰시간
|
1월 1일 1월16일
|
07:47 07:45
|
17:25 17:38
|
2월 1일 2월16일
|
07:36 07:21
|
17:55 18:11
|
3월 1일 3월16일
|
07:03 06:43
|
18:26 18:39
|
4월 1일 4월16일
|
06:19 05:57
|
18:26 18:39
|
5월 1일 5월16일
|
05:38 05:23
|
19:21 19:34
|
6월 1일 6월16일
|
05:13 05:10
|
19:47 19:55
|
7월 1일 7월16일
|
05:14 05:23
|
19:57 19:53
|
8월 1일 8월16일
|
05:35 05:48
|
19:41 19:24
|
9월 1일 9월16일
|
06:01 06:14
|
19:02 18:40
|
10월 1일 10월16일
|
06:27 06:40
|
18:15 17:55
|
11월 1일 11월16일
|
06:56 07:12
|
17:35 17:21
|
12월 1일 12월16일
|
07:27 07:39
|
17:14 17:15
|
필요한 항목만 사용하기 위해 코딩을 간단히 하였습니다.
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// Replace with your network credentials
const char *ssid = "skyiptime7209";
const char *password = "leeleelee";
byte close=30;
byte open =5;
// 시간은 얻기 위한 클라이언트
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "kr.pool.ntp.org");
// HH:MM:SS
void setup() {
Serial.begin(115200);
//열고 닫기위한 비트
pinMode(0,1);
pinMode(2,1);
digitalWrite(0,0);
digitalWrite(2,0);
// 와이프이 연결
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// 시간을 가져오기 위한 클라이언트 초기화
timeClient.begin();
//표준시간과 우리나라 시간은 9시간 차이
timeClient.setTimeOffset(32400); // GMT+9
}
void loop() {
timeClient.update();
//년월일 사용
time_t epochTime = timeClient.getEpochTime();
// 시간, 분, 초, 요일
String formattedTime = timeClient.getFormattedTime();
// 시간
int currentHour = timeClient.getHours();
Serial.print("Hour: ");
currentHour=currentHour;
Serial.println(currentHour);
//분
int currentMinute = timeClient.getMinutes();
//초
int currentSecond = timeClient.getSeconds();
Serial.print("Seconds: ");
Serial.println(currentSecond);
//Get a time structure
//달
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
int currentMonth = ptm->tm_mon+1;
Serial.print("Month: ");
Serial.println(currentMonth);
//년
int currentYear = ptm->tm_year+1900;
String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
delay(2000);
// 모타 제어
if(currentSecond==close){
digitalWrite(0, 1);
delay(10000);
} else {
digitalWrite(0, 0);
}
if(currentSecond==open){
digitalWrite(2, 1);
delay(10000);
} else {
digitalWrite(2, 0);
}
}
결과
Seconds: 3
Month: 1
Hour: 8
개발 중에는 Seconds를 사용합니다. 완료 시에는 시간, 월만필요합니다.