Ubidots-Lua: OpenWRT router + Ubidots = Real-time data monitoring

In my projects I needed a tool to monitor data in real time, the data is captured by a microcontroller and uploaded to the web using a wireless router. Initially I was trying to use yeelink since it is in chinese langguage and yet I'm a Malaysian Chinese.

One month ago, I have tried with Ubidots, there were a few things that caught my eye.

Ubidots is a cloud service to store and analyze sensor data in real-time, it is free with 30,000 dots/month data-points usage, features as follow:

Push Data from Internet-Enabled Device

Display sensor data through widgets without any coding required.

Data are updated in real-time and no need to refresh the browser

Trigger alerts when a sensor value hits a threshold.

Powerful and flexible API

There are number of well-documented tutorials and sample projects provided by Ubidots, however there is no simple way to upload data by using an OpenWRT wireless router. After spend some time studying on the TP-Link TL-Wr103ND V2 project, this "Ubidots-Lua" is developed.

Ubidots-Lua is written in Lua, it allows you to post data to or get data from Ubidots with minimum coding required. Ubidots-Lua should run on any OpenWRT wireless router, and it is able to execute from console, Shell script or as a Lua function call.

Getting your Ubidots API key

On the screen top right corner, click on your user name and select My Profile

Click on API keys to view your API key

Write down your API key, which are then used in every API request.

Create Ubidots data source and variable

You must setup the Ubidots data source and variable before you can upload data to your Ubidots. A data source represents a device or a virtual source, while a variable is a series of data containing values that change over time.

Create a Widget

Widgets are custom visualizations of your data that help you extract the value of it. Example, you can display your sensor data through widgets like line charts, gauges, multi-line charts, scatter plots or maps.

Click on Add Widget icon from Dashboard as shown in figure below

Select a desired widget as shown in figure below. I'm select Metric Widget in order to displays the last value of a variable

Now choose a Data source and Variable

Click Finish to complete and return to Dashboard

Follow picture below to get the URL for the variable

You Use this URL to share a public version of your widget

When you post a value to this variable, you should able to read its value from this URL. It is so simple and yet no coding required.

Execute ubidots-Lua from command-line interpreter

It is time to post your data to Ubidots. With only a single line command, you should able to post data to your Ubidots.

SSH to OpenWRT via PuTTY and running the following commands in the terminal. This will post a value of 100 to your Ubidots Variable[email protected]:~# /usr/lib/lua/ubidots.lua -post api_key deviceID 100

You must replace api_key and deviceID with your API key and Ubidots Variable ID respectively. Upon posting this data to your Ubidots, its data is available in the widget URL immediately.

You may want to retrieve your data from a Ubidots Variable ID, but this is not often necessary since using a Ubidots widget is much more simple and flexible.

Retrieve last value from Ubidots Variable and save it to router /tmp/data.txt directory[email protected]:~# /usr/lib/lua/ubidots.lua -get api_key deviceID last data.txt

Calling Ubidots-Lua function from lua

Here is a lua script demonstrate how to use the Ubidots-Lua as a function call. You need only call get_token() once.

Ruuning Ubidots-Lua from Ash

OpenWrt comes with Ash shell.

Running Ubidots-Lua from Bash shell

I'm not able to install Bash onto my TL-MR3020 wireless router, the limited 4MB flash router have not enough space to installing Bash. You should get a wireless router with at least 8MB flash memory, for example: Nexx WT3020, GL.iNet, TL-WR1043ND.

I have made a time-lapse project which read the elapsed time from an Arduino and send its value to Ubidots, however it is not fully tested yet. Please give me some time and I will upload the source code to Github on this weekend.

You can read the value from DHT11 via serial and hence a micro controller is required. The micro controller such as Arduino read the data from DHT11 and send the value to the serial port of the router.
For the router, you can use the USB port for serial communication, however certain router (eg. GL.inet) embedded with build-in serial port, so you can use it to communicate with your micro controller.
For serial programming, you can use Lua script or BASH script to read data from your micro controller.