Get on the Internet of Things fast with an embedded Web app server: Part 2

Wilfred Nilsen, Real Time Logic

January 01, 2014

Wilfred Nilsen, Real Time LogicJanuary 01, 2014

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.



Code Example 2: LSP is implemented using Lua script bracketed by <?lsp and ?> tags within HTML.

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.
Web application servers help provide a slick graphical interface without requiring expensive hardware. The web app can send data to and retrieve data from a server asynchronously (in the background) without interfering with the display and behavior of the existing page. The use of AJAX (Asynchronous JavaScript and XML) or JSON (JavaScript Object Notation) techniques provide this functionality and ensure an impressive response.

Web applications can easily be enhanced by JavaScript code to deliver and present real-time data from the server. In this exchange, the server initially generates the bulk of the user interface when responding to the initial GET request. After the browser loads a server-generated HTML page, it executes the embedded JavaScript code inside the HTML response data. This Javascript code can, for example, connect back to the server and fetch data in real time. Figure 5 shows how this instantly updates the client without having to refresh the page.



Figure 5: JavaScript code in the dynamically created HTML page uses Ajax when connecting back to the server. In this example, we submit an AJAX request for information about a satellite dish signal strength while processing an overview form, which is dynamically updated concurrently with the form being presented on the client side. The form submission in response to change from the client side is just as before, as in this example with the request to change the angle of the dish.

The XMLHTTPRequest object enables JavaScript code to send HTTP commands directly to a server, requesting, for instance, the strength of satellite signal. The browser's XMLHTTPRequest object enables the JavaScript code to use a callback function which allows it to return the signal strength data asynchronously. Because of this, there is no blocking while the client browser waits for that information.

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.

The XMLHTTPRequest API is weird and cumbersome, but luckily many JavaScript libraries such as the jQuery library makes it very easy to use this object. jQuery is ideally suited since it is small and makes it easy to write compact JavaScript code without having to learn the details of the browser's Document Object Model, as illustrated by code Example 3.




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.

< Previous
Page 2 of 2
Next >

Loading comments...

Most Commented