How to make an app for remote controlling an elevator (part 2)

Eric SvenssonBlogs, Tutorials

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+
App for calling and unlocking the doors of an elevator.

App for calling and unlocking the doors of an elevator.

Previously we told the story of how we hacked an elevator and talked mostly about the hardware involved. Now we will tell you about the easy part: how to make a mobile app in HTML5 and JavaScript that allowed us to both call and unlock the doors of our office elevator. Using Evothings Studio and the Electric Imp IDE we could quickly develop a working IoT app!

Three simple parts are needed to make the app work: the code for the mobile app itself, the code for the Electric Imp Agent and the code for the Electric Imp Device. We will start by explaining these Agents and Devices and thereafter show the code!

How to communicate with the Electric Imp using Agents and Devices

The Electric Imp is very easy to communicate with. After setting it up like described in Part 1 of this article, it already has an Internet URL accessible from anywhere. This URL looks something like “https://agent.electricimp.com/gpTxbKL5BAq1” and can be found in the Electric Imp IDE. When you navigate to that URL from a web browser, you are communicating with a service called an Electric Imp Agent that is basically a web server (cloud service) that you are in control of. The Agent service is programmed through the Electric Imp IDE.

We cannot over the Internet communicate directly with the Electric Imp, only through the Electric Imp Agent service. But we can easily forward any information sent to the Agent to the Electric Imp itself, and vice versa. This approach is for many purposes very effective as it for example leads to fewer messages being processed and communicated by the Electric Imp itself, which leads to decreased energy consumption.

Communication-pathway

How our app communicates with the Electric Imp.

Because we have different code running in the cloud, i.e. the Electric Imp Agent, and on the device itself, the term Electric Imp Device is used for the latter to distinguish between these. Multiple Devices can communicate with the same Agent.

Electric Imp Agent and Devices

Example of how a mobile app could communicate with multiple Devices through an Agent.

The Electric Imp is intended to provide Internet connectivity to battery-powered devices like for example scales or motion detectors and for these purposes it doesn’t need a constant internet connection. For more advanced devices, decision-making based on information attained through the Internet can be done in the Agent service, and only if deemed necessary it will be pushed to all connected Devices. The Electric Imp API makes it very simple to write code that sends messages back and forth between the Agent and its connected Devices.

Sending commands to the Electric Imp Agent from the mobile app

Electric_Imp_elevator_remote_app_screenshotOur mobile app needs to simply be composed of two buttons, one for unlocking the elevator’s doors and another for calling the elevator. When pressing one of these buttons, we want a message to be sent to the Electric Imp that turns on one of it’s I/O ports. Because we connected the I/O ports to the elevator’s control panel, the app can then be used to remotely press the control panel’s buttons.

We based the app on the Hello World example bundled with Evothings Studio, by simply making a copy of it, dragging its modifying the text and inserting two buttons and some JavaScript. Testing the app on any device is easy with Evothings Studio as we basically only have to drag the index.html file into the Evothings Workbench project list and press the Run button.

Each button should simply send an HTTP request to the Electric Imp Agent URL when pressed. The URL contains an additional parameter telling the Agent whether we want to unlock the door or call the elevator.  With the help of jQuery, we can make this request with one line of JavaScript, like the following:

$.get('https://agent.electricimp.com/gpTxbKL5BAq1?command=unlock')

Each button is simply composed of an HTML-element with an additional attribute specifying what should happen when we press it, like the following:

<button onclick="unlock_door()">
	Unlock the door
</button>

<button onclick="call_elevator()">
	Call the elevator
</button>

The “unlock_door” and “call_elevator” functions should make the respective HTTP request to the Electric Imp Agent like described above. In the final version of the app’s code we made the JavaScript code even more compact, but for the sake of the article it’s here slightly more verbose.

Receiving messages on the Electric Imp Agent

In the Electric Imp IDE there are two code panes, one for the code running on the Agent and one for the Device. The code we need to write in each of these is very basic, and will be explained in this and the following section. The language used in coding for the Electric Imp is called Squirrel and uses a C-like syntax. The Electric Imp API contains functions for handling HTTP requests, controlling I/O ports, power management, and much more.

Electric Imp IDE and the mobile app controlled elevator.

Electric Imp IDE

Firstly we enable listening for HTTP requests by calling the http.onrequest function in the Agent and giving it a reference to a function that will receive our requests. In this receiving function we check for the “unlock” or “call” commands specified through the Agent URL query string, like the following:

function on_http_request(request, response)
{
    if (request.query.command == "unlock")
    {
        device.send("command", "unlock");
    }
    else if (request.query.command == "call")
    {
        device.send("command", "call");
    }
}

http.onrequest(on_http_request);

Now when we make a request through the URL “https://agent.electricimp.com/gpTxbKL5BAq1?command=unlock”, the Agent will send an “unlock” command to the Electric Imp.

Receiving messages on the Electric Imp Device

In the Electric Imp Device code we simply need to call the agent.on function, specifying that when a message with the title “command” arrives from the Agent, its contents should be sent to a function that we created. This receiving function on the Electric Imp should turn on and off its corresponding I/O port, to activate one of the relays connected to the elevator control panel’s buttons, like following:

function on_command(command)
{
    if (command == "unlock")
    {
        // Press the unlock button (activate relay connected to I/O port 1).
        hardware.pin1.write(1);
 
        // Keep it held for a short moment...
        imp.sleep(0.1);
 
        // Release the unlock button (deactivate relay).
        hardware.pin1.write(0);
    }
}

agent.on("command", on_command);

With this added functionality, when our app makes a request to the URL “https://agent.electricimp.com/gpTxbKL5BAq1?command=unlock”, I/O pin number 1 on our Electric Imp will be turned on. As this pin is connected to the elevator’s control panel, we will have remotely activated one of its buttons by pressing a button in our app!

Full source code

The source code for the mobile app and the Electric Imp can be found in the evothings-gallery repository. Feel free to use it for your own projects!

Go and have fun!

It’s great fun bringing some friends together for a few hours and creating things like this. We hope that you feel inclined to try, and know that even the simplest project gives a great amount of satisfaction!

Download Evothings Studio now and get started within minutes. It is fun and easy!