Thansk for responding Lowell, I was working with your library for a few hours last night and didn’t have much luck. I love the concept of the library, it’s priceless for home security. Basically I was trying to get the D7 onboard LED to turn on and off and couldn’t figure out what I was doing wrong, here’s my code:
//Program Start
#include "DailyTimerSpark.h"
#define SUN_UPDATE_INTERVAL 30*60*1000UL
const char* cityLocation = "Princeton"; //City for my Photon
const char* stateLocation = "NJ"; // State for my Photon
char publishString[125] = "";
char timeVars[125] = "";
struct deviceTime{
int Hour;
int Minute;
};
deviceTime sunset = {18,30};
deviceTime sunrise = { 5,30};
DailyTimer timer1(1, 24, 1, 30, EVERY_DAY, FIXED); // optional callback function for random number generation, see below example
DailyTimer timer2(12, 00, 13, 0, SATURDAY, FIXED);
// default is FIXED, this will randomize the start time only
DailyTimer timer3( 8, 30, 23, 30, WEEKENDS);
// creates with a default fixed start time and end time
bool timer1_LastState = false;
bool timerState;
uint32_t lastUpdateTime = 0;
uint32_t lastSunTime = 0;
void setup()
{
timer3.begin(); // use this for syncing the state change tools startTrigger() and endTrigger() on startup
timer2.begin();
timer1.begin();
Serial.begin(9600);
pinMode(D7, OUTPUT);
strcpy(publishString, "{\"my-city\": \"");
strcat(publishString, cityLocation);
strcat(publishString, "\", \"my-state\": \"");
strcat(publishString, stateLocation);
strcat(publishString, "\" }");
Particle.function("ToggleLED", toggleLED);
Particle.subscribe(System.deviceID(), webhookHandler, MY_DEVICES);
Particle.variable("State", timerState);
Particle.variable("SunTime", timeVars, STRING);
timer1.setRandomOffset(45, RANDOM_END);
//timer1.setDaysActive(WEEKDAYS); // Set the timer to be active on weekdays
//timer1.setDaysActive(B10101010); // or define a custom day mask...
SMTWTFS0 example: Sunday, Tuesday, Yhursday, Saturday
//timer1.setRandomDays(4); // or four random days per week
//timer3.setRandomOffset(5); // Change random start time, +/- 5 minutes... max 59 mins, default is 15mins
//Serial.println(timer1.getDays(), BIN); // getDays() returns active days as a byte in the format above
Particle.publish("pushover", "Timer Restart", 60, PRIVATE);
Particle.publish("sun_time", publishString, 60, PRIVATE);
}
void loop()
{
timerState = timer1.isActive();
if(timerState != timer1_LastState)
{
if(timerState)
{
digitalWrite(D7, HIGH);
Serial.println("LED is ON");
Particle.publish("pushover", "LED is ON", 60, PRIVATE);
}
else
{
digitalWrite(D7, LOW);
Serial.println("LED is OFF");
Particle.publish("pushover", "LED is OFF", 60, PRIVATE);
}
timer1_LastState = timerState;
}
if(timer2.startTrigger())
{
Serial.println("Timer 2 FIRED!");
Particle.publish("pushover", "Timer2 Fired", 60, PRIVATE);
}
if(timer3.startTrigger())
{
Serial.println("Timer 3 FIRED!");
Particle.publish("pushover", "Timer3 Fired", 60, PRIVATE);
}
if(timer3.endTrigger())
{
Serial.println("Timer 3 Expired!");
Particle.publish("pushover", "Timer3 is Inactive", 60, PRIVATE);
}
if(millis() - lastUpdateTime > 1000UL)
{
char timeBuffer[32] = "";
Serial.println(Time.timeStr());
lastUpdateTime += 1000UL;
}
if(millis() - lastSunTime > SUN_UPDATE_INTERVAL)
{
Particle.publish("sun_time", publishString, 60, PRIVATE);
lastSunTime += SUN_UPDATE_INTERVAL;
}
}
uint32_t customSeedGenerator()
{
Serial.println("New Seed Created");
return random(0xFFFFFFFF);
}
void webhookHandler(const char *event, const char *data)
{
if (strstr(event, "sun_time"))
{
gotSunTime(event, data);
}
}
void gotSunTime(const char * event, const char * data)
{
deviceTime currentTime;
char sunriseBuffer[25] = "";
strcpy(sunriseBuffer, data);
sunrise.Hour = atoi(strtok(sunriseBuffer, "\"~"));
sunrise.Minute = atoi(strtok(NULL, "~"));
sunset.Hour = atoi(strtok(NULL, "~"));
sunset.Minute = atoi(strtok(NULL, "~"));
currentTime.Hour = atoi(strtok(NULL, "~"));
currentTime.Minute = atoi(strtok(NULL, "~"));
Time.zone(0);
Time.zone(utcOffset(Time.hour(), currentTime.Hour));
timer1.setStartTime(sunset.Hour, sunset.Minute);
//timer1.setEndTime(sunrise.Hour, sunrise.Minute);
char buffer[125] = "";
sprintf(buffer, "Sunrise=%02d:%02d, Sunset=%02d:%02d", sunrise.Hour,
sunrise.Minute, sunset.Hour, sunset.Minute);
strcpy(timeVars, buffer);
}
int utcOffset(int utcHour, int localHour) // sorry Baker Island, this won't work for you (UTC-12)
{
if (utcHour == localHour)
{
return 0;
}
else if (utcHour > localHour)
{
if (utcHour - localHour >= 12)
{
return 24 - utcHour + localHour;
}
else
{
return localHour - utcHour;
}
}
else
{
if (localHour - utcHour > 12)
{
return localHour - 24 - utcHour;
}
else
{
return localHour - utcHour;
}
}
}
int toggleLED(String command)
{
if (command == "1")
{
digitalWrite(D7, HIGH);
}
else if (command == "0")
{
digitalWrite(D7, LOW);
}
else if(command == "-1")
{
digitalWrite(D7, !digitalRead(D7));
}
char message[125] = "";
sprintf(message, "LED is %s", digitalRead(D7)? "ON" : "OFF");
Particle.publish("pushover", message, 60, PRIVATE);
return digitalRead(D7);
}
The code verified and I thought that I had my code formatted properly?
DailyTimer timer1(1, 24, 1, 30, EVERY_DAY, FIXED);
Thanks for your patience.
Peace,
EJ