If you have had a chance to play around with some of the new Arduino-enabled hardware platforms such as the ESP8266 Wi-Fi SoC, you may already have used an online IoT service for your project. In this article, we will show you how to setup your own online service by setting up a Virtual Private Server (VPS) and server software for your Arduino IoT project.
In addition to setting up your own VPS, we will show you how to install a simple example that lets you control LEDs, lights, etc., by using a browser. The following figure shows how any number of devices can be controlled in real time by navigating to your own VPS using a browser. The online VPS functions as a proxy and makes it possible for any number of users to control the devices via the online server.
Some of the benefits of using an online server and operating it as a proxy for routing messages between users and the devices are:
Enables users on one network to control devices on another network. For example, a device connected to a home network can be controlled via a Smartphone connected to the cellular network.
Devices operate as network (TCP/IP) clients, thus no router port forwarding or other tricks are required for gaining access to the devices via the online server.
Devices acting as network clients operate in stealth mode, thus greatly improving the security of the devices. We will get more into security later.
Small devices typically do not have the resources for providing a direct web interface. The online server manages all the heavy lifting of providing a web interface for the devices, thus greatly minimizing the code complexity in the devices.
Virtual Private Server
Setting up your own VPS may sound complicated and expensive, but the reality is that setting up a VPS is very easy and the cost can be minimal. It is possible to find VPS services as low as $8 a year. My favorite site for shopping for a budget VPS is lowendbox.com and one of my favorite VPS providers is securedragon, where you can get a three-month lease for less than $6 and a yearly lease for $12.
A limitation with a budget VPS is the amount of memory available. The budget VPS we are using in this article has only 64Mbyte of memory, thus limiting the selection of IoT server solutions that can be used. The IoT solution we will use in this article is an application server framework that includes an IoT protocol, a web server, a server side scripting language, and a database in one unit. This software package can easily run on a budget VPS, and in my test environment, I was able to connect 10,000 devices to the online server. I performed the test by using a number of additional Virtual Private Servers, where I ran a number of simulated devices.
A budget VPS will be running 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 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.
Remote Login via SSH
An online VPS enables you to remotely login by using SSH (Secure Shell). If your own computer is running Windows, download the SSH client Putty, and start the executable from any directory. Mac and Windows computers typically include a command line SSH client, thus you can simply type ssh in a command line on Linux and Mac.
When you sign up for a VPS service, such as the budget securedragon VPS (O64), you will receive an email with instructions for how to remotely log into your VPS by using SSH. The instructions include the IP address of your new VPS and the root (admin) password. The following figure shows how to enter the IP address in Putty and how to login as user root in the command line window.
At this point, feel free to try a few Linux commands. Don't worry about corrupting the VPS since a simple button click in the VPS control panel will re-install the Linux operating system.
Installing the IoT Server
Before installing the server software, I recommend re-installing the Linux operating system by using the VPS control panel if you have made any modifications to the Linux operating system running on the VPS.
To install the server software and the example program, copy all of the following and paste the commands into the SSH console window.
source <(wget -q -O- http://makoserver.net/install/brokerX86/install.sh)
The following short video shows the complete installation sequence, from logging into the VPS, installing the software, and verifying that it works by using a browser.
Downloading and Compiling the Arduino Sketch
The Arduino example has been tested on an Arduino compatible ESP8266 WiFi. However, you should be able to use any Arduino with only minor changes to the startup code.
Download and unpack the Arduino Sketch in any directory https://simplemq.com/downloads/SMQ-Arduino.zip
Open the sketch file SMQ.ino in your Arduino IDE and scroll down until you see:
#error REMOVE THIS LINE AND SET THE TWO VARIABLES BELOW static const char* ssid = ""; static const char* password = "";
Remove the first line above and set variable 'ssid' to your wireless network ID and 'password' to your wireless password.
Open the file led-example.cpp and scroll down until you see:
#define SIMPLEMQ_DOMAIN "http://simplemq.com"
Change the domain name "simplemq.com" to the IP address of your VPS.
Compile and upload the sketch file to your Arduino board. If everything goes as it should, the Arduino device should show up in the web interface. You get to the web interface by using a browser and navigating to your VPS IP address.
The following video shows how to compile the Arduino sketch, how to upload the sketch to the Arduino board, and how to control the board using a web browser.
I recently published an article on IoT security that includes security related to pub/sub protocols, such as the SMQ protocol. I recommend reading this article since some pub/sub protocols can be risky to use in their default state. Although the LED/Light-Controller application used in this article does not use password authentication, it is still fairly secure since the SMQ broker cannot easily be detected and SMQ does not allow wildcard subscription. See the IoT security article for details. As a coding exercise, you may add authentication to the LED/Light-Controller app.