#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <ESP32Servo.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
Servo servoPakan;
IPAddress timeServer(162, 159, 200, 1);// id.pool.ntp.org
const int timeZone = 7; // Jakarta, Bangkok, Hanoi Time
WiFiUDP Udp;
unsigned int localPort = 8888;
time_t prevDisplay = 0;
WiFiClient espClient;
int status = WL_IDLE_STATUS;
void InitWiFi()
{
Serial.println("Connecting to AP ......");
// attempt to connect to WiFi network
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("...");
}
Serial.println("Connected to AP");
}
}
void setup() {
servoPakan.attach(33);
InitWiFi();
Serial.begin(115200);
while (!Serial) ;
// get and set the time from the RTC
Udp.begin(localPort);
setSyncProvider(getNtpTime);
//setSyncProvider(RTC.get);
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
// to test your project, you can set the time manually
//setTime(21,22,0,10,22,22); // set time to Saturday 8:29:00am Jan 1 2011
// create the scheduled tasks, to trigger functions at specific times
Alarm.alarmRepeat(23,40,10,feedingPagi);
Alarm.alarmRepeat(23,40,20,feedingSiang);
Alarm.alarmRepeat(23,40,30,feedingMalam);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
reconnect();
}
digitalClockDisplay();
Alarm.delay(1000);
}
void feedingPagi() {
Serial.println("Eksekusi Feeding ke-1");
servoPakan.write(30);
delay(3000);
servoPakan.write(90);
}
void feedingSiang() {
Serial.println("Eksekusi Feeding ke-2");
servoPakan.write(30);
delay(3000);
servoPakan.write(90);
}
void feedingMalam() {
Serial.println("Eksekusi Feeding ke-3");
servoPakan.write(30);
delay(3000);
servoPakan.write(90);
}
void digitalClockDisplay() {
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}