Hi, Im trying to send an sms when my device looses AC power ( I have DC backup)
I use this to determine when the state changes.
pinMode(ackinput, INPUT_PULLUP);
void setup()
pinMode(ackinput, INPUT_PULLUP);
{
digitalWrite(ackinput,LOW);
}
void loop ()
{
acState1 = digitalRead(ackinput);
if (acState1 != lastacState1)
{
if (digitalRead(ackinput) == HIGH) {
lastacState1 = acState1;
Serial.println(F("AC POWER FAIL"));
error = _4G.sendSMS(phone_number, "AC Power Fail");
}
My issue is I get an SMS when I loose power, and when i plug it back in. I only need to know when its unplugged.
If i dont implement the State =LastState code, then i get a lot of AC Failed messages as long as im unplugged.
EDITED: Added pinMode to the Setup portion to reflect actual code.
acState1 = digitalRead(ackinput);
if (acState1 != lastacState1)
{
if (digitalRead(ackinput) == HIGH) {
Why do you need to read the pin again? You know that the current state, acState1, is not the same as it was last time, and you know the current state. No need to read it again.
All function calls, like pinMode() need to happen in setup() or loop() or some other function, not outside of any function.
The value in lastacState1 needs to be set regardless of what the current value of acState1 is.
Why bother to set the pinMode to INPUT_PULLUP and then disable the internal pullup?
When you make changes to your code, please post the latest version, in a new post, so that we can keep up.
Because oddly enough if i make it only an INPUT, I get floods of sms messages.
I have a 120V power supply that has a battery backup, and contacts that close when AC Fails
Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"
C:\Users\DaD\AppData\Local\Temp\arduino_modified_sketch_487141\sketch_jul25a.ino: In function 'void setup()':
sketch_jul25a:14: error: a function-definition is not allowed here before '{' token
{
^
sketch_jul25a:28: error: expected '}' at end of input
}
^
exit status 1
a function-definition is not allowed here before '{' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Did you try to compile the code before posting? Aren't you missing something to close the setup() function?
Because oddly enough if i make it only an INPUT, I get floods of sms messages.
I have a 120V power supply that has a battery backup, and contacts that close when AC Fails
Something is wrong here. INPUT_PULLUP is correct for reading a contact closure which connects the input pin to ground. Can you provide a hand drawn schematic of your circuit?
You can make the state change detection one sided to detect failure and not reconnection
if (acState1 == LOW and acState1 != lastacState1 )
The assignment of acState1 to lastacState1 should be made outside of the conditional section.
I don't see how you know what the code does. I have not seen code that actually compiles.
Here is an example sketch that does what cattledog mentioned.
You can make the state change detection one sided to detect failure and not reconnection
This will send the message over the serial port only on switch closure (switch goes HIGH to LOW). It sends nothing when the switch goes LOW to HIGH. It uses the method from the state change detection tutorial.
I commented out the "/error = _4G.sendSMS(phone_number, "AC Power Fail");" cause the error variable is not declared and I think you are missing more code to send an SMS.
const byte ackinput = 5;
boolean acState1 = 1;
boolean lastacState1 = 1;
void setup()
{
Serial.begin(9600);
pinMode(ackinput, INPUT_PULLUP);
}
void loop()
{
acState1 = digitalRead(ackinput);
if (acState1 != lastacState1)
{
if (acState1 == LOW) // only send message if the switch goes HIGH to LOW
{
Serial.println(F("AC POWER FAIL"));
//error = _4G.sendSMS(phone_number, "AC Power Fail");
}
else
{
// do something else when the switch goes LOW to HIGH
//Serial.println(F("AC POWER RESTORED"));
}
}
lastacState1 = acState1;
}
I have a 120V power supply that has a battery backup, and contacts that close when AC Fails
Are you certain that the contacts don't open when AC fails? Are there wiring options for normally closed or normally open? Do you have any specifications or data sheets for the power supply?
groundFungus:
I don't see how you know what the code does. I have not seen code that actually compiles.
Here is an example sketch that does what cattledog mentioned.
This will send the message over the serial port only on switch closure (switch goes HIGH to LOW). It sends nothing when the switch goes LOW to HIGH. It uses the method from the state change detection tutorial.
I commented out the "/error = _4G.sendSMS(phone_number, "AC Power Fail");" cause the error variable is not declared and I think you are missing more code to send an SMS.
if (acState1 == LOW) // only send message if the switch goes HIGH to LOW
{
Serial.println(F("AC POWER FAIL"));
//error = _4G.sendSMS(phone_number, "AC Power Fail");
}
else
{
// do something else when the switch goes LOW to HIGH //Serial.println(F("AC POWER RESTORED"));
}
}
lastacState1 = acState1;
}
Thanks for the code and explanation. I have implimented that and whats still extreemely odd is I still get a notification when I turn the AC back on.. I test with a multi meter and that contact is only closing when i loose ac, it doesnt cylce really fast like the println is showing. I am using this.. hover over photo to see contacts https://www.amazon.com/gp/product/B0137IP6CI/ref=ppx_yo_dt_b_asin_title_o06_s02?ie=UTF8&psc=1
cattledog:
Are you certain that the contacts don't open when AC fails? Are there wiring options for normally closed or normally open? Do you have any specifications or data sheets for the power supply?
N/O
I am using this...hover over to see photo of contacts