Motion Sensor SMS Alert App for nerds and bikers

Hammad TariqBlogs, Tutorials

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

bikes_smallA smartphone is packed with sensors, and can continue to serve well for several years after it has stopped being as your personal phone. In this use-case you place a second device, other than your primary smartphone, on your bike under the (locked) saddle compartment. Then if out of bravery or foolishness — despite the on-tank airbrushed skull and crossbones — someone would dare to move or even budge the bike, the phone detects this motion and instantly sends you an alert via SMS. There are tons of other use cases, but as several of us have bikes this one could be very practical!

A while back, I wrote a general tutorial on instructables.com in which I outlined instructions on how to use a smartphone as Motion Sensor Alarm, again putting your outdated or second Android phone to use, letting the occured motion event turn a table lamp on/off when the motion was detected. So let’s take this theme a bit further, and as for the use-case I just mentioned above to invoke an SMS to a preset number typically your other phone. That will allow you to receive alerts even when you are not in close proximity of your belongings, or even to a list of multiple receivers. Your whole gang can all come and teach that motorcycle handling menace a lesson!

Please note: Even as Evothings Studio is a multi-OS appmaking system, this tutorial can only be used when developing for Android, simply because on iOS (and Windows for mobile devices, an SMS can only be invoked using the native SMS dialog, which in turn means we can’t send automated SMS alerts on those operating systems without manually pressing “ok” or “send” via an on-screen dialog.

Still sounds interesting? Great, let’s start!

Step 1: Get Evothings Studio

We will be using Evothings Studio to rapidly develop an app in HTML5 and JavaScript. To get started (about 5 minutes):

  1. Download Evothings Workbench
  2. Download Evothings Client for Android
  3. Connect the Evothings Client app with Evothings Workbench using the app’s Workbench scanning function or by punching in your computer’s IP address.

More about Evothings Studio

  • Evothings Studio an open source tool which lets you develop mobile apps within minutes using HTML5 and JavaScript.
  • You can start building your app right after downloading – no lengthy SDK installations.
  • It saves you from the complexity of learning native app development languages and SDKs.
  • Evothings Client is openly released under the Apache 2 licence.
  • An Evothings Client is based on the Apache Cordova app framework, meaning that functionality available for Cordova (and hence for its commercial incarnation Phonegap), it will also run under Evothings Studio and on the Evothings Client.
  • Evothings Client comes already bundled with selected Cordova plugins, allowing you to start prototyping your IoT apps straight away.
  • When you need extra Cordova plugins – like in this case where we want to send an SMS – just clone Evothings Client’s Git repository and build a custom Evothings Client that looks and behaves exactly like you want, which is covered in this recent tutorial.

Step 2: Build a custom Evothings Client with Cordova SMS Plugin

As mentioned in the previous step, Evothings Client comes with selected plugins which you may need to develop an IoT app, however, the official Evothings Client does not come bundled with a plugin which facilitates sending an SMS directly from the app. In this case, as the Evothings Client is open-source, we just need to clone a copy from its Git repository and build our own custom Evothings Client that includes the ​Cordova SMS Plugin.

Sounds complex? Don’t worry, there is a very detailed tutorial available explaining how to add the Cordova SMS plugin (or any other plugin) to Evothings Client. Follow the given tutorial step-by-step, you will have your custom Evothings Client app with SMS sending capability in no time!

Let’s say that you’ve created your custom Evothings Client, so we can take the final step and develop our app!

Step 3: Developing Motion Sensing SMS Alert App

We will need to have two functionality blocks, as we need to:

  • Detect motion using the mobile device’s accelerometer.
  • Send an alert SMS.

For ease of understanding, download this example code from my Git repository and unzip it.
After downloading:

  1. Launch Evothings Workbench, if you haven’t already
  2. Launch your recently built customized Evothings Client on your phone.
  3. Connect Evothings Client with Evothings Workbench.
  4. Use your mouse to drag-drop the index.html from the downloaded example’s directory straight into Evothings Workbench. This creates a new top row in the Workbench with your project
  5. Click Run!

You should be able to see the example running in the Evothings Client app. Press the “Wait for movement” button, that will change the status text to “Waiting for motion…”. Now move your phone slightly, the status text will change to “Motion Detected” and after a second you will see the alert box notifying that a text message has been sent. At the moment the message will be sent to nobody, as we have not yet filled in our phone number in the code, so please read further. You can also continue to tweak and further develop you app anyway you like!

sms_motion_dumps_small

Step 4: Code Explanation

For detecting the motion, we are using Cordova’s own Accelerometer API, which is already built in. Open index.html in your favourite code editor and analyze the following code block:

// Start watching the mobile device's movement (acceleration).
	app.waitForMotion = function()
	{
		/* Start watching the device's acceleration to determine if it has
		 * moved, unless we're already watching the device's movement. */
		if (!app.watchId)
			app.watchID = navigator.accelerometer.watchAcceleration(
				app.onAccelerationReceived,
				app.onAccelerationError,
				{ frequency: app.movement_check_interval }
			)

		app.setStatusText('Waiting for motion...')
	}

First, we ask the Accelerometer object to start watching the accelerometer for any changes in the current state. Once a change is detected, the app.onAccelerationReceived function is called; this function compares the current readings with previous ones and if there is a positive difference between the current and previous reading of accelerometer along x-axis or y-axis, the app.sendSMS function is invoked in order to send an SMS through the Cordova SMS plugin.

/* onAccelerationReceived:
	 * Called when an accelerometer value has been received from the mobile
	 * device. Determines whether the device has moved since the last check. */
	app.onAccelerationReceived = function(acceleration)
	{
		// Round the accelerometer readings to avoid detecting small vibrations.
		var xMotion = Math.round(acceleration.x, 4)
		var yMotion = Math.round(acceleration.y, 4)

		/* Compare the current readings with previous ones. If they differ 
		 * we conclude that the device has moved. If no previous readings were
		 * collected, compare with 0. */
		if (xMotion != (app.previous_xMotion || 0) ||
			yMotion != (app.previous_yMotion || 0))
		{
			app.setStatusText('Motion detected')

			// Send alert SMS.
			app.sendSMS()

			/* Stop checking for device movement to avoid sending multiple SMS.
			 * TODO: re-enable the movement checking after e.g. 1 minute. */
			app.stopWaitForMotion()

			// Store the readings for use in the comparison the next time.
			app.previous_xMotion = xMotion
			app.previous_yMotion = yMotion
		}
	}

You will need to change the number property of “app” object with your own phone number and also change the message variable with your own custom alert text, for example, “Bike is moving .. Alert! Alert! Alert!” (You can probably be more creative here :-).

// Application object.
	var app = {
		/* Phone number through which the message will be sent upon movement.
		 * Replace with your own phone number. */
		PHONE_NUMBER : '0xxxxxxxxxx',
		// Message sent to the recipient's phone upon device movement.
		SMS_MESSAGE : 'Alert! Motion has been detected',
		/* Interval at which to check for device movement, in milliseconds.
		 * Lower values means we are able to detect smaller movements, and
		 * higher values means less battery consumption.
		 */
		movement_check_interval : 3000
	}

That’s it! You have successfully developed your Motion Sensing SMS Alert app, they’ll never be messing with your fine wheels again without you knowing!