|
| 1 | +// Arduino MCP79412RTC Library |
| 2 | +// https://github.com/JChristensen/MCP79412RTC |
| 3 | +// Copyright (C) 2018 by Jack Christensen and licensed under |
| 4 | +// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html |
| 5 | +// |
| 6 | +// Example sketch: Power Outage Logger using Microchip MCP79412 RTC. |
| 7 | +// Assumes the RTC is running and set to UTC. |
| 8 | +// A maximum of 7 outages (power down/up times) can be logged in the |
| 9 | +// RTC's SRAM. |
| 10 | +// |
| 11 | +// Jack Christensen 23Aug2012 |
| 12 | + |
| 13 | +#include <MCP79412RTC.h> // https://github.com/JChristensen/MCP79412RTC |
| 14 | +#include <Streaming.h> // http://arduiniana.org/libraries/streaming/ |
| 15 | +#include <TimeLib.h> // https://github.com/PaulStoffregen/Time |
| 16 | +#include <Timezone.h> // https://github.com/JChristensen/Timezone |
| 17 | + |
| 18 | +#define FIRST_OUTAGE_ADDR 0x08 // address of first outage timestamps in RTC SRAM |
| 19 | +#define OUTAGE_LENGTH 8 // 8 data bytes for each outage (start and end timestamps, both are time_t values) |
| 20 | +#define MAX_OUTAGES 7 // maximum number of outage timestamp pairs that can be stored in SRAM |
| 21 | +#define MAX_OUTAGE_ADDR FIRST_OUTAGE_ADDR + OUTAGE_LENGTH * (MAX_OUTAGES - 1) // last outage address |
| 22 | +#define APP_ID 1 // APP_ID and 4 bytes of the RTC ID are stored in sram to provide |
| 23 | + // a way to recognize that the logging data structure has been initialized |
| 24 | +#define RTC_ID_LO 0x00 // lower 4 bytes of RTC unique ID are stored at sram addr 0x00 |
| 25 | +#define APP_ID_ADDR 0x04 // address of appID (1) |
| 26 | +#define NBR_OUTAGES_ADDR 0x05 // address containing number of outages currently stored in SRAM |
| 27 | +#define NEXT_OUTAGE_ADDR 0x06 // address containing pointer to next outage |
| 28 | +#define RFU_ADDR 0x07 // reserved for future use |
| 29 | + |
| 30 | +// US Eastern Time Zone (New York, Detroit) |
| 31 | +TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; // Daylight time = UTC - 4 hours |
| 32 | +TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; // Standard time = UTC - 5 hours |
| 33 | +Timezone myTZ(myDST, mySTD); |
| 34 | +TimeChangeRule *tcr; // pointer to the time change rule, used to get TZ abbrev |
| 35 | +time_t utc, local, lastUTC; |
| 36 | + |
| 37 | +void setup() |
| 38 | +{ |
| 39 | + Serial.begin(115200); |
| 40 | + |
| 41 | + setSyncProvider(RTC.get); // the function to get the time from the RTC |
| 42 | + Serial << "RTC SYNC"; |
| 43 | + if (timeStatus()!= timeSet) Serial << " FAIL"; |
| 44 | + Serial << endl; |
| 45 | + |
| 46 | + //logClear(); |
| 47 | + logOutage(); |
| 48 | +} |
| 49 | + |
| 50 | +void loop() |
| 51 | +{ |
| 52 | + // nothing here in loop() has anything to do with logging power outages, |
| 53 | + // we just print the time every second so that something is happening. |
| 54 | + utc = now(); |
| 55 | + if (utc != lastUTC) |
| 56 | + { |
| 57 | + lastUTC = utc; |
| 58 | + local = myTZ.toLocal(utc, &tcr); |
| 59 | + Serial << endl; |
| 60 | + printTime(utc, "UTC"); |
| 61 | + printTime(local, tcr -> abbrev); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// initialize the log data structure in the RTC SRAM if needed. |
| 66 | +// log a new outage if one occurred. |
| 67 | +// print out the outages logged. |
| 68 | +void logOutage() |
| 69 | +{ |
| 70 | + union { |
| 71 | + uint8_t b[8]; |
| 72 | + struct { |
| 73 | + uint32_t hi; |
| 74 | + uint32_t lo; |
| 75 | + }; |
| 76 | + } uniqueID; // 8-byte RTC "unique ID" with access to upper and lower halves |
| 77 | + |
| 78 | + uint32_t loID; // lower half of the unique ID read from sram |
| 79 | + uint8_t appID; // app ID read from sram |
| 80 | + uint8_t nOutage; // number of outages stored in sram |
| 81 | + uint8_t nextOutage; // address of next outage timestamps in sram |
| 82 | + uint8_t outageAddr; // outage address in sram |
| 83 | + time_t powerDown, powerUp; // power outage timestamps |
| 84 | + |
| 85 | + RTC.idRead(uniqueID.b); // get the RTC's ID |
| 86 | + loID = read32(RTC_ID_LO); // if already initialized, the lower half of the ID is stored at SRAM addr 0x00, |
| 87 | + appID = RTC.sramRead(APP_ID_ADDR); // and the app ID (1) is at addr 0x04. |
| 88 | + Serial << "RTC ID"; |
| 89 | + for (uint8_t i=0; i<8; i++) |
| 90 | + { |
| 91 | + Serial << (uniqueID.b[i] < 16 ? " 0" : " ") << _HEX(uniqueID.b[i]); |
| 92 | + } |
| 93 | + |
| 94 | + if ( loID != uniqueID.lo || appID != 1 ) // logging initialized? |
| 95 | + { |
| 96 | + write32(RTC_ID_LO, uniqueID.lo); // least significant half of the RTC unique ID |
| 97 | + RTC.sramWrite(APP_ID_ADDR, APP_ID); // app ID |
| 98 | + RTC.sramWrite(NBR_OUTAGES_ADDR, 0); // number of outages |
| 99 | + RTC.sramWrite(NEXT_OUTAGE_ADDR, FIRST_OUTAGE_ADDR); // next location for outage times |
| 100 | + RTC.sramWrite(RFU_ADDR, 0); // reserved for future use |
| 101 | + Serial << "Logging initialized" << endl; // no, do it now |
| 102 | + } |
| 103 | + |
| 104 | + // if an outage has occurred, record it |
| 105 | + if ( RTC.powerFail(&powerDown, &powerUp) ) |
| 106 | + { |
| 107 | + nOutage = RTC.sramRead(NBR_OUTAGES_ADDR); |
| 108 | + nextOutage = RTC.sramRead(NEXT_OUTAGE_ADDR); |
| 109 | + write32(nextOutage, powerDown); |
| 110 | + write32(nextOutage + 4, powerUp); |
| 111 | + nextOutage += OUTAGE_LENGTH; |
| 112 | + if (nextOutage > MAX_OUTAGE_ADDR) nextOutage = FIRST_OUTAGE_ADDR; |
| 113 | + RTC.sramWrite(NEXT_OUTAGE_ADDR, nextOutage); |
| 114 | + if (nOutage < MAX_OUTAGES) RTC.sramWrite(NBR_OUTAGES_ADDR, ++nOutage); |
| 115 | + } |
| 116 | + |
| 117 | + // print out all the outages logged |
| 118 | + nOutage = RTC.sramRead(NBR_OUTAGES_ADDR); |
| 119 | + nextOutage = RTC.sramRead(NEXT_OUTAGE_ADDR); |
| 120 | + outageAddr = nextOutage - OUTAGE_LENGTH; |
| 121 | + if (outageAddr < FIRST_OUTAGE_ADDR) outageAddr = MAX_OUTAGE_ADDR; |
| 122 | + Serial << endl << endl << "Power outages logged: " << _DEC(nOutage) << endl; |
| 123 | + for (uint8_t i=nOutage; i>0; i--) |
| 124 | + { |
| 125 | + powerDown = read32(outageAddr); |
| 126 | + powerUp = read32(outageAddr + 4); |
| 127 | + Serial << endl << _DEC(i) << ": Power down "; |
| 128 | + printTime(myTZ.toLocal(powerDown, &tcr), tcr -> abbrev); |
| 129 | + Serial << _DEC(i) << ": Power up "; |
| 130 | + printTime(myTZ.toLocal(powerUp, &tcr), tcr -> abbrev); |
| 131 | + outageAddr -= OUTAGE_LENGTH; |
| 132 | + if (outageAddr < FIRST_OUTAGE_ADDR) outageAddr = MAX_OUTAGE_ADDR; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// initialize the logging data structure and log data |
| 137 | +void logClear() |
| 138 | +{ |
| 139 | + for (uint8_t i=0; i<MAX_OUTAGE_ADDR + OUTAGE_LENGTH; i++) |
| 140 | + { |
| 141 | + RTC.sramWrite(i, 0); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// write a time_t or other uint32_t value to sram starting at addr |
| 146 | +void write32(uint8_t addr, uint32_t t) |
| 147 | +{ |
| 148 | + union { |
| 149 | + uint8_t b[4]; |
| 150 | + uint32_t t; |
| 151 | + } i; |
| 152 | + |
| 153 | + i.t = t; |
| 154 | + RTC.sramWrite(addr, i.b, 4); |
| 155 | +} |
| 156 | + |
| 157 | +// read a time_t or other uint32_t value from sram starting at addr |
| 158 | +time_t read32(uint8_t addr) |
| 159 | +{ |
| 160 | + union { |
| 161 | + uint8_t b[4]; |
| 162 | + time_t t; |
| 163 | + } i; |
| 164 | + |
| 165 | + RTC.sramRead(addr, i.b, 4); |
| 166 | + return i.t; |
| 167 | +} |
| 168 | + |
| 169 | +// Print time with time zone |
| 170 | +void printTime(time_t t, char *tz) |
| 171 | +{ |
| 172 | + sPrintI00(hour(t)); |
| 173 | + sPrintDigits(minute(t)); |
| 174 | + sPrintDigits(second(t)); |
| 175 | + Serial << ' ' << dayShortStr(weekday(t)) << ' '; |
| 176 | + sPrintI00(day(t)); |
| 177 | + Serial << ' ' << monthShortStr(month(t)) << ' ' << year(t) << ' ' << tz << endl; |
| 178 | +} |
| 179 | + |
| 180 | +// Print an integer in "00" format (with leading zero). |
| 181 | +// Input value assumed to be between 0 and 99. |
| 182 | +void sPrintI00(int val) |
| 183 | +{ |
| 184 | + if (val < 10) Serial << '0'; |
| 185 | + Serial << val; |
| 186 | + return; |
| 187 | +} |
| 188 | + |
| 189 | +// Print an integer in ":00" format (with leading zero). |
| 190 | +// Input value assumed to be between 0 and 99. |
| 191 | +void sPrintDigits(int val) |
| 192 | +{ |
| 193 | + Serial << ':'; |
| 194 | + if(val < 10) Serial << '0'; |
| 195 | + Serial << val; |
| 196 | +} |
0 commit comments