Skip to content

Instantly share code, notes, and snippets.

@kenci
Created January 10, 2020 13:16
Show Gist options
  • Save kenci/8187bc0127d77b271ca4a736992c7eb7 to your computer and use it in GitHub Desktop.
Save kenci/8187bc0127d77b271ca4a736992c7eb7 to your computer and use it in GitHub Desktop.
Alarmsystem atmega328/168p
///////////
// Set UTC time from Serial 1607,yy,m,d,h,m,s
///////////
#include <EEPROM.h>
#include <TimeAlarms.h>
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/
#include <Time.h> //http://playground.arduino.cc/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
#include <Timezone.h> //https://github.com/JChristensen/Timezone
byte alarmOnID;
byte alarmOffID;
//#define DEBUG
//Timezone
//Central European Time (Frankfurt, Paris)
TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; //Central European Summer Time
TimeChangeRule CET = { "CET", Last, Sun, Oct, 3, 60 }; //Central European Standard Time
Timezone CE(CEST, CET);
TimeChangeRule *tcr; //pointer to the time change rule, use to get the TZ abbrev
time_t utc, t;
typedef struct AlarmSettings {
byte ID;
byte hours;
byte minutes;
} alarm;
#define ALARM_ON_PIN 4
#define ALARM_OFF_PIN 5
void setup(void)
{
Serial.begin(9600);
//setSyncProvider() causes the Time library to synchronize with the
//external RTC by calling getLocalRTC() every five minutes by default.
setSyncProvider(getLocalRTC);
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
pinMode(ALARM_ON_PIN, OUTPUT);
pinMode(ALARM_OFF_PIN, OUTPUT);
Serial.println("Setting Alarm times...");
//turn alarm led on
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
Alarm.alarmRepeat(23, 0, 0, AlarmON); // every day at 23:00
Alarm.alarmRepeat(6, 0, 0, AlarmOFF); // every day at 06:00
Serial.print("SystemTime: ");printDateTime(now());
Serial.print("RTCTime: ");printDateTime(RTC.get());
}
void loop(void)
{
Alarm.delay(0);
static time_t tLast;
tmElements_t tm;
//check for input to set the RTC, minimum length is 12, i.e. 1607,yy,m,d,h,m,s
//set UTC time, localtime will be set automatically
if (Serial.available() >= 12) {
//note that the tmElements_t Year member is an offset from 1970,
//but the RTC wants the last two digits of the calendar year.
//use the convenience macros from Time.h to do the conversions.
int c = Serial.parseInt();
if(c == 1607) {
int y = Serial.parseInt();
if (y >= 100 && y < 1000)
Serial << F("Error: Year must be two digits or four digits!") << endl;
else {
if (y >= 1000)
tm.Year = CalendarYrToTm(y);
else //(y < 100)
tm.Year = y2kYearToTm(y);
tm.Month = Serial.parseInt();
tm.Day = Serial.parseInt();
tm.Hour = Serial.parseInt();
tm.Minute = Serial.parseInt();
tm.Second = Serial.parseInt();
utc = makeTime(tm);
RTC.set(utc); //use the time_t value to ensure correct weekday is set
setTime(CE.toLocal(utc, &tcr));
Serial << F("RTC set to: ");
printDateTime(utc);
Serial << endl;
//dump any extraneous input
while (Serial.available() > 0) Serial.read();
}
}
}
#ifdef DEBUG
t = now();
if (t != tLast) {
tLast = t;
Serial.print("RTC: ");
printDateTime(RTC.get());
Serial.print("Local[system]: ");
printDateTime(t);
if (second(t) == 0) {
float c = RTC.temperature() / 4.;
float f = c * 9. / 5. + 32.;
Serial << F(" ") << c << F(" C ") << f << F(" F");
}
Serial << endl;
}
#endif
}
void pressButton(byte pin) {
digitalWrite(pin, HIGH);
delay(500);
digitalWrite(pin, LOW);
//Serial.println("Button pressed!");
}
// functions to be called when an alarm triggers:
void AlarmON() {
pressButton(ALARM_ON_PIN);
Serial.println("ALARM ON");
//alarmOnID = Alarm.getTriggeredAlarmId();
}
void AlarmOFF() {
pressButton(ALARM_OFF_PIN);
Serial.println("ALARM OFF");
//alarmOffID = Alarm.getTriggeredAlarmId();
}
String printDigits(int digits) {
String n = "";
// utility for digital clock display: prints preceding colon and leading 0
if (digits < 10)
n = "0";
return n + digits;
}
//print date and time to Serial
void printDateTime(time_t t)
{
printDate(t);
Serial << ' ';
printTime(t);
Serial.println();
}
//print time to Serial
void printTime(time_t t)
{
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
}
//print date to Serial
void printDate(time_t t)
{
printI00(day(t), 0);
Serial << monthShortStr(month(t)) << _DEC(year(t));
}
//Print an integer in "00" format (with leading zero),
//followed by a delimiter character to Serial.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
if (val < 10) Serial << '0';
Serial << _DEC(val);
if (delim > 0) Serial << delim;
return;
}
//Print an integer in "00" format (with leading zero).
//Input value assumed to be between 0 and 99.
void sPrintI00(int val)
{
if (val < 10) Serial.print('0');
Serial.print(val, DEC);
return;
}
//Print an integer in ":00" format (with leading zero).
//Input value assumed to be between 0 and 99.
void sPrintDigits(int val)
{
Serial.print(':');
if (val < 10) Serial.print('0');
Serial.print(val, DEC);
}
time_t getLocalRTC()
{
return CE.toLocal(RTC.get(), &tcr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment