Code
Once the (virtual) device is defined on Lelylan you need to make it communicate with
Netduino. Checkout
the sample code on Github.
Lelylan Core
The LelylanCore
static class defines the device settings.
The DeviceId
and DeviceSecret
are used to authenticate
the physical object (Netduino) 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 Netduino 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.
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
.
Be careful to payload, you have to build a JSON array with the status id of your device
(you can find the status id by clicking settings under your Device Name, the clicking on
the device type name in "Device details" section.
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 the Types API.
Program Class
The Program
class is the entry point of your application, a static class to invoke your device logic (controller).
Controller Class
In the Controller
constructor of this class you need to define a unique identifier for your MQTT client
(random string, max 23 bytes). Once you have defined it you can create an instance of
your MQTT client.
The RunLightMonitor
public method starts the netduino application. After settting an
onboard led reference, the code runs into a loop. In this loop we set the connection with
Lelylan (LelylanConnection), read the led status and publish it on Lelylan when changing.
The
public method starts the netduino application. After set an
onboard led reference the code runs into a loop. In this loop we set the connection with
Lelylan (LelylanConnection), read the led status and publish it on Lelylan when changing.
To communicate with Lelylan Netduino needs to initialize the MQTT client connection
to the MQTT server. You have to do it before a connection being active. In LelylanConnection
method you can see the detailed code explanation.
The MqttMsgPublishReceived
is the event that receives the requests coming from
Lelylan to apply them to the physical world.
More information about M2MQTT .NET client are available
here.
Deploy
Once you are done ready we can deploy the code into the Netduino board. Make sure that
the deployment properties are the same as the picture below and then
select Debug -> Start Debugging (or press [F5] button) to run and deploy your sketch
into your netduino board.