Curious of my family's bathroom habits, I wanted to create a device that would be able to detect when the door to the bathroom is opened. This device can be utilized on any door with a turning knob.
This project was part of Lane Tech HS's Physical Computing Lab course. I created this project as part of the Sensors and Me project.
CircuitryThe circuitry is relatively simple, as it was just hooking up ground, 3.3V power, and digital signal to the Argon:
For setup, I created variables for the digital pin reading the tilt switch's value, output to the serial monitor, and for the digital read itself. I also started up the serial monitor and set pin D2 to INPUT.
int tiltSwitch = D2; // digital pin for reading tilt switch value
int returnValue = 0; // variable for preventing repeat publishes and serial monitoring
int val; // variable for digital read
void setup() {
pinMode(tiltSwitch, INPUT);
// start serial monitor for testing and presentations
Serial.begin(9600);
}
For the loop, I read the value of the tilt switch every 100ms and used a conditional statement to print a return value in the serial monitor:
void loop() {
// read pin D2
val = digitalRead(tiltSwitch);
// if switch read as LOW
if (val == LOW)
{
// if new occurence of door being detected as open
if (returnValue != 0)
{
// publishes event for webhook to Google Sheets
Particle.publish("Door Opened", "door opened");
}
returnValue = 0;
}
else
{
returnValue = 1;
}
// output read on serial monitor for testing and presentations
Serial.println(returnValue);
// wait 100ms before checking again
delay(100);
}
The nested If statement that checks that the most recent value is not 0 is to prevent the event from publishing more than once if the tilt switch reads LOW continuously.
WebHook Setup in Google SheetsThe following setup for the WebHook is adapted from Gustavo Gonnet's Pushing Data to Google Docs further simplified by my teacher for Google Sheets.
In order to get a timestamp on when the tilt ball sensor detected the door was open, I created a spreadsheet that would host the data pushed by the Argon.
Opening the script editor, I pasted the script code from Daniel Law into a script attached to the spreadsheet (which can be found below). I then created a trigger for the script with the following settings:
I then deployed the script as a web app and matched the dialogue window to the following:
Once you receive the web app URL, make sure to copy it or save it somewhere. Run the setup function once and your spreadsheet is set up.
WebHook Setup on the ArgonAdd code to publish an event when your tilt ball sensor is activated.
Particle.publish("Door Opened", "door opened");
After flashing your Argon, open the console and create the webhook. Set the request type to "POST" and request format to "Web Form." Your event name is the first parameter of your Particle.publish() call. The URL is the web app URL that you copied from your spreadsheet earlier. Your device should be set to your personal Argon.In advanced settings, select the following:
Your webhook will be set up and ready to be tested.
ResultData should now appear in your spreadsheet with timestamps which you can then utilize to create charts on when the door is opened.
(Note: the data utilized below was gathered from testing)
Once data is collected, you will be able to make the data into a chart, telling you when the respective door is opened.
See a full demonstration here.
Comments
Please log in or sign up to comment.