Want your IoT project to run in the cloud? No problem, this tutorial will walk you through creating and starting your own virtual server to support your IoT project.

If you’ve used some of the new Arduino-enabled hardware platforms for development, chances are you’ve already used online IoT services in your projects. In this article, I demonstrate how to service your Arduino IoT project by setting up a virtual private server (VPS) and configuring some software.

In addition to setting up your VPS, I’ll show you how to install some simple examples to control leds and lamps through the browser. The following illustration shows how to redirect to your own VPS via the browser and achieve real-time control over different devices. Here the online VPS acts as a proxy, allowing different users to control their respective devices through their browsers.


Using an online server as a proxy for communication between users and devices has the following benefits:

  • Users can control devices across networks. For example, you can use your phone’s mobile network to control devices connected to your home network.
  • The device runs as a (TCP/IP) network client and can be controlled directly from a browser, without the need for other tricks like port forwarding.
  • The hidden operation of devices as network clients significantly improves device security. We’ll talk more about security later.
  • Small devices often do not have the resources to provide a Web interface directly. An online server can take on the responsibility of providing a Web interface to the device, which can significantly reduce code complexity on the device.

Virtual private server

Setting up a VPS on your own sounds complicated and expensive, but in reality it is very simple and can be very inexpensive, and it is possible to find VPS services that cost as little as $8 a year. I like to budget the cost of VPS I use through Lowendbox.com. Securedragon is one of my favorite VPS providers and costs less than $6 for three months and less than $12 for a year.

Memory limit

An important constraint in the VPS budget is the amount of memory available. The VPS I used in this article has only 64 Mbytes of memory, which greatly limits the IoT solutions available to our server. The IoT solution we use in this article is an application server framework that includes the IoT protocol, Web server, server-side scripting language, and database. Packages can easily run on budget VPS, and in my test environment, online servers can connect to up to 10,000 devices. I did this using some extra VPS to simulate a large number of devices.

Linux

The VPS in my budget runs Linux. If you’ve never used Linux, you might think this article isn’t for you, but how do you know until you try? For starters on the Linux command line, online VPS is the best place to start, because no matter what you do wrong on the VPS, you can restore your VPS to its original configuration with a single button on the Web control interface provided by the VPS provider. Therefore, the online VPS is the best place to practice the Linux command line. If your VPS goes into an unrecoverable state, you can reinstall the system by logging into the VPS Web control panel. A minute or two later a brand new Linux VPS will be regenerated and you can start again on it.

Remote login is implemented over SSH

You can use Secure Shell (SSH) to remotely log in to online VPS. If you’re running Windows, you can download Putty, install it, and run it. Mac and Linux usually have SSH command-line tools built in, so you can connect via SSH commands.

When you sign up for a VPS, you will usually receive an email with instructions about your VPS, including instructions on how to use SSH to log in to the server and your new VPS administrator password (and possibly the private key needed for access). The following figure shows how to enter an IP address in Putty and log in from the command line as user root.


Now feel free to try some Linux commands. Don’t worry about damaging the VPS, because even if you do, you can reinstall your Linux system from the control panel on the Web.

Installing the IoT Server

Before installing the server software, if you have made changes to Linux on your VPS, I recommend reinstalling the system through the control panel.

To install the software and instance programs required by the server, copy and paste the following commands into the SSH command line window.

source <(wget -q -O- http://makoserver.net/install/brokerX86/install.sh)Copy the code

Download and compile the Arduino Sketch

Arduino examples can be tested on an Arduino board compatible with ESP8266 WiFi. With a few modifications to the original code, it can run on any Arduino.

Download and unzip the Arduino Sketch provided at the link below:

Realtimelogic.com/downloads/S…

Open the Sketch file smq.ino in the Arduino IDE and scroll down until you see the following code snippet:

#error REMOVE THIS LINE AND SET THE TWO VARIABLES BELOW
static const char* ssid = "";
static const char* password = "";Copy the code

Change the SSID variable value in the first line to the ID of the wireless network to be connected to the device, and change the password value to the password of the wireless network.

Open the file led-example. CPP and scroll down until you see the following:

#define SIMPLEMQ_DOMAIN "http://simplemq.com"Copy the code

Change the domain name simplemq.com to the IP address of your VPS.

Then compile and upload your Sketch file to your Arduino board. If all goes well, the Arduino device will show up on the Web interface. You can access the Web interface by typing the IP address of the VPS in the browser.

That’s all you need to configure an online server for real-time access to your Arduino device through a browser. The complete application can control on/off components such as LEDS, lights, and motors. The complete application consists of two parts :(1) the JavaScript script that drives the application in your browser, which establishes a connection to the online server. (2) Arduino code running on the Arduino development board, which will also establish a connection with the online server. The online server functions as a generic proxy and is responsible for routing messages between the device and the browser client.

The IoT protocol used in the application, called SMQ, provides one-to-one communication in addition to the publish/ Subscribe mode. The PUB/SUB protocol in SMQ is similar to other PUB/SUB protocols (such as MQTT and AMQP). When using the PUB/SUB protocol, an online server is often referred to as a proxy. The broker is responsible for routing messages between publishers and subscribers. A detailed description of the JavaScript code and Arduino code in the application can be viewed here.

IoT Security

I recently wrote an article about protocol security (such as SMQ) in IoT. I recommend you read this article, as you should be aware that some of the pub/ SUB protocol defaults are security risks. Although the LED/ light control application used in this article does not use password encryption, it is still quite secure because the SMQ proxy is not easily detected and the SMQ protocol does not allow wildcard subscriptions. Refer to the article mentioned above for details. As an exercise, you can also add validation to the application in this article.