Zac Johnson
Published

Illuminate | IoT Project Team 2

Imagine opening your door and the room immediately illuminates to greet you or to inform you of any other visitors.

IntermediateFull instructions provided10 hours562
Illuminate | IoT Project Team 2

Things used in this project

Story

Read more

Schematics

Lights & Relay

Remote

Door Switch

Code

Door Photon Code

C/C++
This code publish door status open or closed. It basically lets the door photon know that the door is opened or closed.
door_sensor = A0;
bool contactBroken = false; // This flag will be used to mark if we have a new status or not. We will use it in the loop.

void setup() {
 pinMode(door_sensor,INPUT);  // Our door sensor pin is input (reading the sensor)

}
void loop() {
/* In this loop function, we're going to check to see if the contact has been broken.
  When the status of the sensor changes, we'll send a Particle.publish() to the cloud
  so that if we want to, we can check from other devices when the door is open or closed. */
  
  if (analogRead(door_sensor) >4000 ) {
  
  delay(1000);
   
    if (contactBroken==true) {
        // If the contact was broken before, then this is a new status.
        // We will send a publish to the cloud .
    
         // Send a publish to your devices...
        Particle.publish("DoorStatus789456","closed");
         // Finally, set the flag to reflect the current status of the sensor.
        contactBroken=false;
    }
    else {
        // Otherwise, this isn't a new status, and we don't have to do anything.
    }
  }
  else {
      // If you are below the threshold, the contact is probably broken.
     delay(1000);
     if (contactBroken==false) {
          
           // Send a publish...
        Particle.publish("DoorStatus789456","open");
        
         // Finally, set the flag to reflect the current status of the sensor.
        contactBroken=true;
      }
     
  }

}

Light Photon Code

C/C++
This code subscribes to when the door sensor photon publishes door status: open and when the remote photon publishes off button. It basically means that when the door is opened the lights turn on and when button on the 3rd photon is pushed down, the lights turn off.
int Light_switch = D0;
int Light_1 = D1;
int Light_2 = D2;
int Light_3 = D3;
int led =D7;


void setup() {
 pinMode(Light_switch,OUTPUT);  
 pinMode(Light_1,OUTPUT);
 pinMode(Light_2,OUTPUT);
 pinMode(Light_3,OUTPUT);
 pinMode(led,OUTPUT);
 
digitalWrite(Light_switch,1);
digitalWrite(Light_1,1);
digitalWrite(Light_2,1);
digitalWrite(Light_3,1);
 
 Particle.subscribe("DoorStatus789456", myHandler);
 Particle.subscribe("offbutton123456", shutdown);
}

void loop (){
delay(1000);

}
void myHandler(const char *event, const char *data)
{
   digitalWrite(led,HIGH);
   
  if (strcmp(data,"open")==0) {
    // if the door is open, turn LED's on
    digitalWrite(Light_switch,0);
    digitalWrite(Light_1,0);
    digitalWrite(Light_2,0);
    digitalWrite(Light_3,0);
    
     Particle.publish("RemoteON123456","ON");
    
  }
  
}
void shutdown(const char *event, const char *data)
{
    digitalWrite(Light_switch,1);
    digitalWrite(Light_1,1);
    digitalWrite(Light_2,1);
    digitalWrite(Light_3,1);
    
}

Remote Photon Code

C/C++
This code controls the button on the third photon. It publishes and event to turn off the lights when the button is pushed.
int offbutton = D3;
int led = D7;
int greenled = D6;

void setup() {
 pinMode(offbutton,INPUT_PULLDOWN);  
 pinMode(led,OUTPUT);
 pinMode(greenled,OUTPUT);

Particle.subscribe("RemoteON123456", ledON);

}
void loop() {
/* In this loop function, we're going to check to see if the contact has been broken.
  When the status of the sensor changes, we'll send a Particle.publish() to the cloud
  so that if we want to, we can check from other devices when the door is open or closed. */
  
if (digitalRead(offbutton)==1)
{
    delay(500);
    Particle.publish("offbutton123456","Lights Out");
    digitalWrite(led,1);
    delay(500);
    digitalWrite(greenled,0);
}

else (digitalRead(offbutton)==0);

 {
     digitalWrite(led,0);
 }
}
   void ledON(const char *event, const char *data)
{
   digitalWrite(greenled,HIGH);
}
 

Credits

Zac Johnson

Zac Johnson

1 project • 0 followers
UNCC ; Mechanical Engineering Student ; Motorsports Concentration
Thanks to Stephen Cardell and Chaitanya Pawar.

Comments