Lelylan Lab

Learn how to control and monitor your lights from mobile, tablet and desktop.
15 minutes | $44 in hardware | basic level


To complete this tutorial you need the following components.

Components Buy Price Quantity
Electric Imp Sparkfun $29.95 1
Breakout Board Sparkfun $12.95 1
Push button Component packs $0.35 1
Led Component packs $0.35 1
10K Ohm resistor Resistor kit $0.25 1


In order to connect Electric Imp to the internet follow the official Getting started guide. To fully understand this tutorial you need to know the Electric Imp's concepts behind Agent and Device entities.

Hardware Setup

This schema represents the led, resistor and pushbutton setup. With this setup each time you press the button, the LED is turned on and off.

Lelylan Setup

Open Lelylan Dashboard and create a new device by following 3 simple steps (if you are new to Lelylan, you can sign up for free).

1) Set a meaningful name (e.g. bedroom light).

2) Choose the device type. For this tutorial choose Basic Light.

What is a type? A type defines what a device is by defining its properties, functions and statuses. For this tutorial you do not need to now more about.

3) Choose Custom URI as connectivity option.

In order to communicate with the physical device you need to connect Lelylan with the Electric Imp agent. Every agent has a unique url that can be found in your Electric Imp Ide.

Copy and paste it into the Custom URI field. In this way Lelylan will forward all requests coming from the dashboard (or APIs) to the Electric Imp.

4) Get the Device ID and Device Secret.

Once the device is created, click the settings link (placed under the device name) and get the device ID and device secret. You'll need them in the next section.


Once the (virtual) device is defined on Lelylan you need to make it communicate with the Electric Imp. Copy the Electric Imp Agent and Device sketches below and change the <DEVICE-ID> and <DEVICE-SECRET> with your device credentials.

You are done

Access Lelylan Dashboard and control your connected light from mobile, tablet and desktop. If any problem occours, let @lelylan know.

Code Explained

To better understand how the Electric Imp sketch works, follows a brief description of what most important sections do.

Agent Code

Device Settings

The deviceId and deviceSecret are used to authenticate the physical object (Electric Imp) with Lelylan. To get the device credentials open the Dashboard, select the desired device, click on settings and copy the Device ID and the Device Secret.

Sending messages to Lelylan

To send the physical updates to Lelylan we defined the function lelylanSet(properties) which defines the Lelylan endpoint and fires the real request.

The request function fires the HTTP request to Lelylan. To make it work we first need to define the headers by setting the content type (JSON) and the physical secret. At this point we can fire the request and let Lelylan know the current light status (on or off).

Receiving messages from Lelylan

To receive the requests from Lelylan the Agent must declare a unique handler for incoming requests. In this tutorial we defined the lelylanUpdate function.

When, interacting with the Dashboard (or APIs) Lelylan sends a PUT request with the properties you want to change to the physical world (see Makers Api). The lelylanUpdate function checks for the X-Physical-Secret header in order to avoid unauthorized requests and sends an update event to the Device Entity with the desired light status in order to physically turn the light on or off. Once the led status is updated, a confirmation message must to be sent back to Lelylan to confirm that the physical changes were successfully applied (if not, the device will keep being pending).

Device Code

Led and Button Pins

Here we define the pins used to connect the led and the button. If you have followed the Fritzing diagram you don't have to change anything. If you used different pins, remember to change these values.

Led and Button Logics

Here we define the variables you need to make the physical button work correctly with the led.

Button's handler

We use the pushbutton as a switch. Each time you press the button the led is turned on or off. To make it work we need to add a debounce, otherwise everytime you press the button a single press would be recognized as multiple press.

Once the push button is pressed (the led status changes) we publish a message to Lelylan to confirm the updated led status.


When communicating with Lelylan we use a fixed JSON structure. For this basic example we defined first a Squirrel object which later will be JSON encoded by the Agent.

Remember that every message exchanged with Lelylan is made up by a list of properties where each of them contains a property ID and a property value. For the type "basic light" we have the status property with ID 518be5a700045e1521000001 accepting the values on and off.


Everything should work just fine. If any problem occours mail or Tweet us.

Get inspired

Did you like this tutorial? You could be interested in the following ones.

If you want to learn more check out the Lelylan Dev Center and Lelylan Lab.


If you like our work, let us know on twitter or by sharing the tutorial with anyone you think might find it useful.

Facebook Twitter Google Digg Reddit LinkedIn Pinterest StumbleUpon Email


This article is brought to life from the following people. Mail or Tweet us for any idea that can improve the project.