Trouble with simple door alarm

Hello, I am trying to make a door alarm that will sound a siren and email me (via a php web page that is hosted on my server) and I am running into an issue with it. I should point out that this is my first Arduino project, and I am somewhat new to programming. The code verifies/compiles and uploads without issue to the Arduino. After the code is uploaded, nothing happens. If you guys have any ideas or suggestions; i would love to hear them.

Thanks.

/*
Door Alarm
Simple alarm using a magnetic reed switch 
with the arduino and ethernet shield
 */

#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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,0,4,5);
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 doorpin = 9;
int alarmpin = 8;
int doorState = 0;


void setup() {
 pinMode(doorpin, INPUT);
pinMode(alarmpin, OUTPUT);
  // start the serial library:
  Serial.begin(9600);
  Ethernet.begin(mac,ip);
  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop(){
 digitalWrite(alarmpin, HIGH); //The alarm is attached to a relay that will activate when the pin is pulled LOW, so I am turning it off here 
  delay(10000); //wait ten secs to vacate
 doorState = digitalRead(doorpin);
 if (doorState == LOW) { //the reed switch is a normally closed one, so it goes LOW when the door is opened
 Serial.println("Door Opened");
 client.connect(server,80);
 client.println("GET /email.php HTTP/1.0");
 client.println();
 delay(10000);
 digitalWrite(alarmpin, LOW); // Turn relay and attached alarm on
}

How have you got this wired together? Do you get the message Door Opened in the serial monitor? Also, not sure why you are delaying for 10 seconds I would remove that unless you have a reason for it I am missing.

Pauly: How have you got this wired together? Do you get the message Door Opened in the serial monitor? Also, not sure why you are delaying for 10 seconds I would remove that unless you have a reason for it I am missing.

The doorpin is hooked up to a magnetic reed switch, that is normally closed. Meaning that if the reed switch and the magnet are together, there is a connection. The alarmpin is hooked to a relay that switches on the alarm, I have already tested the relay with another sketch; and it is functional. I do not receive the Door Opened message in the serial monitor.

int doorState = 0;
if (doorState == LOW)

You have already declared doorState LOW in the header.
Try making it HIGH or declaring it as HIGH.

I tried your suggestion about declaring the state high, and now it is progressing to the door detection point. It looks like it is now having a issue with the connection to the server. I added the diagnostic code from the WebClient example and it reports to me that “Connection Failed” in the serial monitor. I do not understand, as the connection code was taken directly from another sketch I made for testing the web functionality and the code works in the other sketch. Maybe I messed something up while moving the code around. As it stands this is the complete code from the alarm sketch:

#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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,0,4,5);
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 doorpin = 9;
int alarmpin = 8;
int doorState = 1;
 

void setup() {
 pinMode(doorpin, INPUT);
pinMode(alarmpin, OUTPUT);
  // start the serial library:
  Serial.begin(9600);
  Ethernet.begin(mac,ip);
  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop(){
 digitalWrite(alarmpin, HIGH);
 doorState = digitalRead(doorpin);
 if (doorState == LOW) {
 Serial.println("Door Opened");
   if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /email.php HTTP/1.0");
    client.println();
     digitalWrite(alarmpin, LOW);
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }

 }

}

Well, progress is good. Someone else will have to help you with the ethernet shield as I have not used one yet. Good luck.

Pauly: Well, progress is good. Someone else will have to help you with the ethernet shield as I have not used one yet. Good luck.

Thank you for all your help, it saved me from a lot of head scratching

Well, I was able to get the email notifications working, and now my issue is getting the alarm to sound. What I want it to do is if the door is opened, sound the alarm for 10 minutes; then reset. What happens is that after I upload the code, and then I open the door (make the door pin go LOW); I get the “Door Opened” message in the serial monitor, but nothing else happens. Again, if someone has any ideas or suggestions, I would be happy to hear them.

Here is my current code:

/*
Door Alarm
Simple alarm using a magnetic reed switch 
with the arduino and ethernet shield
 */

#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 doorpin = 9;
int alarmpin = 8;
int doorState = 1;
int alarmState = HIGH; 
long previousMillis = 0;
long interval = 8000;  //changed to 8 sec. for testing

void setup() {
pinMode(doorpin, INPUT);
pinMode(alarmpin, 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(){
 unsigned long currentMillis = millis();
 digitalWrite(alarmpin, HIGH);
  doorState = digitalRead(doorpin);
// delay(10000); //wait ten secs to vacate
// Serial.println("Armed");
 if (doorState == LOW) {
 Serial.println("Door Opened");
 /* commented out the go to web site portion until I get the alarm reliably working.
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.stop();
 
  } 
*/
 
 if(currentMillis - previousMillis > interval) {
 previousMillis = currentMillis;
Serial.println("Sounding Alarm"); 
    // if the Alarm is off turn it on and vice-versa:
    if (alarmState == HIGH)
      alarmState = LOW;
    else
      alarmState = HIGH;
      // set the alarm with the alarmState of the variable:
    digitalWrite(alarmpin, alarmState);
  } 

  }

 }

Decided to go in a new direction, instead of having the alarm on a timer, I am thinking that it would be best it the alarm ran while the door was opened. If I do this however, I need to find a way to limit the amount of emails the arduino will send, since it will send one once per loop. I was thinking of creating a state "email sent" and having it look at it before sending another email. Still figuring out how to implement this though.