Learn how to control and monitor your lights from mobile, tablet and desktop.
15 minutes | 60ドル in hardware | basic level
For this tutorial you need an Arduino Ethernet or an Arduino UNO with an Ethernet shield. To complete this tutorial you need the following components.
Components | Buy | Price | Quantity |
---|---|---|---|
Arduino Ethernet | Sparkfun | 59ドル.95 | 1 |
Push button | Component packs | 0ドル.35 | 1 |
Led | Component packs | 0ドル.35 | 1 |
10K Ohm resistor | Resistor kit | 0ドル.5 | 2 |
To program your Arduino you need to install the Arduino IDE and the following libraries (if you do not know how to install a library checkout this tutorial).
Library | Notes |
---|---|
PubSubClient | This is a fork of the original PubSubClient setting the max packet size to 512 bytes (needed for Lelylan to work). |
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.
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).
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.
In this tutorial we'll use MQTT, a publish subscribe protocol for the Internet of Things.
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
Arduino. Copy the sketch below and change the following variables:
deviceId
, deviceSecret
,
outTopic
, inTopic
and clientId
(it will not take more than one minute).
Access Lelylan Dashboard and control your connected light from mobile, tablet and desktop. If any problem occours, let @lelylan know.
To better understand how the Arduino sketch works, follows a brief description of what most important sections do.
The deviceId
and deviceSecret
are used to authenticate
the physical object (Arduino) 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 Arduino 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.
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.
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.
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.
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.
Here we define the setup function where Arduino connects to the Internet using DHCP. When the connection is made, Arduino open 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, Arduino reconnects to Lelylan MQTT server.
This is why the lelylanConnection
method is also defined into the loop
function (see below).
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 status changes) we publish a message to Lelylan with the updated led status.
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.
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
Arduino 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).
If any problem occours check out the following points.
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.