Electronicdesign 5999 Fabienfleutot595x335

Use Lua To Develop M2M Embedded Applications

March 25, 2013
With the projected growth in machine-to-machine (M2M) communications, the number of skilled embedded developers will become a limiting factor. It’s therefore critical to simplify and shorten the development process as much as possible—and Lua can help.

Machine-to-machine (M2M) communications is a large and growing market. Solar and wind power generators, automated teller machines (ATMs), vending machines, highly specialized industrial engines such as heavy-duty compressors, portable water treatment plants, professional equipment, and more all can use M2M to connect to the “Internet of Things.”

M2M implementation is challenging, though. Although the individual components can appear to be simple, building a complete solution requires expertise in the customer’s specific business, embedded hardware and software, generic IP networking, wireless telecom, airtime billing systems, enterprise systems, and Web applications.

With the projected growth in M2M, the number of skilled embedded developers will become a limiting factor. It’s therefore critical to simplify and shorten the development process as much as possible—and Lua can help.1

In fact, our goal is to do for M2M what Ruby-on-Rails (RoR) did for Web servers.

It was once considered normal to spend one’s day in a PHP spaghetti mess, concatenating SQL strings with code injection attack opportunities, and hand-writing mindless object/relational models. Only after RoR did people realize that most of what they did looked like work but was a waste of time and money.

On The Bench

M2M, for example, can be used to remotely monitor several sensors such as temperature, humidity, and lighting and command a few actuators such as a light switch and a roof opener in an industrialized greenhouse (see the figure).2

A Lua-powered gateway puts intelligence on the edge, optimizing communication.

The periodic acquisition of sensor values and actuator control is made using a PLC talking modbus.

The intelligence of our development case is implemented in Lua on a wireless gateway, which will read sensor values through the programmable logic circuit (PLC) using the modbus protocol and push the data out to the cloud (or a server). Commands received from the cloud/server are transmitted to actuators thru the PLC using modbus commands.3,4


Our first step is to read the raw data from modbus, pre-process it, and post-process it into directly usable data (Code 1).

Second, use message queuing telemetry transport (MQTT) to report sensors’ values. This provides a simple way to broadcast data to anyone interested, under a publish/subscribe model (Code 2).

Third, make things more systematic. Organize regular polling and caching, with different polling periods (Code 3).

Related Articles
Use Lua To Address M2M Development Challenges
Accelerate Embedded Development With Lua
A Successful “Internet of Things” Hinges On M2M

This example shows relatively simple local actions: control the light with the switch. We can quickly change our mind and assign it, for instance, to the luminosity detector. If we figure out that the original spec is wrong (e.g., low luminosity switches the light on, which raises the luminosity, which switches the light off, so what’s actually specified is a stroboscope), Lua allows us to see it and try alternatives in minutes, instead of refactoring C code for half a day to implement a trivial change in a state automaton.

Note that MQTT is great for small-size fleets and low latency, but businesses need to be able to scale their solutions to manage large fleets. In that case, managing one device in fractions of a second is less important than answering a million devices in a couple of minutes. That’s where a cloud-based scalable solution offers a better option. So, we’ll replace MQTT with an interface to m2mop.net, through a demo account obtained from http://developer.sierrawireless.com/Cloud Platform.

Fourth, we’ll replace the MQTT server to use our M2M cloud platform, which is designed for large fleet management and is built to support a whole solution through its complete life cycle. This allows an enterprise manager to add, remove, and update applications and device firmware remotely over the air; manage policies to consolidate and report data according to specific parameters; and protect data from loss.

We offer two distinct application programming interfaces (APIs) for data reporting: just give Lua table objects to the embedded agent, and it will do the most sensible thing, declare an advanced staging table to optimize bandwidth, perform consolidation locally, or make critical data crash-proof (Code 4).


Writing M2M applications has always been possible, although it hasn’t always been sensible in terms of cost and time. With Lua and dedicated libraries ready to deploy on specialized and on generic open-source hardware, these applications can be developed with a high degree of productivity.

The radical focus on productivity in development frameworks has deeply transformed the Web and the world, enabling the Web 2.0 revolution. Delivering the same empowerment to the Internet of Things will have similarly far-reaching consequences, and the Lua open-sources framework will accelerate this change. 


1. Lua’s open-source materials can be found at http://www.eclipse.org/mihini/.

2. For the Lua Workshop 2012 presentation slide deck, go to www.lua.org/wshop12/Fleutot.pdf. For the presentation video, go to www.youtube.com/watch?v=eX_L0yiQekI.

3. For a detailed description of the hardware components used, visit http://developer.sierrawireless.com.

4. For the full code used in each step, visit http://github.com/fab13n/wshop12.

Fabien Fleutot is a technical expert at Sierra Wireless. He is an expert in embedded systems and programming languages semantics, as well as an active member of the Lua community. He has more than 12 years of experience with embedded, distributed, and networked systems, both in academia and in the industry, including five years at Sierra Wireless designing better ways to create and deploy M2M solutions.

Sponsored Recommendations


To join the conversation, and become an exclusive member of Electronic Design, create an account today!