I am a noob that is trying to setup a notification engine for my home alarm system. I am monitoring the alarm out on the alarm with an analogRead function. If the pin goes high enough, it will visit a web page on my home server that will trigger an email alert to my cell. That function is working okay, but I tried adding a relay into the mix to reboot the alarm box after the alarm has sounded and then stopped. The reason I want this is due to I will be going on a trip and I want the ability to re-arm the alarm in case the motion detector gives a false positive and triggers the alarm. The issue that I am having, is it seems that the "rebooting alarm" loop is the only thing running, but it is not triggering the relay. All I see in the serial monitor is: "rebooting alarm". I have tested the relay, and it does work; so I think that it is an issue with my code. I would appreciate any advice or suggestions.
Thank you
My Code:
/*
Door Alarm (notifier)
Uses the Ethernet shield to notify us when there is an alarm sounding
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x07, 0x9E };
IPAddress server(10,0,0,21);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
//int emailState = 0;
int analogPin = 0;
int val = 0;
int alarmState = 0;
long previousMillis = 0; // will store last time email was sent
long interval = 10000; // interval at which to send emails
void setup() {
pinMode(7, OUTPUT);
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop(){
digitalWrite(7, HIGH);
unsigned long currentMillis = millis();
val = analogRead(analogPin); // read the input pin
if (val >= 600) {
if(currentMillis - previousMillis > interval) //added this in to provide a delay function so I do not get as many email alerts.
{
previousMillis = currentMillis;
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /email.php HTTP/1.0");
client.println();
Serial.println("disconnecting.");
client.flush();
client.stop();
}
alarmState = 1;
Serial.println("alarmState = 1");
}
}
if (val < 600) {
if (alarmState = 1) {
Serial.println("rebooting alarm");
digitalWrite(7,LOW);
delay(2000);
digitalWrite(7, HIGH);
alarmState = 0;
}
}
}