September 2015 | Dios Kurniawan
Last Lebaran holiday in July was a good time for me to jump back into my programming hobby. Quiet days, no interruptions and no deadlines. It was really a good time to delve into coding. Well, programming is actually much more fun if it is only a hobby when you can choose what real-life problems you want to solve with your software, pick the language you like, and choose how to do it. This time, I got about a week worth of leisure time, and I decided to create a solution to one domestic problem: a refrigerator door sensor.
A remote sensor for refrigerator
Let’s be more clear with the requirement. The problem definition is: I want to know how many Magnum Mini ice cream packs are left in my fridge. Yes, ice cream. My daughter loves to eat Magnum Mini while the same is true for me. It creates some sort of competition situation, either she or myself would eat up the ice cream. Nothing more upsetting to me than seeing no more Magnum Mini is left in the fridge each time I got home from work. I wish I could have an app in my hands telling me if my ice cream supply falls short, so I could stop by at local groceries store on my way home to restock the ice cream. I was thinking about some kind of a camera that monitors the inside the freezer, or a counter device to see how many times the fridge door is opened each day, but I could not find such thing in local DIY shops. Enter Android smartphones.
My stockpile of Magnum Minis in the fridge
Nearly all Android phones today are equipped with built-in accelerometers. This sensor is good for measuring the acceleration force that is applied to a device on all three physical axis (x, y, and z), including the force of gravity. Just like an aircraft, an Android device orientation is also defined by pitch, roll and azimuth. So why don’t we use this sensor for detecting door movements? Simply attach a cheap Android phone on the door and let it run to detect motion. All we need is an app that reads the accelerometer and sends the data over the internet thru Wi-Fi or 3G connection, basically turning an Android device into a remote sensor, or as some people prefer to call it : an M2M (machine-to-machine) sensor.
Device orientation variables for detecting open-close state
For this purpose, I wrote a simple Android app (I named “dSensor“) that reads the accelerometer sensor, detects movement and sends the event over the Wi-Fi to a remote server, which in turn will store the data. I do not intend to create anything overly complex because I understand a decent movement detection algorithm would need an in-depth study on digital signal processing theories. I would try to skip that and make the detection function as simple as it can. Simply detect changes in orientation and trigger the event to send data. Luckily, Android provides pretty complete library for accessing sensors under android.hardware.Sensor package.
import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager;
Quite straight forward, detecting door’s open/closed position is achieved by implementing code in the onSensorChanged() event. The movement detection is implemented using accelerometer which in turn would trigger detection using magnetic field sensor, to determine if the door is opened or closed.
@Override public void onSensorChanged(SensorEvent event) { if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) { //detect any slight movement to trigger door check } if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD) { //get azimuth, pitch & roll to check if door is open or closed } }
After a couple days of coding, my first prototype app was ready to test. For this purpose I used my 4-year-old Samsung Galaxy Y which had been lying around unused at my house for quite a long time. This old smartphone is glued on the fridge’s door with its battery charger permanently attached, and continuously running the app in the foreground.
Android app to detect motion and send the data over Wi-Fi
The Android phone is attached to the fridge, acting as a sensor
Data from the device is transferred in JSON format via HTTP and is then stored in a database. Initially I chose MySQL, but later I moved to MongoDB (a NoSQL database) to take advantage of its scalability. I also created a simple web application to display the reports.
+---------------------+------------+----------+------------+ | time stamp | azimuth | pitch | roll | +---------------------+------------+----------+------------+ | 2015-08-23 20:55:28 | 36.71875 | -25 | 11.5234375 | | 2015-08-23 20:50:27 | 36.71875 | -25.7812 | 12.3046875 | | 2015-08-23 20:45:26 | 35.9375 | -25 | 13.0859375 | | 2015-08-23 20:40:25 | 39.84375 | -25 | 9.9609375 | | 2015-08-23 20:35:24 | 39.84375 | -25 | 9.9609375 | | 2015-08-23 20:30:23 | 39.84375 | -24.2187 | 9.9609375 | | 2015-08-23 20:25:32 | 40.625 | -25 | 9.9609375 | | 2015-08-23 20:20:21 | 39.84375 | -25 | 9.9609375 | | 2015-08-23 20:15:20 | 39.84375 | -25 | 9.9609375 | | 2015-08-23 20:10:18 | 35.9375 | -26.5625 | 8.3984375 | | 2015-08-23 20:05:18 | 37.5 | -25 | 11.5234375 | | 2015-08-23 20:00:16 | 37.5 | -25.7815 | 12.3046875 | +---------------------+------------+----------+------------+
Now the analysis part. The raw data sent from the sensor tells me how many times the door is opened, but it cannot tell me how many ice cream packs are still in the fridge. The approach is to make an assumption that each time the door is opened, one Magnum Mini is taken out. I understand that my kids get home from school at around 3 PM, therefore any door opening event during the time until they get to sleep at around 10 PM can be assumed that my kids are extracting Magnum Minis from the fridge. Using simple if-then statements, each time the door is opened during this period, the number of ice cream counter is subtracted by one. Of course in reality it is not that simple as there may be other things inside the fridge, but at least that’s the closest approximation.
The application would require me to adjust the counter each time I restock my ice cream. Subsequently, the application will count how many ice cream has been consumed and will display the (estimated) remaining number in my fridge. Now, I can remotely view this information at my fingertips anywhere I have access to my web application. Pretty neat.
Web application shows how many times door has been opened during the day, and estimate how many remaining ice cream are still there
Not limited to refrigerators and ice cream, my sensor-on-a-phone solution can also be used on doors to detect theft, to count how many people entering a room, and many other uses. I am now working on modifying the code to detect motion and even vibration in my car. This demonstrates that a pretty versatile sensor can be had with a old, cheap hardware (under $30) which would otherwise become electronic waste.
Theft Detector
My simple project here highlights the vast opportunity to use Android devices for sensors, especially in light of the emergence of IoT (Internet of Things) trends. This is just a simple experiment, but the potential use case is limitless. There is nothing as exciting as starting a new project and seeing it evolve.