Skip to content

josemotta/IoT.Starter.Np2.Core

Repository files navigation

IoT Starter Core for Netduino Plus 2

Introduction

In the world there are tenths billions of smart devices connected to the Internet right now, including all sizes of computers, tablets, cell phones, smart watches, pagers, etc. The IoT technology expects to double this quantity before 2020, which represents a $6 trillion market to be developed in the next few years.

IoT development definitely challenges us to behave differently, compared to current computing environment, based on human interaction with screen, touch, keyboard and mouse. The difference is that IoT devices should interact directly with nature, processing data in a real time basis.

The development process is expected to be more complex and may require proper tools for developers. Did you consider buying an oscilloscope for debugging? In order to speed up the IoT development, this article introduces a IoT Starter Kit that may help to speed up an IoT initiative.

Sources are available at Github repository IoT.Starter.Np2.Core.

Background

Supposing you are trying to become an IoT entrepreneur, how to minimize the risks for an IoT initiative? How to satisfy users, investors and decision makers that support your "idea" for an IoT project? It is common knowledge that some wise steps may reduce the risks:

  • Business Plan
  • Team
  • Methodology
  • Proof of Concept
  • Starter Kit
  • Pilot
  • Commercial

Then, suppose the original "idea" has grown enough to fit in a profitable business plan and it was accepted by the investors. After that, an executive manager get involved and gathered a compact developer team, well trained to apply an agile methodology that assures a smooth and continuous workflow. Great! Where are we now?

Some time has passed since the investors came in and they are already pushing for results. We should speed up the development process to launch product as soon as possible, they say.

We just started Proof of Concept, when we expect to build the preliminary version that comply with users and investors needs. This is the first version and it should not handle all the complexity required by the final product. The goal is to generate a healthy embryo. Since methodology and team evolve the embryo continuously, we can start it small and each version will add functionality and new tests, until we reach the Pilot prototype.

At this moment, a Starter Kit may help the team to focus in the main concepts behind the IoT initiative and make it actually profitable. Better than that, the resources we got until now - the manager leading the team, applying the methodology for smooth workflow, all them are fully operational, ready to run, immediately after the Starter Kit is installed.

Building the Starter Kit

Since IoT requires smart devices connected to the Internet, we should choose the product platform, which means a CPU and a network interface. There are many nice ARM platforms today, including Arduino, Netduino and Raspberry PI, among others. The latest models added wired and/or wireless network interfaces to the processor boards, still keeping the low cost. Naturally, we should get the best one that fits our project and our budget.

We should also consider that hardware and software for the product platform should be kept minimal, just to fit the IoT initiative. Most cases include a custom hardware, maybe a daughter board or a shield added to the Arduino bus. Simulating the team workflow, we conclude that most of their time will be spent programming and testing a software application being executed on the target platform, interacting with the customized hardware.

Then, we need an external development system, installed on a microcomputer, in order to have:

  • a programming language and its compiler generating code for the product;
  • utilities to load the executable code and debug it on the target platform;
  • a library containing communication software, providing basic Internet services;
  • a library with other utilities, for disk storage, for example;
  • software library modules, developed for IoT, like IR codecs or sensor/motor drivers.

The first platform chosen to build a Starter Kit is Netduino Plus 2, an open-source electronics platform using the .NET Micro Framework running at 168 MHz with 1 MB Flash and 192 KB RAM. Please check article from Guruprasad detailing the Netduino Plus 2.

Main reason that justifies Netduino is the availability of MS Visual Studio as a development tool. We should also mention the C# programming language, its compiler and the corresponding loader/debugger that make life much easier. The .NET software libraries also provide utilities we need to handle disks and other periferals.

To use Netduino, as recommended, install the development environment in the following order: 

   1)  Microsoft Visual Studio Express 2013
   2) .NET Micro Framework SDK v4.3
   3) .NET MF plug-in for VS2013
   4) Netduino SDK v4.3.2.1

To use the Starter Kit, download it from Github at IoT.Starter.Np2.Core. Three VS projects can be found at the Starter folder:

  • Controller: contains the main application, described below;
  • FileServer: a modified version of Netduino File Server, from Oneleggedredcow, to process files stored at the micro SD disk;
  • HttpLibraryV3: a modified version of HTTPLibrary V3, from Nart Schinackow, including HttpServer and corresponding HttpRequest and HttpResponse.

The File Server is capable of executing the following commands:

  • List: lists the files that are on the netduino SD card;
  • Get: transfers a file from the netduino to the PC;
  • Put: transfers a file from the PC to the netduino;
  • Delete: deletes a file from the netduino SD card

The HttpLibrary V3 supports a simple authentication method and has also a NtpClient utility to initialize date and time directly from the Internet.

Controller

The expected behaviour from the starter is a web server that listen on port 80 and answer with the home.html file, loaded from SD disk. It also accepts HTTP commands from browser to turn on and off the board´s OnboardLed. The browser response includes current date/time , in order to check if NtpClient is properly running at initialization. Then, there are three comands to test, supposing IP address below: 

http://10.1.1.105/

http://10.1.1.105/cmd?on

http://10.1.1.105/cmd?off

The Main() program, shown below, is responsible for the Starter Core functionality.  First, it tries to set local time using NtpClient.GetNetworkTime(). The onBoardLed, used by Http Comand is also declared.

public class Program
{
    static HttpServer Server;                   // server object
    static Credential ServerCredential;         // server security
    static Configuration ServerConfiguration;   // configuration settings
    static bool status = false;                 // on board led status

    public static void Main()
    {
        TimeSpan elapsed = TimeSpan.Zero;

        // Try to get clock at system start
        try
        {
            var time = NtpClient.GetNetworkTime();
            Utility.SetLocalTime(time);
        }
        catch (Exception ex)
        {
            // Dont depend on time
            Debug.Print("Error setting clock: " + ex.Message);
        }

        // On board led
        OutputPort onBoardLed = new OutputPort(Pins.ONBOARD_LED, false);

        Thread.Sleep(1000);

        // Web Server
        ServerConfiguration = new Configuration(80);
        ServerCredential = new Credential("Administrator", "admin", "admin");
        Server = new HttpServer(ServerConfiguration, ServerCredential, @"\SD\");
        Server.OnServerError += new OnServerErrorDelegate(Server_OnServerError);
        Server.OnRequestReceived += new OnRequestReceivedDelegate(Server_OnRequestReceived);
        Server.Start();

        // File Server
        FileServer server = new FileServer(@"\SD\", 1554);

        while (true)
        {
            // null task

            onBoardLed.Write(status);
            Thread.Sleep(500);
            
        }
    }
 }

The web server is started using pre-initialized admin credentials, they will be required when browser asks for data. The File Server is also started at port 1554 to handle micro SD disk. At last, a null task loop updates continuously the onBoardLed, according to HTTP command status.

The Server_OnRequestReceived(), shown below, sets the status, depending on the HTTP command received. If no parameter is passed from the browser then home.html file is sent back to browser.

    static void Server_OnRequestReceived(HttpRequest Request, HttpResponse Response)
    {
        if (Request.RequestedCommand != null)
        {
            switch (Request.RequestedCommand.ToLower())
            {
                case "on":
                    status = true;      // on board led ON
                    break;
                case "off":
                    status = false;     // on board led OFF
                    break;
            } 

            Response.WriteFilesList("<br>" + "Comando " +
                Request.RequestedCommand.ToLower() +
                ": Status = " + status.ToString());
        }
        else if (Request.RequestedFile != null)
        {
            string FullFileName = Request.FilesPath + Request.RequestedFile;
            if (File.Exists(FullFileName))
            {
                Response.WriteFile(FullFileName);
            }
            else
            {
                Response.WriteNotFound();
            }
        }
        else
        {
            Response.WriteFile(Request.FilesPath + "home.html"); // TODO: product page
        }
    }

The screenshot below shows the browser after the HTTP comand sent to the connected Netduino Plus 2. The Starter is now able to establish core functionality and communicate with outside world. It is ready to be equipped with interesting other Things!

Points of Interest

This is the first article about IoT Starter Kit and I expect to improve it with other platforms and also creating real  applications that use it.

History

 2016.09.02 - Initial version with Core functionality for Netduino Plus 2.

Releases

No releases published

Packages

No packages published

Languages