I have a particle photon 2 monitoring garage doors. My code is fairly straightforward but somehow the device is using hundreds of thousands of operations per day. It has been running for years fine without doing so. I switched out the photon board twice just hoping something was off but had no luck. I can watch the console to monitor and do not see events but Particle's report shows them but of course it doesn't show time of day, etc.
Here's my code:
// Constants for clarity
const int PIN_RIGHT_GARAGE = D0;
const int PIN_LEFT_GARAGE = D4;
const unsigned long ALERT_INTERVAL = 600000; // 10 minutes
// Timing variables (independent for each door)
unsigned long lastAlertRight = 0;
unsigned long lastAlertLeft = 0;
bool state = HIGH;
void setup() {
pinMode(PIN_LEFT_GARAGE, INPUT_PULLUP);
pinMode(PIN_RIGHT_GARAGE, INPUT_PULLUP);
Particle.function("leftstatus", getStatusLeftGarage);
Particle.function("rightstatus", getStatusRightGarage);
Particle.function("getbothgaragestatus", getStatusBothGarage);
// Particle.function("switch1", getSwitchStatus);
}
int getStatusBothGarage(String args) {
int left = digitalRead(PIN_LEFT_GARAGE); // 1 if closed, 0 if open
int right = digitalRead(PIN_RIGHT_GARAGE); // 1 if closed, 0 if open
if (left == 0 && right == 0) return 1; // Both open
if (left == 1 && right == 1) return 2; // Both closed
if (left == 0 && right == 1) return 3; // Left open, Right closed
if (left == 1 && right == 0) return 4; // Right open, Left closed
return -1;
}
int getStatusLeftGarage(String args) { return digitalRead(PIN_LEFT_GARAGE); }
int getStatusRightGarage(String args) { return digitalRead(PIN_RIGHT_GARAGE); }
//int getSwitchStatus(String args) { return state; }
void indicateActivity() {
RGB.control(true);
RGB.color(243, 136, 14); // Orange
delay(100); // Short blink to show it processed
RGB.color(47, 124, 27); // Green
RGB.control(false);
}
void loop() {
unsigned long now = millis();
// Check once per hour or similar to see if it's our "Reset Day"
// Time.weekday() returns 1 (Sun) through 7 (Sat)
// We can reset every Wednesday (4) at 3:00 AM
if (Time.weekday() == 4 && Time.hour() == 3 && Time.minute() == 0) {
System.reset();
}
// Right Garage Logic
if (digitalRead(PIN_RIGHT_GARAGE) == LOW) {
if (now - lastAlertRight >= ALERT_INTERVAL) {
lastAlertRight = now;
indicateActivity();
Particle.publish("garage_alert", "RightGarage", PRIVATE);
}
}
// Left Garage Logic
if (digitalRead(PIN_LEFT_GARAGE) == LOW) {
if (now - lastAlertLeft >= ALERT_INTERVAL) {
lastAlertLeft = now;
indicateActivity();
Particle.publish("garage_alert", "LeftGarage", PRIVATE);
}
}
}
