-
-
Save repcsi/4cc2bcf790e967c246ba2503654d245d to your computer and use it in GitHub Desktop.
Particle Boron Power Outage Detector
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
String str1,str2; | |
bool onUSB = false; | |
bool onBattery = false; | |
bool lowBattery = false; | |
unsigned long pwrCheckTimeStart;//to check power every 10sec | |
void setup() { | |
// INITIAL POWER CHECK | |
int powerSource = System.powerSource(); | |
if (powerSource == POWER_SOURCE_BATTERY) {// ON BATTERY | |
onBattery = true; | |
onUSB = false; | |
} | |
else{// ON USB | |
onBattery = false; | |
onUSB = true; | |
} | |
if(onBattery){//bootup message alert so we know things are back online | |
str1 = "HOME Booting..."; | |
str2 = "NO AC POWER"; | |
sendData(); | |
}else{ | |
str1 = "HOME Booting..."; | |
str2 = "AC POWER GOOD"; | |
sendData(); | |
} | |
pwrCheckTimeStart = millis(); | |
} | |
void loop() { | |
// POWER CHECK | |
if(millis()-pwrCheckTimeStart>10000){ | |
pwrCheckTimeStart = millis(); | |
int powerSource = System.powerSource(); | |
if (powerSource == POWER_SOURCE_BATTERY) {// ON BATTERY | |
if(!onBattery && onUSB){// CHANGED FROM USB TO BATTERY | |
onBattery = true; | |
onUSB = false; | |
str1 = "HOME"; | |
str2 = "AC POWER LOST"; | |
sendData(); | |
} | |
}else if(onBattery && !onUSB){// CHANGED FROM BATTERY TO USB | |
onBattery = false; | |
onUSB = true; | |
str1 = "HOME"; | |
str2 = "AC POWER IS ON"; | |
sendData(); | |
} | |
//and also check battery voltage | |
FuelGauge fuel; | |
float batteryVoltage = fuel.getVCell(); | |
if(batteryVoltage < 3.5){// if less than this, send an alert | |
if(!lowBattery){ | |
lowBattery=true; | |
str1 = "HOME"; | |
str2 = "LOW BATTERY"; | |
sendData(); | |
} | |
}else if(batteryVoltage>3.7){//little hysteresis to prevent multiple messages | |
lowBattery=false; | |
} | |
} | |
//******************** | |
} | |
void sendData(){ | |
unsigned long startConnectTime = millis(); | |
char pushMessage[50], pushName[50]; | |
str1.toCharArray(pushName, str1.length() + 1); | |
str2.toCharArray(pushMessage, str2.length() + 1); | |
Serial.println(str1); | |
Serial.println(str2); | |
String pushoverPacket = "[{\"key\":\"title\", \"value\":\""; | |
pushoverPacket.concat(str1); | |
pushoverPacket.concat("\"},"); | |
pushoverPacket.concat("{\"key\":\"message\", \"value\":\""); | |
pushoverPacket.concat(str2); | |
pushoverPacket.concat("\"}]"); | |
Particle.publish("pushover", pushoverPacket, PRIVATE);//then send to push safer so we get the notifications on our mobile devices | |
Serial.print(millis() - startConnectTime); | |
Serial.println("ms to connect"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment