Pushing Data to Your Device

Follow

Update: All of Exosite's APIs support some form of push or pseudo-push functionality. See CoAP Observe, HTTP Long-Polling, or RPC Wait. The method in this article still works, but the new API methods have fewer requirements.

 

The standard method for receiving data from the Exosite Platform is to simply poll the platform for new data. But, maybe you need faster updates or maybe you're on a mobile connection with a limited data allocation. What you need is push!

Instead of asking the platform if it has any new data every few minutes or seconds the push method allows you to simply let the platform take any updates it gets and push them directly to your device as soon as it receives them.

There are some requirements that your device will need to have for push to work. Mainly you'll need to have a publicly addressable IP address or other URL to tell the platform to push to. If your device will be behind a NAT router or other firewall, you may be able to use UPnP to request an open port. Second, your device will need to have some form of server that can receive HTTP POST requests.

To accomplish the push we're going to use the HTTP dispatch feature of Lua scripting on the platform. Paste the following lua script into the scripting interface in portals and modify the variables in the "Configure These Variables" section.

-- This script simply waits until a new value gets written to a 
-- datasource, then pushes that value to the specified url as
-- a URL encoded (ish) string of alias and value.

--------------- Configure These Variables ---------------------
local datasource_alias = 'led'
local url = 'http://198.46.157.16:13579'
---------------------------------------------------------------

local datasource = alias[datasource_alias]

while true do
  local new_timestamp = datasource.wait()
  local new_value = datasource[new_timestamp]
  local body = string.format('%s=%s', datasource_alias, new_value)
  debug(string.format('Sending Message: %s', body, date()))
  dispatch.http(url,
                "post",
                body,
                "application/x-www-form-urlencoded"
               )
end

Note that this is NOT production-ready code and is for demonstration purposes only. If you plan to use this method with your product, you should add real urlencoding and some error checking.

Have more questions? Submit a request

Comments