Setting up a Low Cost SMQ IoT Broker

Want to get your IoT project working in the cloud? Not a problem. Follow along this guide to get your own virtual private server up and running to support your IoT work.

The following Internet of Things IoT tutorial will show how easy it is to build your own IoT cloud solution and connect thousands of devices. For the purpose of demonstration, we have selected a $1 a month low-cost Virtual Private Server (VPS) with 64Mb of memory that is capable of serving up to 10,000 unique devices (tested). Note that the same software solution could easily scale up by using a VPS with more memory.

Instead of initially signing up for a paid for VPS service, you may initially test the installation script on your own computer. Scroll down to the section Windows and Linux Localhost Installation if you want to test the example setup on your own computer.

If you know how to connect to a VPS using SSH and if you quickly want to install the server, scroll down to the script we have prepared that automates the installation. Otherwise, start by viewing the following video:

Video 1: How to Install the IoT solution on an online VPS and how to use a domain name with the VPS

Beginner's Guide to VPS

Setting up your own Virtual Private Server (VPS) may sound complicated and expensive, but the reality is that setting up a VPS is very easy and the cost can be minimal. Note that we are not affiliated with any VPS service provider. We only provide recommendations on how to install the server solution online. If you are new to VPS or if you want to know about alternatives, read the following section:

The following is meant to be a quick introduction and not an exhaustive guide in VPS technology. There are several types of VPN technology and the most common are KVM and OpenVZ. A KVM is full hardware virtualization and you can run almost any operating system on a KVM, including Windows. A KVM is technically more difficult and challenging to set up than OpenVZ. A KVM also takes considerably longer to set up since the OS needs to be installed just as you would do on a regular computer. Our recommendation is to use OpenVZ since it is so easy to set up, it literary only takes seconds to set up an OpenVZ instance. OpenVZ supports Linux only, which just happens to be the best OS for the Mako Server when it comes to managing many (IoT) connections. OpenVZ functions similar to container technology such as Docker and the kernel used is the one installed by the provider. You do not have the option of using your own selected kernel version when using OpenVZ.

Both KVM and OpenVZ share the same hardware. It is up to the discretion of the VPS service provider when it comes to how many instances they install on the actual hardware. Unfortunately, some providers have a tendency to overstock the hardware which results in intermittent sluggishness or downtime. This is especially true of low end providers that we promote for the Mako Server. In most cases, a so called low end VPS is more than sufficient for running the Mako Server, but you need to make sure you select a provider that both looks to stay in business and does not overstock the hardware. You may select any VPS provider and if you are looking for a budget VPS you may be interested in following the lowendbox.com feed, which provides daily VPS discount information. However, keep in mind that many providers overstock the physical hardware. We have had great success with securedragon. For example, the SMQ broker test cluster runs on four O64 OpenVZ powered securedragon powered VPSs.

Another factor to keep in mind is that KVM and OpenVZ instances are running on blade servers installed in large datacenters. These datacenters may host both budget VPS providers and much more expensive VPS providers. What we have observed is that if/when a VPS goes offline, it is in most cases not the VPS that went down but the datacenter's network that went down. That means if you select a more expensive provider, the VPS will still go offline if the datacenter's network goes down.

New to Linux?

If you have never used Linux, you may think that this is not for you; however, nothing can be further from the truth. An online OpenVZ powered VPS is the best starting point for anyone new to learning command line Linux because no matter what you do with the VPS, an easy to use web based VPS control interface will let you restore the VPS to its original configuration by a button click. For this reason, playing around with an online VPS is a great way to learn command line Linux. If you get your Linux VPS into a state where you are unable to recover it, log into the VPS web control panel and click the re-install button. A fresh Linux VPS will then be available in one to two minutes, and you can start over.

Top VPS providers:

The web site LowEndTalk is a great place to ask questions on (budget) VPSs and they also provide a community rated top VPS provider poll.

Amazon, Google Cloud, Microsoft Azure, etc.

You may opt to install the IoT solution (using our installation script) on providers such as Amazon (AWS), Google Cloud, and Microsoft Azure. These providers provide free trial periods. However, note that setting up a VPS on these providers is technically much more challenging than setting up an OpenVZ VPS on providers listed on lowendbox, such as securedragon. Both AWS and Google Cloud tout free lightweight tiers and this may be an option since Mako Server is super light on resources. However, figuring out if the service can be free is difficult. In any event, you must provide credit card information when signing up, even for the free trial.

Getting Started

The cost of the cloud solution, for the purpose of this tutorial, is approximately $12 per year, which includes a VPS and domain name. The addition of security is optional, and the tutorial also explains how to install an SSL certificate for the online server.

We use the Mako Server Engine running as a background service on the Linux operating system to provide the device to SMQ Broker communications. Prior Linux experience is not required, however a working knowledge of a command line interface is necessary for terminal operations.

Signing up for VPS Service

  1. Navigate to securedragon.net, click OpenVZ, click O64, and click the Order Now button.
  2. On the next page in the Wizard, select Billing Cycle, select VPS location, and select Debian or Ubuntu as the operating system.
  3. Complete the wizard.

We have selected Secure Dragon as the VPS provider in this tutorial, however, you may use any VPS provider.

You will receive an email some time after signing up. The following shows an excerpt from such an email with details that you will need when configuring and installing software on the VPS.

Server Details ============================= Server Plan: O64 Main IP: 162.253.179.15 Root Password: https://securedragon.net/xxxxxx

Use the "Root Password" link to retrieve the VPS root password. You will need this password when connecting using SSH (explained below).

Setting up a Domain Name

When you sign up for a VPS service you get a dedicated IP address that uniquely identifies your online server. You can navigate to the VPS by simply using the IP address, but it is more convenient to use a domain name. In the following section, we will show you how to connect a domain name to an IP address.

Navigate to http://www.freenom.com, signup, and select one of the free domain names. During the registration process, select Use DNS and enter the VPS IP address in the two fields. Select 12 month period and click Continue. Your VPS should now be accessible via the domain name you registered. Note that it may take up to 48 hours before it works.

If you did not setup the DNS during the registration, do as follows:

  1. In the control panel, click Domain -> My Domains.
  2. Click on your domain and click Manage Domain
  3. Click Manage Freenom DNS. You should see the page below (Figure 2).
  4. Leave the name field blank and enter the IP address in the Target. 
  5. If you also want the server to be accessible as http://www.your-domain-name, go to the "Add Record" and add www in the Name field and the IP address in the Target field.

Figure 2: The two name records that make the server accessible with or without the www prefix

Installing the Mako Server

The server must be installed from a Linux console and your VPS is accessible via Secure Shell (SSH). You can login to the VPS using the information provided in the email you received after signing up for the VPS service. Install the Putty SSH client if you are using Windows as your host operating system.

Using SSH, enter the server's IP address (from the email you received). You can also use the new domain name if the DNS is ready. You can test this by pinging your domain name. The ping command should respond with your server's IP address if the DNS is ready.

Figure 3: Running Putty on Windows and connecting to online VPS using the domain name simplemq.tk

At the login prompt, enter the user 'root' and the password that is set for your VPS. You can proceed to installing the Mako Server as soon as you are logged in.

The Mako Server and the SMQ LED example can be installed in two ways: automatically (by using a script we have prepared) or manually.

Install Mako Server Automatically

The following script downloads and installs the Mako Server as a Linux Daemon. The script also downloads and installs the Web Based LSP IDE and the SMQ LED Example's server side code.

Copy the following and paste into the Linux console (Putty)

wget http://makoserver.net/install/brokerX86/install.sh; chmod +x install.sh; ./install.sh

You will be asked for a username and password during the installation process. The username and password enable you to securely mount/map your online server's home directory as a WebDAV network drive and access the LSP IDE.

The server will be running when the installation script completes. Proceed by making sure everything is working.

Manual Installation

Installing the server manually will give you a deeper understanding of how to manage and install software on an online VPS. Note that the following instructions do not include installation of the LSP IDE, which is installed when using the automatic installation option above.

To manually install the Mako Server and configure the Mako Server, proceed to the tutorial Installing Mako Server as a Service on Linux and navigate back to this tutorial when you have completed the installation process.

When the server is installed, enter the following commands (or copy and paste) into the SSH terminal window (you must be running as root):

su mako; cd; cd www; wget http://makoserver.net/download/IoT-LED-Broker.tar.gz; tar xvzf IoT-LED-Broker.tar.gz; rm IoT-LED-Broker.tar.gz; exit; /etc/init.d/mako.sh restart;

The above commands download a prepackaged SMQ LED example application (IoT-LED-Broker.tar.gz ) and install the package in the 'www' directory. The last command restarts the Mako Server, where the Mako Server loads the new SMQ broker application.

Testing the Online Server

After completing the installation, navigate to your domain name using your browser. Use the VPS IP address if your domain name is still not working (it takes time for DNS to replicate). You should see the LED demo. The SMQ LED example's web page is included in the package (IoT-LED-Broker.tar.gz) and enables you to quickly verify that everything is working. You can delete the LED example when no longer needed.

The LSP IDE:

You may enter the LSP IDE's user interface by using any non existing URL. The LSP IDE traps all non existing access by installing a 404 not found hook. Click any link to login and login using the credentials you set when installing the server.

Mount Server as a Network Drive:

You should be able to directly work on the server's 'www' directory from your own computer as soon as the online server is installed.

Video 2: Mounting your online server as a network drive.

Your Web File Manager and WebDAV server URL: http://server-address/fs/


IoT Edge Node Source Code

We also recommend downloading the SMQ C client stack and LED example code from GitHub; then modify the example's C source code URL to point to your own domain name, compiling the example, and making sure you can connect the edge node example to your own server/broker.

The following video shows how to use the free ESP8266 IDE with the SMQ LED example.

Video 3: Connect an ESP8266 to your IoT server using the easy to use ESP8266 IDE

See the SMQ source code page for additional language support, including Java and JavaScript.


Windows and Linux Localhost Installation

Instead of initially signing up for a paid for VPS service, you may initially test the installation script on your own computer.

The installation script is designed for Linux, but the installation script also works on Windows 10 if you install the Linux sub system. If you are using Windows, open Microsoft Store and search for "debian". Install the Debian Windows Subsystem for Linux and open a bash shell.

The following video shows how to use the installation script on the Windows Subsystem for Linux. You would use the same installation on a standard Linux distribution.

Video 4: How to Install the IoT solution on your own computer

Note that the installation script requires the command lsof, which is not installed by default on Windows 10. Run the following in the bash shell prior to running the installation script.

sudo apt update sudo apt install lsof

The following commands install the server and IoT solution on your local computer, but note that you must run the command "sudo bash" prior to pasting in the installation commands. After running the command "sudo bash", paste in the following in the bash shell:

wget http://makoserver.net/install/brokerX86/install.sh; chmod +x install.sh; ./install.sh

You may install the Mako Server if you have another server using the HTTP port 80. You may also ignore warnings printed by the installation script if you have another server running. The server will listen on port 9357 if you have another server using port 80. Enter the URL http://localhost:9357 in the browser if the Mako Server is unable to open port 80.

In the above video, we also install git, make, and gcc. We use these commands for downloading and compiling the SMQ C client. The following commands download and compile SMQ:

git clone https://github.com/RealTimeLogic/SMQ.git cd SMQ make ./led-smq -blocalhost/smq.lsp

Note that the Mako Server starts automatically when booting the computer and when using a standard Linux distribution. However, the Windows Subsystem for Linux is inactive until you start it, and the subsystem seems to ignore Linux boot scripts. You must therefore manually start the Mako Server from Bash as follows if you restart your Windows computer:

sudo service mako.sh start

Uninstalling the Server and Example

You may completely remove all traces of the server and example as follows:

sudo bash /etc/init.d/mako.sh stop;sleep 1 update-rc.d -f mako.sh remove rm /etc/init.d/mako.sh deluser mako rm -rf /home/mako rm /bin/mako*


SSL Certificate

Installing an SSL certificate is not required unless you need secure communication for your SMQ broker and/or web applications running on the server.

At a minimum, you need to follow the Server SSL Certificate Installation Tutorial. This tutorial is for installing an RSA certificate. You have some more work to do if you plan on using the secure SMQ device client (SharkMQ) and connecting this client to your online SMQ broker. The secure SMQ LED device demo is setup to use Elliptic Curve Cryptography (ECC) Certificates. You could change the example to use RSA certificates, but this solution introduces another problem. RSA certificates are big and our chained certificate signed by Comodo is even bigger. Chained RSA certificates are no good when communicating with resource constrained edge nodes. The solution is to use ECC certificates and no intermediaries.

The cool thing about the Mako Server is that we can set it up to use an RSA certificate signed by a well known CA and serve this RSA certificate to browsers and set up a different ECC certificate for edge nodes. A complete tutorial on how to set up a dual certificate RSA/ECC server can be found in the online SharkSSL documentation under section Certificate Management for IoT.

Related SMQ IoT Articles



Posted in SimpleMQ