Introduction: Using Smart Phone As Motion Sensor Alarm

About: I am a digital nomad and I often find myself navigating myself the fields of IoT, IIoT, VR, Voice Activation, Web and Mobile Apps and you know.. you got the idea :)

Today, we live in a world where smart phones are becoming increasingly cheaper, basic Android phone just cost around $60 in the part of the world where I live in. Irrespective of the make and model of the OS, nearly every smart phone comes with multiple hardware and software sensors which can be used in a variety of scenarios. The scenario I am going to discuss today is the one where we use smart mobile phone as our personal motion sensor alarm.

There can be multiple use-cases, such as one where I leave the phone under the seat of my Harley Davidson motorbike, next if someone moves the bike, my phone detects the motion and sends me an alert SMS.

Another use-case is, let's say I don't want someone to interfere with my personal bag but I really have to go out as well, in that case I will leave my motion-sensing phone inside the bag, later-on, if motion is detected, the phone can call my other phone, send a SMS or a notification, switch a light-on somewhere or can turn a sound alarm on.

I am sure you can think of a better use-case than me. So, I will just concentrate on building the circuit and corresponding mobile app which will enable my spare Android phone to be used as a motion detector and for demonstration purposes, I will just be turning a study lamp on when movement is detected.

Let's move towards instructions.

We will need:

  • An Arduino Uno
  • Evothings Studio
  • A 5V Relay Module
  • A Study Lamp (for alarm demonstration)

In the next step, we will build our circuit!

Step 1: Connecting 5V Relay Module With Arduino

I am using 5V Relay Module by RM1LS, it's an inexpensive module, easily available from eBay. The model number of relay is SRD-05VDC-SL-C. There are many articles already available with clear instructions on how to use such relay modules, I have used this instructable. As my module is slightly different when it comes to marking of the pins, I will list the connections here:

  1. Connect D12 of Arduino to CL1
  2. Connect 5V of Arduino to DC+
  3. Connect GND of Arduino to DC-

As for the interfacing with the main supply, please see Step 5 of the above mentioned instructable (use it with caution, if you don't understand the electrical equipment and are easily confused with electrical phase systems and the names of the wires (hot, neutral and earth) then please do consult with an experienced electrician before trying to make connections by yourself!)

Once done, upload the Blink example to your Arduino, change the pin number from 13 to 12 and check if your study lamp is working, if it starts blinking, the connections were successful.

In next step, we will add our WiFi shield to the circuit.

Step 2: Connecting WiFi Shield With Arduino

Now, we need to add a communication method. A while earlier, I wrote a tutorial in which I configured DFRobot's WiFi Shield with Arduino, please follow the listed steps in that tutorial to get your WiFi shield up and running (if you are using the same shield).

Important: After stacking the WiFi shield on top of Arduino, re-wire the relay module connections by using the clearly marked female pin headers on top of the shield itself.

In next steps, we will upload our sketch to Arduino and will start developing our mobile app which will sense the motion and trigger the study lamp on.

Step 3: Uploading the Arduino Code

Upload the following sketch to the Arduino (make sure the little white switch on WiFi shield is on "PROG" side while you upload the sketch, once you upload, reset the switch to "RUN").

/*
Arduino Light Control using Serial Commands Created May 27, 2015 Hammad Tariq, Incubator (Pakistan)

It's a simple sketch which waits for a character on serial and in case of a desirable character, it turns an LED on/off.

Possible string values: a (to turn the LED of) b (tor turn the LED on) */

char junk; String inputString="";

void setup() // run once, when the sketch starts { Serial.begin(115200); pinMode(12, OUTPUT); digitalWrite(12, HIGH); }

void loop() { if(Serial.available()){ while(Serial.available()) { char inChar = (char)Serial.read(); //read the input from serial inputString += inChar; } Serial.println(inputString); while (Serial.available() > 0) { junk = Serial.read() ; } // clear the buffer if(inputString == "a"){ // in case of 'a', turn the LED off digitalWrite(12, HIGH); }else if(inputString == "b"){ // in case of 'b', turn the LED on digitalWrite(12, LOW); } inputString = ""; } }

Step 4: Developing Motion Sensor Alarm App

We now have to develop our motion sensor app and we are going to develop this app in under 10 minutes using Evothings Studio.

Evothings Studio let's you rapidly develop your IoT app using JavaScript and HTML. It comes bundled with all required plugins, such as in our case we need to access accelerometer of the phone (for motion detection) and also need to send commands to the Arduino via WiFi shield (for turning the study lamp on). Moreover, when you are using Evothings Studio, you don't need to go through lengthy SDK installations or CLI commands. Instead, you just need to do the following:

  1. Download and install Evothings Workbench on your computer
  2. Download and install Evothings Client app (available for both iOS and Android)
  3. Connect to the Workbench from Evothings Client app using your WiFi IP
  4. Run the "Hello World" example from Evothings Workbench

That's it! We have tested our setup and now we can focus on developing our app.

For your ease, I have uploaded the code of the app to the github, follow these steps:

  1. Download or clone the example app code and save it to your favourite location on your computer
  2. Find index.html in the downloaded folder, drag & drop it to the Evothings Workbench
  3. Run the "Motion Sensing Alarm" example

Now, you should be able to see the example app on your phone. Connect the app to your WiFi Shield using "Start" button, your phone is now a motion sensing device. Try moving the phone in any direction, it will detect the movement and at the same time it will turn the study lamp on!

It was that easy! In next step, I will explain the part of the example app code which is detecting the motion and sending a trigger command to the Arduino.

Step 5: Code Explanation & Conclusion

First of all, if you want to understand the WiFi communication and how the app the app connects to the Arduino using WiFi Shield, it will be beneficial to follow my previous tutorial: Remote Controlled LED using Mobile Phone and Internet. I am using the same example app code but with slight modifications:

Motion Detection

We are using Evothings Studio to rapidly prototype our app, later-on, when we are happy with the app, we can bundle the app in Cordova and ship it to the app stores. How that works? Simple! The Evothings Client app is essentially made in Cordova (formerly PhoneGap), so, whatever is available for Cordova, it's available for Evothings Client app as well.

Cordova is a HTML5 wrapper for mobile phones and through specialized plugins, it provides you the option to access native phone features, such as access to hardware accelerometer, camera, battery status etc.

By using the accelerometer example given in Cordova documentation, I wrote my motion detection function in JavaScript.

In index.html, you will find:

<p>function onSuccess(acceleration) {<br>        var element = document.getElementById('message')
        var pxmotion = 0
        var pymotion = 0
        
                //round the accelerometer readings                    
		var xmotion = Math.round(acceleration.x,4)
		var ymotion = Math.round(acceleration.y,4)
		
		//compare current readings with previous ones
		if(xmotion != pxmotion || ymotion != pymotion){
			element.innerHTML = 'Motion detected'
			app.ledOn()
			pxmotion = xmotion
			pymotion = ymotion
		}
    }</p>

The function take the accelerometer readings, round them off and compares them with previously stored values, if accelerometer values are different, that means the phone is moving in either horizontal or vertical direction.

Turning the light on

<p>// Turn on light.<br>app.lightOn = function()
{
	app.writePin(LOW)
}</p>

It then calls app.lightOn function, which sends a "LOW" trigger to the Arduino, which in turn sends the LOW to D12 output, where our relay module turns the light on.

Conclusion

Today, we have learned how to turn a study lamp on if your mobile phone detects the motion. Feel free to tinker around, maybe interface a car horn instead of the study lamp or turn the lights on/off while you shake your mobile phone. The possibilities are limitless!

Happy tinkering!