Using the NetIO (Network File System) Client on an Embedded Device

Accessing files remotely can be beneficial as a permanent solution or during development phases. The Barracuda App Server provides a Network File Server, known as the Web File Server, and a Network File Client, referred to as NetIO. Together, they enable one Barracuda App Server to serve files to another.

NetIo-Diagram

The Web File Server and NetIO are programmable, allowing for the creation of instances using C and Lua code. This tutorial will show how to activate these components using two development environments that offer a no-code option for configuring and setting up a client/server file system. Here, the Mako Server will function as the Network File Server, while the Xedge IDE will serve as the Network File Client. The Mako Server will operate on your host operating system, such as Windows or Linux, and the NetIO will be activated through the Xedge IDE, which can run on an embedded device or as an application on another Mako Server instance. For more information, refer to the Xedge IDE documentation.

Configuring the Mako Server

Begin by downloading and installing the Mako Server for your host operating system. Familiarize yourself with starting the server using the command line.

Next, create a directory specifically for this tutorial on your file system, for example, C:\Users\[username]\tutorial on Windows or /home/[username]/tutorial on Linux.

Within this directory, create a new file named mako.conf and insert the following configuration:

1
2
3
4
5
6
7
fileserver={
   fsname="fs",
   ioname="home",
   path="/",
   lockdir=".LOCK",
   noauth=true
}

The above configuration details are available in the Web File Server section of the Mako Server documentation.

Then, launch the Mako Server in this directory. You should see an output similar to:

Loading c:\Users\[username]\tutorial\mako.conf
Mounting /c/Users/[username]/tutorial/mako.zip
Server listening on IPv6 port 80
Server listening on IPv4 port 80
Loading certificate MakoServer
SharkSSL server listening on IPv6 port 443
SharkSSL server listening on IPv4 port 443
Creating file server at /fs/ -> c:\Users\[username]\tutorial\ ok
Warning: unprotected file server.

This output indicates that the Mako Server has successfully integrated a Web File Server instance into the virtual file system at "/fs/". Confirm this by navigating to http://localhost/fs/. You should see the content of the "tutorial" directory in your browser. In the above example, the server listens on port 80. If the server is not listening on port 80, include the port number in the URL e.g. http://localhost:9357/fs/

Configuring the NetIO using the Xedge IDE

Note: The following code was developed and tested using the ESP32 version of Xedge, called Xedge32. Consider reading the tutorial Your First Xedge32 Project before proceeding, as this tutorial includes introductory information, including a video, on using the Xedge IDE.

Before using the Xedge IDE, find the IP address of your host computer and replace the URL http://localhost/fs/ with a URL similar to http://192.168.1.100/fs/ and make sure you can navigate to this URL.

In the "tutorial" directory, create a new subdirectory and call it "MyLib." Use the Web File Server and navigate to this directory, e.g., to http://192.168.1.100/fs/MyLib/. Copy this URL.

In the Xedge IDE, right-click the "net" file system in the left pane tree view and select a new app.

In the name field, enter "MyLib". In the URL field, paste the Web File Server URL, click the Running button, and click Save.

Xedge New Application

Figure 1: The Xedge new Application Configuration wizard: adding a new NetIO-powered app.

You can now use the NetIO-enabled app like any other Xedge-managed app. In the following figure, we have created "test.xlua" using Xedge IDE. This file is saved to the MyApp directory on the host computer. You can edit files directly on your host computer using any editor, but note that new files are not visible in the Xedge IDE unless you click the browser's refresh button.

Xedge Test File

Figure 2: A test file created using Xedge IDE and saved to the host computer's file system using NetIO.

How to Use the NetIO to Develop a Lua Module

NetIO proves invaluable when developing large-scale programs, such as extensive Lua modules. This section guides you through creating a Lua module using the Xedge IDE and NetIO.

Begin by creating a .preload script in the MyLib directory. Copy and paste the following Lua code into the .preload script:

1
2
3
4
5
xedge.createloader(io)
 
function onunload()
   package.loaded["MyModule"]=nil
end

For the moment, proceed with this file as is. We will delve into its mechanics later.

Next, create a subdirectory named .lua. Create a file named MyModule.xlua within this directory and copy the Lua code below into MyModule.xlua:

1
2
3
4
trace "Loading MyModule"
return {
   version=1
}

Xedge Lua Module

Figure 3: The Lua Module MyModule created in the .lua sub-directory.

Now, restart the MyLib app via the Xedge IDE to execute the new .preload script (click .appcfg and toggle run button).

In the test.xlua file, insert the following code:

1
trace(require"MyModule".version)

This script will load the module and print the version number. Additionally, you should see the message "Loading MyModule" in the output, which originates from the print statement in MyModule.lua. It's worth noting that the only requirement for a Lua module is to return a Lua table. Refer to the Lua modules documentation for more details on module creation.

Understanding the .preload Script

The xedge.createloader() function is an Xedge-specific feature that allows the MyLib application to act as a Lua module loader. It injects special code enabling the "require" function to locate modules within the .lua subdirectory. For comprehensive details, consult the xedge.createloader() documentation.

The onunload() function is executed when the application is terminated through the Xedge IDE. This function removes MyModule from Lua's package.loaded table, ensuring that upon application restart, the module is reloaded and not cached. This behavior is crucial during module development, allowing for the iterative testing of the module without the need to reboot the entire device.

Programmatically Creating a NetIO Instance

A NetIO instance can be created programmatically using the following code snippet:

1
2
3
4
local nio = ba.mkio(ba.openio"net", "http://192.168.1.100/fs/MyLib/")
for name in nio:files("/") do
    print(name)
end

You can copy this code and execute it within the Xedge IDE. However, ensure you modify the URL to match your configuration before running the script.

Posted in Whitepapers

Complex Circuitry Got You Stumped?

Navigate the world of embedded web servers and IoT effortlessly with our comprehensive tutorials. But if time isn't on your side or you need a deeper dive, don't fret! Our seasoned experts are just a call away, ready to assist with all your networking, security, and device management needs. Whether you're a DIY enthusiast or seeking expert support, we're here to champion your vision.



OPC-UA

OPC-UA Client & Server

An easy to use OPC UA stack that enables bridging of OPC-UA enabled industrial products with cloud services, IT, and HTML5 user interfaces.

Edge Controller

Edge Controller

Use our user programmable Edge-Controller as a tool to accelerate development of the next generation industrial edge products and to facilitate rapid IoT and IIoT development.

On-Premises IoT

On-Premises IoT Platform

Learn how to use the Barracuda App Server as your On-Premises IoT Foundation.

Embedded Web Server

Barracuda Embedded Web Server

The compact Web Server C library is included in the Barracuda App Server protocol suite but can also be used standalone.

WebSocket Server

Microcontroller Friendly

The tiny Minnow Server enables modern web server user interfaces to be used as the graphical front end for tiny microcontrollers. Make sure to check out the reference design and the Minnow Server design guide.

WebDAV Server

Network File System

Why use FTP when you can use your device as a secure network drive.

HTTP Client

Secure HTTP Client Library

PikeHTTP is a compact and secure HTTP client C library that greatly simplifies the design of HTTP/REST style apps in C or C++.

WebSocket Client

Microcontroller Friendly

The embedded WebSocket C library lets developers design tiny and secure IoT applications based on the WebSocket protocol.

SMTP Client

Secure Embedded SMTP Library

Send alarms and other notifications from any microcontroller powered product.

Crypto Library

RayCrypto C Library

The RayCrypto engine is an extremely small and fast embedded crypto library designed specifically for embedded resource-constrained devices.

Embedded PKI Service

Automatic SSL Certificate Management for Devices

Real Time Logic's SharkTrust™ service is an automatic Public Key Infrastructure (PKI) solution for products containing an Embedded Web Server.

Modbus

Modbus TCP client

The Modbus client enables bridging of Modbus enabled industrial products with modern IoT devices and HTML5 powered HMIs.