Get on the Internet of Things fast with an embedded Web app server: Part 2
Lua Server Pages provide the ‘missing link’
Lua bindings permit a neat interface between script (such as Lua) and C, but the nature of HTTP commands means that we must also interface between Lua script and HTML. Lua Server Pages (LSP) and the bindings for them provide that ‘missing link between application tasks and the functions in the application server managing the HTTP operations. Just as for the Lua code, the just-in-time compilation mechanism makes development a breeze, as illustrated in Figure 3.
Figure 3: By using Lua applications and server pages, the results of any modifications to code are seen almost instantly. Compilation both from LSP to Lua and from Lua to executable code happens seamlessly and automatically. This is in stark contrast to high level languages such as C or C++, which require a build cycle before they can be uploaded for use.
The LSP scripts are the last component in the event-driven mechanism. They ensure that there is an appropriate response each time the web server interprets an incoming HTTP operation. For example, suppose a remote control device adjusts an incubator temperature control. The browser sends a HTTP GET request to the application server on the incubator temperature controller, which responds with the HTML form for the “adjust temperature” page. The browser then typically sends an HTTP POST request when the new temperature is selected (Figure 4), and the application server makes the necessary adjustment and responds with another HTML form.
Figure 4: The HTML forms contain input elements that allow the user to enter and send information to the server, such as the temperature set point for an incubator. The values of the input elements are transmitted to the server side when the user clicks a submit button.
The beauty of the LSP scripts is that they permit the server side logic to be incorporated into the HTML form itself. The Lua Server Page (LSP) in code example 2 shows how this looks in practice.
Example 2 Script code comments
Application servers can handle C/C++ code in addition to scripts. They include support for different plug-ins, secure transactions, and various types of I/O. They are very flexible; for example, they offer file storage and even database functionality if required, and yet they need not have a file system at all. An API that remains consistent between different operating systems gives ease of portability, perhaps allowing the use of the same code for an entire range of incubator monitors, each using different hardware and OS solutions but controlled by the same handset.
Crisp user interface from asynchronous data handling
An application server coupled with Lua scripts for high-level functionality and C source code for hardware interfaces creates a development environment that easily takes advantage of the simplicity of HTTP operations while maximizing your programming efficiency. A well-designed web application server does away with the lengthy high-level build cycle of code for everything but hardware interfaces. In so doing, it optimizes even the development work.
But is this enough to give clients what they expect to see?
Today’s demanding customers expect attractive displays and smartphone-like functionality from all of their devices, even those that are compact and low-powered, such as a dedicated hand controller for the positioning of a satellite dish. Such expectations provide a challenge for embedded developers.
From the user’s perspective, the client browser continues seamlessly without any apparent ‘freezing’. The presentation of the remainder of the form continues seamlessly and the real time data signal strength data appears when it becomes available.
Code Example 3: Using Ajax to respond to the satellite dish client application request for a signal strength update
Example 3 Script code comments
Embedded web apps: a different environment
The underlying HTTP communication technology for Web-based applications is the same, whether embedded or otherwise. But the difference in environment for the embedded sector makes a massive difference to the optimal deployment of that technology.
In this environment, you want developers to spend their time on application-specific code whether it controls tunnel lights or a satellite dish or drives safety-critical applications like a tank turret or incubator, where speed and efficiency are vital.
Using an application server specifically designed for embedded web applications ensures that generic HTTP communications are tackled with approaches which are optimized for the demands of limited resource, deeply embedded applications.
When used in conjunction with Lua Server Pages and AJX/JSON asynchronous data handling, the developer gains three technologies that combine to powerfully create the same quality of dynamic application they’ve come to expect from advanced smart phone and desktop applications.
With these tools, teams can focus on application development that gives users the interface they want, while letting the team dedicate most of its energy to the applications only they do best.
Embedded web apps the easy way
To be used in combination with this two part series on Embedded.com, we have created four online tutorials you can download using browsers running on Windows XP, Vista, and Windows 7 & 8. They will give you the fundamentals for writing your own web applications and interfacing these applications to your hardware. It's not technical to run; just download and run the executable. After unpacking, a black console window will open with some instructions. The demo will open your browser automatically after 30 seconds if you do not do anything.
After downloading the self-extracting zip file, start the self-extracting zip file and follow the self extracting zip wizard. Unzip to any directory. The self extracting zip file should automatically start the unpacked ‘startdemo.bat’ batch script, which in turn starts the demo server in a command window. Closing this window closes the server. You can restart the demo at any time by restarting the ‘startdemo.bat’ batch script. Uninstall by deleting all files unpacked by the self extracting zip file.
Read Part 1
Wilfred Nilsen, Founder & CTO of Real Time Logic,has 25 years' experience in designing embedded software. Powered by a vision of a connected embedded systems, he designed the Barracuda Application Server, tailoring it for the small footprint, real-time needs of embedded microcontrollers and microprocessors.