Holiday Light Show
Holiday Light Show is one of our standard Apps that comes with the Liberty IoT Hub.
We are using it as an example for developers to learn application development.
Liberty-IoT-OS and Liberty App Engine is the one and only logically complete IoT system.
A logically complete IoT system gives users complete control to use any possible algorithm over devices.
Nevertheless, algorithms (Apps) still have to be built by programmers. However, regular users can simply use the Apps in the most intuitive and straightforward way, without writing a single line of code.
Creating the App
In the “Develop” tab, click “Add” button on the upper left side tool-bar.
A dialog pops up. Choose your Developer Name. In this example, the Application is named “Tutorial1”.
An Application with the name “Tutorial1” is created. Note in the example we use Developer Name of “LibreHome”. You need to use your own developer name.
On the right side a source code editor will open with title of “Tutorial1”.
The applications are displayed with icons indication the current status of the application.
- - Lua new application, checked out for editing
- - Lua application, currently running in production mode, source code checked out for editing
- - Lua application, currently disabled by user
- - Lua application, currently disabled by user, source code checked out for editing
- - Lua application, currently disabled because of error
- - Lua application, currently disabled because of error, source code checked out for editing
Copy Source Code
Copy the code below to the editor.
function MyLightShow(groups) while true do -- groups is a list of control group for _, group in ipairs(groups) do for _, lightState in ipairs(group.lightStates) do Libre_DeviceLevelSet(lightState.light, lightState.onLevel) end Libre_Wait(group.wait * 1000) end end end
From tool bar on top, drop “Developer” menu, choose “Deploy”.
App Friendly Name
An editor dialog will pop up. Note the app file name is “Tutorial1”. Developer can give it a user friendly name. It can be any string. In this example, we give it a name “Libre Home Tutorial My Light Show”.
Edit Input Data Structure (Signatures)
This step is very important! Since Lua is dynamic language, the function signatures can not be inferred from techniques such as reflection (in Java or .Net).
Developer has to manually specify the exact structure using our GUI tool.
The final signature for MyLightShow function looks like below:
- The function only takes 1 argument, “groups”. It is an array (we call it “List”). We give it a name “Light Groups” (in “Display Name” column).
- Based on the code, each item in the array is a struct (like “class” in C++ or Java, we call it “Table”). Naturally we give it a name “Light Group”.
- According to code, each “Light Group” object has two members, “lightStates” and “wait”. Those members has to be added with exactly the same names appeared in the code (in “Native Name” column). The “Display Name” can be any string developer chooses. In this case, we name them “List of Lights” and “Wait for (Seconds)”.
- Member “lightStates” is another array.
- “lightStates” (List of Lights) is an array of struct (Table)
- We give “lightStates” member a friendly name of “Light & Level”.
- “Light & Level” has two members
- Member “light” is a device, we will take both “On/Off” devices and “Level Control” devices.
- Member “onLevel” is an integer with value between 0 and 255, inclusive.
- Friendly name of “Light” and “Level” are given.
- Member “wait” is a number, representing wait seconds. A real number with ‘0.001’ (millisecond) precision.
Apart from the friendly name “Display Name”, extra constraints of some data types can be given.
Remember we only want to take “On/Off” and “Level Control” devices.
We need to add to types of devices separately.
Finally “Device Type” will have two entries.
Using the “Attribute” menu on the top tool bar, extra attributes can be added to data type node.
Apart from “Device Type”, we added two other attributes to “Light”.
“Min”, “Max” and “Step” specified for “Level”.
“Min”, “Max” and “Step” specified for “Wait for (Seconds)”.
For more information about Attributes, please read the chapter “Attributes”.
Testing the App
From your Smartphone, navigate to “Tasks” -» “Add”.
You will be able to see newly added App “Libre Home Tutorial My Light Show”. (You can also search for it).
Choose the only function “My Light Show”.
Create a New Task
Follow the instruction in “Creating a Task”.
Add two lights to the task. Once the task is started, the two lights will start blinking with the pattern you specified.