Lelylan Lab
(board not anymore in the market)

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

Hardware

To complete this tutorial you need the following components.

Components Buy Price Quantity
EZControl EZControl.it (out of stock) $45 1
Push button Component packs $0.35 1
Led Component packs $0.35 1
10K Ohm resistor Resistor kit $0.5 2

Software

To program your EZControl you need to install the Arduino IDE (version 1.0.6 or later) and the following libraries (if you do not know how to install a library checkout this tutorial).

Library Notes
CC3000 WiFi This is a library for the Adafruit CC3000 WiFi Breakouts. Designed specifically to work with the Adafruit CC3000 Breakout, it works perfectly with EZControl CC3000.
PubSubClient This is a fork of the original PubSubClient that works with the AdaFruit CC3000 breakout board (and EZControl CC3000), setting the max packet size to 512 bytes (needed for Lelylan to work).

Before uploading your sketch set the following configurations through the Arduino IDE.

Hardware Setup

This schema represents the relay connected to a light (represented by a relay), resistor and pushbutton setup. With this setup each time you press the button, the relay makes the light turn on and off.

Remember to be careful on working with mains voltages when using the relay (learn more).

Do not warry if you see the Yun board, we use it simply because Fritzing doesn't offer the EZControl board. To make the sketch work also remember to use the right pins. In the code below we used the pin 5 to control the botton and pin 7 to control the relay.

About the EZControl

EZboard is a low power consumption Arduinoâ„¢ compatible board, equipped with an onboard WiFi controller, microSD card socket, temperature sensor and power relay.

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 "Connect with MQTT" as connectivity option.

In this tutorial we'll use MQTT, a publish subscribe protocol for the Internet of Things.

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.

Code

Once the (virtual) device is defined on Lelylan you need to make it communicate with the EZControl. Copy the sketch below and change the WiFi (WLAN_SSID and WLAN_PASS) and device credentials (deviceId, deviceSecret, outTopic, inTopic and clientId). It will not take more than one minute.

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 EZControl sketch works, follows a brief description of what most important sections do.

Device Settings

The deviceId and deviceSecret are used to authenticate the physical object (EZControl) 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.

Lelylan uses MQTT, a publish subscribe protocol for the Internet of Things. To make Lelylan communicate with EZControl you need to set two topics: one receiving messages from Lelylan (inTopic) and one sending messages to Lelylan (outTopic). These topics are unique and identified by the device id.

Finally, you need to set the MQTT client id, a random string not longer than 23 bytes.

JSON Payloads

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

To properly generete and parse JSON you can use the aJSON library. We tried too, but had some problems with the memory management. If you make it work let @lelylan know.

If you want to know the property IDs of most common types or if you want to create your own types, check out the Types Dashboard or check out the Types API.

MQTT Signatures

To open the communication with Lelylan you need to initialize the MQTT client settings by defining the MQTT server address, the MQTT server port, a callback function (called every time a message is received from Lelylan) and the ethernet client.

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 need to change anything. If you use different pins, remember to change these values.

Led and Button Logics

Here we define the variables you need to make the button work correctly. If you are new to push buttons check out this switch tutorial.

EZControl Setup

Here we define the setup function where EZControl connects to the WiFi CC3000 module.

When the connection is made, EZControl opens the connection with the Lelylan MQTT server, and set the led and button pin mode.

During the MQTT connection phase the client set the deviceId as username and the deviceSecret as password. If the credentials are missing or not valid, the connection will be rejected.

To keep the connection alive after temporary loss of internet connection we check if the client is connected. When not, EZControl reconnects to Lelylan MQTT server. This is why the lelylanConnection method is also defined into the loop function (see below).

EZControl Loop

Here we define the loop function where we use a 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 turns on or off) we publish a message to Lelylan with the updated led status (this way the dashboard receives the physical update).

Publish Messages to Lelylan

To let Lelylan know when the led is turned on or off, you need to publish a message to the MQTT out topic with the updated properties.

Receive Messages from Lelylan

When a user updates a device property using Lelylan Dashboard a message is published to the topic devices/<DEVICE-ID>/get (in topic) and it's received from EZControl through the callback function. We check it the received message turns on or off the light and 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).

Troubleshooting

If any problem occours check out the following points.

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.

Social

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

Thanks

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