49713 Designing for the Internet of Things
· 25 members
A hands-on introductory course exploring the Internet of Things and connected product experiences.
A bathroom occupancy indicator which sends notifications on a smartphone to let people know if it is occupied or vacant.
One of my housemate's bedroom is at the first level of the house we live in. Unfortunately for him the bathroom is located at the second level. Every morning when he wakes up he has to walk up the stairs to check if the bathroom is vacant. If occupied, he has no choice but to wait outside or make his way back down. An IoT device that can tell him the occupancy status of the bathroom in the comfort of his bedroom which will help him time his trek to the bathroom every morning accordingly.
I will use a Hall effect sensor to detect if the bathroom door is open or closed. A push notification will be sent to his phone using IFTTT telling him that the bathroom is occupied when the door closes and vacant when the door opens again. He can now check the status of the bathroom in his bed without having to go up all the way to the second level. In edition an LED will indicate the occupancy status outside the bathroom for anyone else who passes by.
Initially I set up the circuit and code to test the Hall Effect Sensor and make sure the output LED displayed the correct color. After successfully accomplishing that task I modified the code for publishing and integration with IFTTT. During this process I realized that just publishing the event in the if-else loop was telling the photon to continuously publish 'Vacant' and 'Occupied' when the LED was green and red respectively. But I wanted the photon to publish the event just once in each case. I had to create two more variables to store and change the state of the hall effect sensor in order to publish the event just once.
After publishing to the particle cloud I integrated the hack with IFTTT to send push notifications on my phone. This was relatively straightforward. I used the following tutorial : https://community.particle.io/t/beginner-tutorial-ifttt-monitor-a-device-status-trigger/9406?redirected=true
After getting the notifications to work the next step was to test it to see if it will actually work in the context it was made for. I hooked up the circuit board to the door frame and attached a magnet to the door and tested it. It actually worked!
//TEST CODE TO CHECK IF HALL EFFECT SENSOR IS WORKING BEFORE PUBLISHING TO CLOUD AND CONNECTING TO IFTTT
int hallPin = A0;
int redPin = D0;
int greenPin = D1;
void setup(){
pinMode( redPin, OUTPUT);
pinMode( greenPin, OUTPUT);
pinMode (hallPin, INPUT);
}
void loop(){
int hallState = digitalRead( hallPin ); // to store and read state of hall effect sensor
if( hallState == LOW ) // LOW = vacant, HIGH = occupied
{
digitalWrite( redPin, LOW);
digitalWrite(greenPin, HIGH); // green when vacant
}
else{
digitalWrite( greenPin, LOW);
digitalWrite(redPin, HIGH); // red when occupied
}
}
Click to Expand
//FINAL CODE WITH IFFTTT INTEGRATION
int hallPin = A0;
int redPin = D0;
int greenPin = D1;
int pastState = 1;
int currentState = 1; // 1 = Green, 0 = Red
void setup(){
pinMode( redPin, OUTPUT);
pinMode( greenPin, OUTPUT);
pinMode (hallPin, INPUT);
}
void loop(){
int hallState = digitalRead( hallPin ); // to store and read state of hall effect sensor
if( hallState == LOW ) // LOW = vacant, HIGH = occupied
{
digitalWrite( redPin, LOW);
digitalWrite(greenPin, HIGH); // green when vacant
currentState = 1;
if( currentState != pastState){
Particle.publish("occupied"); // publish event to particle cloud
pastState = currentState;
}
}else{
digitalWrite( greenPin, LOW);
digitalWrite(redPin, HIGH); // red when occupied
currentState = 0;
if( currentState != pastState){
Particle.publish("vacant"); //publish event to particle cloud
pastState = currentState;
}
}
}
Click to Expand
With no prior coding experience, I found writing code very challenging. But once I got a hang of the logic and the syntax of certain functions it became relatively easier. The most fun and interesting part for me was coming up with the logic and flow of how this homehack would work. My original idea was to pair up the hall effect sensor with a motion detector to detect the presence of someone in the bathroom. At present, this hack works on the assumption that if there is no one in the bathroom the door must be left open. Integrating the motion detector will enable the hack to tell if the bathroom is vacant even if the door is closed. But a conversation with Daragh about my limited understanding of coding kept my ambitions in check. But I will definitely try to integrate the above mentioned functionality in the future.
A hands-on introductory course exploring the Internet of Things and connected product experiences.
A bathroom occupancy indicator which sends notifications on a smartphone to let people know if it is occupied or vacant.
January 24th, 2018