Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

An Arduino ESP8266 Light Controller (In Time for the Holidays)

DZone's Guide to

An Arduino ESP8266 Light Controller (In Time for the Holidays)

Learn how to make your own light controller, including security considerations, as the holiday season draws near.

· IoT Zone
Free Resource

Address your IoT software testing needs – improve quality, security, safety, and compliance across the development lifecycle.

The Holiday Light Controller is a fun project that lets you provide public access to your outdoor lights during the holiday season. Although the ESP8266 Arduino source code and the server source code can be used "as is," the code is designed for educational purposes and comes with an important message: security. We will first go into how to install the solution, then we will briefly explain how the software works before looking into IoT security.

ESP8266 IoT Light Controller

The server solution requires that you use at least one domain name. You can sign up for free domain names using, for example, freenom. A suggestion is to select a name such as your last name or your street address. You could then create a sign and display this sign on your front yard. The sign could for example say:

Control My Lights!
Use your Smartphone and navigate to:
http://mydomainname

ESP8266 Wi-Fi Four Relay Board

The device firmware is designed for NodeMCU ESP8266, and in particular for the ESP8266 Wi-Fi Four Relay board. You can use any Arduino ESP8266 board, but that will require modifications for the GPIO settings in the firmware source code. The benefit in using the ESP8266 WiFi Four Relay board is that it comes ready to use with four relays and a power supply. Other ESP8266 boards may require that you also assemble your own I/O hardware and relay banks.

The ESP8266 WiFi Four Relay board, which supports both U.S. and European voltage, is connected directly to your household electric power (mains). The actual lights must be wired to the ESP8266 WiFi Four Relay board as shown in the following figure:

ESP8266 WiFi Four Relay Board

Installing the Arduino ESP8266 Firmware

Download the Light Controller Software, unpack the ZIP file and open the Sketch in the Arduino IDE. Compile the code and upload the code to the ESP8266 board.

If you are new to the Arduino IDE and to the ESP8266 Arduino IDE plugin, follow one of the getting started guides such as this one or watch an ESP8266 video on YouTube.

Note that one ESP8266 is referred to as a light bank, and the ESP8266 WiFi Four Relay board provides a bank of four lights. You can connect any number of light banks to the online server. The server will then merge the light banks into one coherent user interface, enabling you to control any number of lights.

Installing the Light Controller Server Solution

In a previous article, I walked the reader through the process of signing up for a budget Virtual Private Server and how to access the server using SSH. I recommend that you read this article before continuing. Read the article Setting Up Your Own Arduino IoT Cloud Server up until the point of installing the server software, but do not install the server software in this article.

When you have logged into your VPS using SSH, copy and paste the following commands into the SSH command window:

 source <(wget -q -O- makoserver.net/apps/LightController/installX86.sh) 

The above script will ask you to enter a name for an administrator and the password. When the server is installed, the administrator credentials will enable you to log into the web based administrator user interface. Use your browser and navigate to the IP address of the online server as soon as the installation script completes.

Note that you could also install the server solution on a computer on your own private network, but that will exclude public access to the server. Using an online server enables visitors to use their browser on their mobile phones to control your lights. The visitors do not have access to your WiFi network, but they can use their own cellular network and control your lights via the public online server. The ESP8266 light controllers are also connected to the online server via your WiFi network. The online server acts as a proxy, routing messages between the connected browsers and the ESP8266s connected to the online server. In other words, the online server enables communication between disparate networks.

Light Controller Manual

The Manual, which explains how to use the online web interface and how to initially configure the ESP8266 using a browser, can be downloaded as a PDF. The manual is also integrated into the online web interface and can be accessed after installing the Light Controller server solution.

How the Software Works

The Arduino device code is designed to initially act as an Access Point, where you can configure the settings required for the device to connect to the online server. The web-server used is the standard web-server that comes with the ESP8266 Arduino environment.

After configuring the device, the device restarts, configures the WiFi for Station Mode, connects to your WiFi, and then connects to the online server.

The device connects to the online server by using a pub/sub protocol called SMQ. We introduced this protocol in the Setting Up Your Own Arduino IoT Cloud Server article. In this article, we used a JavaScript powered web application that could connect to ESP8266 devices by using an online SMQ broker. However, this application did not include any specific logic on the server side besides providing the SMQ broker functionality. The Light Controller server application behaves differently from the LED example in that the Light Controller includes an application on the server side designed specifically for controlling the lights. The following figure shows the setup:

Arduino ESP8266 IoT Light Controller Server

The Light Controller server application (shown as color red above) is in charge of controlling most of the Light Controller solution's logic.

The server solution is designed to operate on limited budget Virtual Private Servers (VPSs), however, even a budget VPS is totally overkill for controlling the lights for only one user. A budget VPS with say 64Mbytes of memory is sufficiently powerful to control hundreds of homes.

The server solution is therefore designed such that it enables multiple homes to be registered in the administrator web interface. For each domain name (the home) added by using the online administrator web interface, a new SMQ broker is created. A unique SMQ broker instance per home completely separates the SMQ message flow for each home. We can address multiple brokers on the same server/IP-address by using the domain name to uniquely identify each broker. This is possible since the SMQ pub/sub protocol initially starts as HTTP. The protocol then upgrades to a persistent real-time connection similar to how WebSockets are upgraded to a persistent connection.

You can download and study the server code LightController.zip. The server code is implemented in the Lua scripting language. The code can also be viewed by navigating to your online Light Controller server by using the address, http://VPS-ip-address/doc/. On the documentation page, navigate to the developer information and view each file by using the built in web-based source code viewer.

Security

The web based Light Controller App is powered by JavaScript and enables the user of the web interface to control the lights in real time by sending one-to-one and pub/sub SMQ messages to the light controller devices via the online server.

A problem with many pub/sub protocols is that they typically cannot be used without authentication since this would leave the door completely open, especially for pub/sub protocols that enable wildcard subscription.

However, the web-based Light Controller App would not be very user-friendly if visitors were forced to register and login prior to being able to control the lights. We wanted the app to be user friendly; thus the Light Controller solution was designed to not enforce authentication for the SMQ protocol.

You are probably at this point thinking, hey wait minute, the Light Controller App must be totally insecure. However, that is not the case, since the SMQ protocol enables us to enforce strict authorization.

The Light Controller solution uses two lines of defense: (1) working in stealth mode, and (2) strict authorization. Authentication would have added one additional line of defense, however, the solution is designed to be sufficiently secure without having to use authentication. As an introduction to designing solutions with a multilayered defense system, I recommend reading my article: Have We Forgotten the Ancient Lessons About Building Defense Systems?

Hackers first need to identify a solution before they can attempt to exploit it. The ESP8266 light controller devices operate as network clients (TCP/IP clients), thus they cannot be identified nor can they be directly compromised. Although the SMQ brokers operate as servers and can be detected, it is unlikely since an automated port scanner cannot easily see the difference between a web server and the SMQ broker. In addition, the SMQ broker cannot be found by going directly to the IP address of the server. The broker can only be found by using one of the registered domain names.

Should a hacker still find the broker, the second line of defense kicks in. The Light Controller Server App is designed to enforce strict authorization, where an attacker will be identified and temporarily banned. The exact authorization logic implemented for the Light Controller Server App is best understood by studying the Lua source code. Download the source code as explained in the software section above. Navigate to the Lua source code file ".preload" and scroll down to the security section.

Accelerate the delivery of high-quality software in the connected IoT era through an integrated analysis, testing, security, and analytics platform

Topics:
security ,web app ,iot ,arduino

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}