Last active
September 8, 2016 07:32
-
-
Save technobly/b64574b0f31e598c1560 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "application.h" | |
SYSTEM_MODE(SEMI_AUTOMATIC); | |
// ALL_LEVEL, TRACE_LEVEL, DEBUG_LEVEL, INFO_LEVEL, WARN_LEVEL, ERROR_LEVEL, PANIC_LEVEL, NO_LOG_LEVEL | |
Serial1DebugOutput debugOutput(9600, ALL_LEVEL); | |
unsigned long whenPublished = 0; | |
retained int publishCount = 0; | |
int resetPublishCount(String _) { | |
return publishCount = 0; | |
} | |
void setup() { | |
pinMode(D1, INPUT_PULLDOWN); | |
pinMode(D7, OUTPUT); | |
Particle.variable("c", publishCount); | |
Particle.function("r", resetPublishCount); | |
} | |
void blink() { | |
DEBUG(""); | |
for (int i = 0; i < 4; ++i) { | |
digitalWrite(D7, HIGH); | |
delay(100); | |
digitalWrite(D7, LOW); | |
delay(100); | |
} | |
} | |
void publishOnce() { | |
if (0 == whenPublished) { | |
Particle.connect(); | |
DEBUG("Particle.connect"); | |
waitUntil(Particle.connected); | |
DEBUG("Particle.connected"); | |
bool sent = Particle.publish("b", String(publishCount + 1)); | |
if (sent) { | |
DEBUG("publish sent"); | |
++publishCount; | |
whenPublished = millis(); | |
} else { | |
DEBUG("publish not sent"); | |
// blink D7 to indicate publish failed | |
blink(); | |
} | |
} | |
} | |
void sleepAfterDelay() { | |
// allow time for retransmission of the published message | |
if (0 != whenPublished && millis() - whenPublished > 5000) { | |
DEBUG("Going to sleep"); | |
delay(1000); | |
System.sleep(D1, RISING, 60*60); // Halt processor here for 1 hour | |
delay(1000); | |
DEBUG("waking up!"); | |
whenPublished = 0; // publish again when we wake up | |
} | |
} | |
void loop() { | |
publishOnce(); | |
sleepAfterDelay(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment