IT441: StallSense
Published: 2024-06-13 16:17:30

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtbmwitbrsdf4k0yz0m8.jpg)

# Overview
Have you ever got up from your desk and walked over to the restroom, only to find that all of the stalls are full? Then you walked to the other restrooms in the building, only to find that all of them are full too? Enter StallSense, the bathroom occupancy detector to help you know to go.

# Materials
The physical materials used in this project are as follows.

- Personal Computer
- Personal Cloud Server
- (1/sensor) Arduino WEMOS D1 mini
- (1/sensor) Breadboard
- (1/sensor) LED Stoplight
- (1/sensor) Adafruit Grideye Thermal Sensor
- (1/sensor) Magnetic Door Switch Sensor

# Resources
The software, services, and code libraries used in this project are as follows.

- Arduino IDE
- C Libraries
- ESP8266WiFi
- WiFiManager
- PubSubClient
- ArduinoJson
- SPIFFS
- AdafruitAMG
- Mosquitto MQTT Broker
- Node.js
- Node.js Libraries
- Express
- Morgan
- MQTT
- Sequelize
- Socket.io

# References
The following guides and references were very useful in the completion of this project.
- SteveFi. Simple reference to the GPIO pin layout on the WEMOS D1 mini.
- GitHub. The published Adafruit AMG library, used for collecting data from the thermal camera.
- Adafruit Learning. Demonstration of collecting and using data from the Adafruit thermal camera.
- Random Nerd Tutorials. Nice walk-through using custom parameters with the WifiManager library.
- Circuits4You. Explaination of how to reset an ESP8266 chip programatically.
- Mahesh Bhanushali. Refresher course on creating a Node.js website.
- Ramesh Prajapati. Help connecting Node.js to MySQL database.
- Rising Stack. Easy to understand explaination of using the MQTT library in Node.js.
- Rip Tutorial. Tutorial for running websockets in Node.js.

# Procedure
## Part 1: Create Management Server
While the individual sensors themselves are an important part of this project, the status server is what will really make the sensors most useful. I personally did not start by creating the server, and I wish I would have. It is the crux of the whole project.

- Find a computer that you can use to run your Node.js server. You’ll need a system that can run Node.js, which is actually quite a lightweight service, and that is in a network location where the devices themselves can access it. Outside of that, feel free to use any setup you have at your disposal.
- Install Node.js. Install Node.js and Node Package Manager.

- Initialize your StallSense server. I am not a constant user of Node.js, so I always forget how to do this. Mahesh Bhanushali has one of the best walkthroughs that I’ve found on getting up and running with your own Node.js webserver. He specifically uses Express Generator, EJS, and Bootstrap in his tutorial. That is what I did as well, but feel free to use your own setup if your are more comfortable with that.
- Create and implement the visual design of your website. I went with a simple table layout for my StallSense web interface, but you could do something fancier if you want. If you want to do something more complex, I’d suggest sketching up the layout of your website before trying to implement it in HTML/CSS code. Then, edit the index.ejs view file to reflect what you want your design to be. If you followed step 3 above, you'll have access to Bootstrap styling which makes implementing your design much easier. (Below is a screenshot of my website, for your reference.)

## Part 2: Build a Thermal Sensor
Wire/soder your thermal sensor to your Arduino. For this project, I used the Adafruit Grideye 8x8 thermal camera. It initially came without header pins sodered on, but since I was planning on using it for other IoT projects outside of this, I decided to add them for ease of use. Once the headers were attached, I put both the thermal camera and my Arduino on a breadboard, and then I ran wires between them as if they were placed on top of each other. I also added a stoplight to my contraption so that I could have visual feedback of the thermal state.

## Part 3: Build a Door Sensor
Wire your door sensor to your Arduino. Door sensors are a lot more common to come by, so I actually don’t know what kind of door sensor I used. You should be able to find a generic one on your own though. The door sensor needs to be connected on one end to a power pin and on the other to an input pin. I also added a stoplight to this Arduino for immediate feedback.

# Appendix
## FAQ
- **I really don’t want to use Node.js. What are the alternatives?** I definately agree. Node.js can be a headache sometimes, espically if you don’t use it a lot. I chose Node.js because it I liked the fact that it has tons of feature extensions already developed and because it is web-centered. However, any multi-function language that has web, MQTT, and MySQL support should be able to do the job. Python and C# are the first alternative languages that come to my mind, amid others.

## Thought Questions
- **How well does the final product actually perform? It is worth it?** In the end, the stall sensors were a success! Definately worth it. The low-down is this: (1) The StallSense server added a lot to the functionality. Honestly, if all I had was one sensor, it still would have been worth it to me to write all that server code. (2) The thermal sensor needs more resolution. It worked just fine in my test bathroom, which was only a foot tall, until my roommate suck his hand underneath it and it didn’t go off. I don’t think that it would work in a normal-sized bathroom, espically with all the thick, heat-capturing clothing that people wear in the winter. (3) The door sensors are the way to go. If you want the most reliable and least expensive option, go with the door sensors. They are accurate, much easier to develop, and are less of a privacy concern.
- **What improvements would you like to make to StallSense?** Two main ideas here. One, I want to build commercial-type case for my sensors, just to see what that would be like. Would it be possible to make this contraption discreet enough that it wouldn’t look out-of-place in an actual bathroom? Second, it would be really cool to tear apart a toilet motion sensor (you know, the kind that automatically flush the toliet) and see if I could leverage it to also report to the stall occupancy status. If this idea were ever sold commercially, I think that having it embedded in a motion sensor would be the way to go.
- **How much time did it take to complete this project?** Unlike many of my previous projects, this project was almost completely from scratch. Of course, I did not have to write the WifiManger, AdafruitAMG, or Node Express libraries myself, but I did do the integrations between them myself. It took me about 8 hours to setup and program the thermal and door sensors. Then, it took an additional 8 hours to build the StallSense server. In total, about 16 hours of work time and an additional 4 hours to write this tutorial.

# Code
All code for this project can be found in [my repo on GitHub](https://github.com/charlesrc019).