Table Of Contents

Previous topic

LuaDeskSpot

This Page

Scripting Reference

Once the Lua SPOT is ready and deployed, you can add new applications to it by writing a Lua script. The script script is need to be compiled before you can actually send it to the Lua SPOT.

Lua Installation

Lua 5.1.4 is needed to compile the Lua scripts that are going to be installed on the Lua SPOT. The Lua source code can be downloaded from http://www.lua.org/ftp/lua-5.1.4.tar.gz. Follow the installation instruction inside the source code archive to install Lua on your machine.

Writing Lua Script for Lua SPOT

Before writing the script, you have to determine a name for your script. This name will also be your script identifier when installing and calling the script from the Lua SPOT.

The script should be written according to the following standard.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
appname = {
   func1 = function()
      -- your code here
   end,

   func2 = function()
      -- your other code here
   end,

   -- your other functions
}

Your application name will be a new table containing all functions that you want to make.

Functions can have one or more parameters. Lua SPOT will only pass one parameter, therefore if you expect more parameters, you have to split the parameters manually. There is a function provided by the Lua SPOT (luaspot.next_token) that will help you split the parameters.

Lua SPOT will only send the parameter using string format. You have to manually convert the string to number or other format you expect if you don’t want to use string. For example, you can use tonumber() function to convert a string to a number.

Compiling The Script

After writing the script, you have to compile the script into the binary format that later can be installed to the Lua SPOT.

Use the following command to compile the script.:

$ luac -o app.lbc app.lua

Substitue app.lua with your script name and app.lbc will be the binary format of your script.

Installing The Script

The binary version of the script can be installed to the Sun SPOT (with Lua SPOT installed) using the help of LuaDeskSpot application. Follow the instruction that has been explained earlier.

Lua SPOT APIs

In order to use facilities that are provided by Sun SPOT and Lua SPOT, there are several functions that can be used by the Lua script. These functions will be divided into two components, Sun SPOT and Lua SPOT API.

Sun SPOT API

This API provides functions to access functions provided by Sun SPOT.

  1. sunspot.led_on(pos)

    Turns on the LED at position pos.

  2. sunspot.led_off(pos)

    Turns off the LED at position pos.

  3. sunspot.led_rgb(pos, r, g, b)

    Sets the color of LED at position pos with RGB value of (r, g, b).

  4. sunspot.temp_celcius()

    Returns temperature value in Celcius.

  5. sunspot.temp_fahrenheit()

    Returns temperature value in Fahrenheit.

  6. sunspot.accel_x()

    Returns the accelerometer value at x axis.

  7. sunspot.accel_y()

    Returns the accelerometer value at y axis.

  8. sunspot.accel_z()

    Returns the accelerometer value at z axis.

  9. sunspot.sleep(delay)

    Pauses the execution for delay miliseconds.

Lua SPOT API

This API provides functions to access functions provided by Lua SPOT.

  1. luaspot.send(addr, msg)

    Sends a message msg to destination address addr using the routing function.

  2. luaspot.send_raw(addr, msg)

    Sends a message msg to destination address addr directly without using the routing function.

  3. luaspot.add_id(id)

    This function is used to insert a new message id provided in id to the message id table. This table is intended to store all received message id so messages with duplicate id can be dropped.

  4. luaspot.check_id(id)

    Checks whether a message id is already marked as received or not.

  5. luaspot.get_new_id()

    Returns a new globally unique message id.

  6. luaspot.get_node_addr()

    Returns the node address.

  7. luaspot.mem_set(app, var, value)

    Stores a value to the persistent storage. The value value is identified by application name app and a variable name var.

  8. luaspot.mem_get(app, var)

    Returns a value from the persistent storage. The value is located using the application name app and a variable name var.

  9. luaspot.dispatch(addr, msg)

    Dispatches a new message msg from source address addr to the main Lua SPOT message dispatcher.

  10. luaspot.lock(app)

    Creates a global lock identified by app. Other threads that are trying to acquire lock with the same identifier will get blocked until the corresponding luaspot.unlock function called.

  11. luaspot.unlock(app)

    Releases the lock identified by app.

  12. luaspot.next_token(str)

    Gets the next token inside the string str. This function returns two values, the next token and the remaining string.