Hey there! I'm semi-noob so, I apologize if my question/ problem is basic.
I am trying to get an Arduino to sync time with network time. (A local network NTP server).
And then based on that time Open/Close a relay every 5 seconds past the hour, every hour.
So, at:
1:00:05AM - Close relay for 10 seconds, open relay
2:00:05AM - Close relay for 10 seconds, open relay
3:00:05AM - Close relay for 10 seconds, open relay
etc.
I can get it to control the relay, and I can get it to sync to NTP time, but I can't get it to do both.
Any suggestions?
See the following code: (When compiling I get "'alarm1' was not declared in this scope".)
Thanks in advance.
Arduino UNOr3
Ethernet shield
2 Relay Module and ZS-042 Real Time Clock module - DS3231
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
/* ******** Ethernet Card Settings ******** */
// Set this to your Ethernet Card Mac Address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x23, 0x36 };
/* ******** NTP Server Settings ******** */
/* us.pool.ntp.org NTP server
(Set to your time server of choice) */
IPAddress timeServer(192, 168, 11, 66);
/* Set this to the offset (in seconds) to your local time
This example is GMT - 8 */
const long timeZoneOffset = -28800;
/* Syncs to NTP server every 15 seconds for testing,
set to 1 hour or more to be reasonable */
unsigned int ntpSyncTime = 3600;
/* ALTER THESE VARIABLES AT YOUR OWN RISK */
// local port to listen for UDP packets
unsigned int localPort = 8888;
// NTP time stamp is in the first 48 bytes of the message
const int NTP_PACKET_SIZE= 48;
// Buffer to hold incoming and outgoing packets
byte packetBuffer[NTP_PACKET_SIZE];
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
// Keeps track of how long ago we updated the NTP server
unsigned long ntpLastUpdate = 0;
// Check last time clock displayed (Not in Production)
time_t prevDisplay = 0;
int RELAY1 = A0;
int RELAY2 = A1;
int delayValue = 1000;
void setup() {
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
Alarm.alarmRepeat(0,0,5,alarm1);
Alarm.alarmRepeat(1,0,5,alarm1);
Alarm.alarmRepeat(2,0,5,alarm1);
Alarm.alarmRepeat(3,0,5,alarm1);
Alarm.alarmRepeat(4,0,5,alarm1);
Alarm.alarmRepeat(5,0,5,alarm1);
Alarm.alarmRepeat(6,0,5,alarm1);
Alarm.alarmRepeat(7,0,5,alarm1);
Alarm.alarmRepeat(8,0,5,alarm1);
Alarm.alarmRepeat(9,0,5,alarm1);
Alarm.alarmRepeat(10,0,5,alarm1);
Alarm.alarmRepeat(11,0,5,alarm1);
Alarm.alarmRepeat(12,0,5,alarm1);
Alarm.alarmRepeat(13,0,5,alarm1);
Alarm.alarmRepeat(14,0,5,alarm1);
Alarm.alarmRepeat(15,0,5,alarm1);
Alarm.alarmRepeat(16,0,5,alarm1);
Alarm.alarmRepeat(17,0,5,alarm1);
Alarm.alarmRepeat(18,0,5,alarm1);
Alarm.alarmRepeat(19,0,5,alarm1);
Alarm.alarmRepeat(20,0,5,alarm1);
Alarm.alarmRepeat(21,25,5,alarm1);
Alarm.alarmRepeat(22,0,5,alarm1);
Alarm.alarmRepeat(23,0,5,alarm1);
Serial.begin(9600);
// Ethernet shield and NTP setup
int i = 0;
int DHCP = 0;
DHCP = Ethernet.begin(mac);
//Try to get dhcp settings 30 times before giving up
while( DHCP == 0 && i < 30){
delay(1000);
DHCP = Ethernet.begin(mac);
i++;
}
if(!DHCP){
Serial.println("DHCP FAILED");
for(;;); //Infinite loop because DHCP Failed
}
Serial.println("DHCP Success");
//Try to get the date and time
int trys=0;
while(!getTimeAndDate() && trys<10) {
trys++;
}
}
// Do not alter this function, it is used by the system
int getTimeAndDate() {
int flag=0;
Udp.begin(localPort);
sendNTPpacket(timeServer);
delay(1000);
if (Udp.parsePacket()){
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
unsigned long highWord, lowWord, epoch;
highWord = word(packetBuffer[40], packetBuffer[41]);
lowWord = word(packetBuffer[42], packetBuffer[43]);
epoch = highWord << 16 | lowWord;
epoch = epoch - 2208988800 + timeZoneOffset;
flag=1;
setTime(epoch);
ntpLastUpdate = now();
}
return flag;
}
// Do not alter this function, it is used by the system
unsigned long sendNTPpacket(IPAddress& address)
{
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011;
packetBuffer[1] = 0;
packetBuffer[2] = 6;
packetBuffer[3] = 0xEC;
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.beginPacket(address, 123);
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
}
// Clock display of the time and date (Basic)
void clockDisplay(){
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
// Utility function for clock display: prints preceding colon and leading 0
void printDigits(int digits){
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
// This is where all the magic happens...
void loop() {
// Update the time via NTP server as often as the time you set at the top
if(now()-ntpLastUpdate > ntpSyncTime) {
int trys=0;
while(!getTimeAndDate() && trys<10){
trys++;
}
if(trys<10){
Serial.println("ntp server update success");
}
else{
Serial.println("ntp server update failed");
}
}
// Display the time if it has changed by more than a second.
if( now() != prevDisplay){
prevDisplay = now();
clockDisplay();
}
void alarm1(){
digitalWrite(RELAY1, LOW);
Serial.println("LAFF Channel ID ON");
digitalWrite(RELAY2, LOW);
Serial.println("COURT TV ID ON");
alarm.delay(10000); //10 Seconds (10000 Milliseconds): Both relays ON
digitalWrite(RELAY1, HIGH);
Serial.println("LAFF Channel ID OFF");
digitalWrite(RELAY2, HIGH);
Serial.println("COURT TV ID OFF");
}