Home | pfodApps/pfodDevices | WebStringTemplates | Java/J2EE | Unix | Torches | Superannuation | | About Us

Forward Logo (image)      

Simple, Secure, Internet Power Switch with Manual Override
IoT Extension Cable Inline Switch, Android controlled by pfodApp
No Android programming required

by Matthew Ford 5th June 2016 (originally posted 2nd December 2015)
© Forward Computing and Control Pty. Ltd. NSW Australia
All rights reserved.


This a simple, inexpensive, customizable Android controlled power switch (110V to 240V). You can build a complete functioning, secure, Internet accessible power switch, all without any programming at all. pfodDesignerV2 generated all the Arduino code and pfodApp provided all the Android code. You can use the free pfodDesignerV2 to customise the screen displayed on pfodApp and generate you own customized sketch.

With a little additional code, you can add a manual override push button and with a few more lines you can add a simple web page configuration that makes it easy to securely configure the power switch on your local WiFi network.

Although the project described here is just an On/Off switch, using pfodDesignerV2 you can turn it into a timed pulse. That is turn the switch On and have it automatically turn off after a set period (1sec to 50days) or turn the switch Off and have it turn on again automatically after a set period. See Pulsed Outputs for Arduino using pfodApp for a tutorial on using pfodDesignerV2 to make pulsed outputs and generate the Arduino code.

Warning – Mains Power can Kill You

This project connects to and controls lethal mains power (110V to 240V). It should only be attempted by experienced constructors. Main Power should ONLY be applied when the enclosure (mounting box) is completely sealed. The hardware has been designed so it can be fully tested without using mains power. Carefully note the safety points detailed below.


The project is constructed using two pre-built boards, a cheap USB supply board (~US4) and an ESP8266 based OLIMEX-EVB (~US13) with an on board relay (prices as at Dec 2015). It is shown here using an Australian extension cable, but can just as easily be fitted to a US or European style extension cable.


The power switch is programmed as a pfodDevice and is controlled by pfodApp on your Android mobile. pfodApp is a general purpose application, the one pfodApp can be used to control all your pfodDevices. No Android programming is required.

The ESP8266 based OLIMEX-EVB board is programmed using the Arduino IDE with the ESP8266 add on. See Code Generator for OLIMEX ESP8266-EVB for how to set up to program the OLIMEX-EVB via the Arduino IDE.

The basic control sketch for this project is completely generated by the free pfodDesignerV2 as detailed in this tutorial, Inline Power Switch, Auto refresh and Blank Space for pfodApp menu. You can use the pfodDesignerV2 to create your own customized menu for this power switch. If you don't want the manual push button override or the web page network configuration, then no Arduino programming is required either. You can still have 128bit security by adding your password to the sketch so you can securely connect via the Internet. See below for how to generate a password and a QR code for it.

However, this project also covers how to add the manual override push button code and the web page configuration to easily connect the power switch to your home WiFi network.

Internet Connection

To connect via the internet you will need to change some settings in your home router. This page Connecting DIY IoT devices to the Internet with pfodApp shows you the changes you will need to make and the supporting software you will need to install.

Hardware Construction

The USB 5V power supply board

The OLIMEX-EVB board needs a 5V supply. A cheap source of a mains powered 5V supply is the Sparkfun USB wall charger (or similar).

The Sparkfun USB Wall charger is held together with just on screw. Remove the screw, prise off the base and slide out the circuit board and remove the power wires.

Looking at the bottom of the board, identify the Mains power connections and the +/- 5V outputs.

Wiring Up the Two Boards

The wiring diagram for the complete switch is shown here is also available as a PDF

The USB supply board is mounted under the OLIMEX-EVB board using Scotch Heavy Duty (holds 2Kg) double sided tap AND a small sheet of nylon to insulate between the two. The two boards are then tie wrapped together to ensure the tape does not come loose. Here is bottom view of the two boards.

SAFETY NOTE: The nylon insulating sheet is held in place by the mounting spaces. The Earth wire of the extension cable is NOT cut. It just goes directly from input to output.

The Neutral power wire is cut and a black neutral wire added to connect to the USB board. The Active wire is cut and incoming connected to the COM relay screw terminal and the outgoing Active connected to the NO screw terminal. A (red) wire runs from the COM terminal to the active pin on USB power board.

One mounting spacer is missing. Its mounting hole is used for a strain relief tie wrap for the Manual Push Button wires.

The Enclosure

The two boards are mounted on the lid of an ABS 80mm x 80mm x 55mm or larger enclosure. E.g. Hammond Manufacturing ABS IP65 enclosure part No RP1065 or similar (~US5.60 from digikey). The momentary IP67 normally open push button is mounted in the body of the box. E.g CW Industries GPB556A05BB (~US4 from digikey). Some push buttons come with built in indicator leds. If you have one of those, you can drive the led via a 270 ohm resistor from the OLIMEX-EVB GPIO5 (pin 12 on the edge connector) to GND to indicate when the relay is being driven.

The OLIMEX-EVB / USB power board are mounted offset in the lid to clear the push button in the base.

SAFETY NOTE: There are no metal parts connecting the outside to the inside of the enclosure. The board mounting screws are nylon and the push button is plastic. DO NOT use a metal push button. As you can see from the photo above the screws holding the lid of enclosure on are external to the internals of the enclosure. Also note the strain relief, using tie wraps, on the incoming and outgoing extension cable which set in tight slots cut in top edge of the base of the enclosure.

That is the construction complete. When you have finish programming and testing using a 5V power pack plugged into the OLIMEX board, you can screw the lid in place and ONLY THEN you can plug the power cable in.

Software Programming

SAFETY NOTE: DO NOT plug the power cable in unless the lid of the enclosure is screwed down. You can do all your programming and testing by powering the OLIMEX-EVB from a 5V power pack.

As described on Code Generator for OLIMEX ESP8266-EVB, to code the OLIMEX-EVB board you need a 5V power supply to center-positive 2.1mm dia barrel connector of at least 1A capacity and a USB to Serial 3.3V output cable. I used OLIMEX's USB Serial Cable F

Programming the Initial Sketch

The initial sketch will be the one generated by pfodDesignerV2 as described in this tutorial, Inline Power Switch, Auto refresh and Blank Space for pfodApp menu. That sketch is InlinePowerSwitch.ino You can use pfodDesignerV2 to customise the screen displayed on pfodApp and generate you own sketch.

Before programming the InlinePowerSwitch.ino sketch you will need to edit it to add your network's SSID and password in place of
#define WLAN_SSID "myNetwork" // cannot be longer than 32 characters!
#define WLAN_PASS "myPassword"

Also add the line
#define DEBUG
below the #define WLAN_PASS line. This will print out debug information to the Arduino serial monitor (at 115200) as the board starts up.

If you don't also set a staticIP, you will need to check the debug output find your board's IP so you can connect to it.

To program the OLIMEX ESP8266-EVB, follow the steps given on https://github.com/esp8266/arduino under Installing With Boards Manager. When opening the Boards Manager from the Tools → Board menu and select Type Contributed and install the esp8266 platform. This project was compiled using the ESP8266 version 1.6.4-673-g8cd3697. Later versions well be better but may have their own bugs as the platform is evolving rapidly. From the Tools → Board menu select the OLIMEX-MOD-WiFi-ESP8266 board.

To compile the InlinePowerSwitch.ino sketch you will need to install two libraries, pfodParser.zip and pfodESP8266BufferedClient.zip, (V2.34+) from pfod Parser Libraries page

Programming the OLIMEX ESP8266 is as simple as plugging in the USB Serial cable, pressing the white push button on the OLIMEX ESP8266-EVB and, while holding it down, plugging the power cable in. You can release the push button once the board is powered up. From the Tools → Board menu select the Port corresponding to your USB to Serial cable.

Then click the Arduino download button to compile sketch and download it to the OLIMEX ESP8266-EVB. When the download is completed, the board reboots in normal operating mode and you can open the Arduino IDE serial monitor (set to 115200 baud) to see the sketch's start up messages.

Once the board reboots, you can connect to it using pfodApp. Remember DO NOT apply mains power at this stage, just leave the 5V power supply plugging powering the OLIMEX-EVB board.

Having installed pfodApp on your Android mobile, set up a connection for the Power Switch as described in the pfodAppForAndroidGettingStarted.pdf, using OLIMEX's IP and portNo 4989. Then on connecting to the Power Switch, the sketch will send back this menu for you to turn the power relay on and off. You can click anywhere in the Extension Power button area to toggle the power on and off.

That's the first sketch working. If you don't want the manual push button over ride or the web page network configuration, then you can stop here. Except perhaps to add a 128 bit password so you can put the power switch on the Internet securely and accesses it from outside your home.

Adding 128bit Security

Before setting up your router to allow access to the power switch from the Internet, you should protect the connection with a password. pfodApp supports 128bit passwords that provide a secure challenge and response when someone tries to connect and also add a secure hash to each message in each direction to detect any attempt as message replay or message alteration. Full details of this security are described in SipHash Secure Challenge and Response.

This page, Secret Key Generator for Secure Challenge and Response, shows how to use the free java program to generating random 128 bit keys and encode them as ascii characters. The resulting keys can be saved as a QR code for easy scanning into your mobile.

An example secret key is

where the name of the file is the also the secret key.

#define pfodSecurityCode ""
#define pfodSecurityCode "b0Ux9akSiwKkwCtcnjTnpWp"

in the sketch enables the 128bit security. Now pfodApp will not connect unless add the password to the connection. Edit the existing connection in pfodApp and click the Scan QR button to scan the password in from it QR code. For convenience print out the QR code and attach it to your power switch.

Connecting via the Internet

Now that you have added security you can modify your router to allow you to control the power switch via the internet. This page, Connecting DIY IoT devices to the Internet with pfodApp shows you the changes you will need to make and the supporting software you will need to install to provide a Dynamic IP. Once you configured your router and set up your Dynamic IP, you can use your web address instead the local IP to connect to the power switch. When connecting via a web address, pfodApp automatically turns of the mobile's WiFi connection and uses mobile data to go out to the internet and back in via your router. This is necessary as the router prevents you connecting to its Internet address while connected to your local WiFi network.

That is a complete functioning, secure, Internet accessible power switch, all without any programming at all. pfodDesignerV2 generated all the Arduino code and pfodApp provided all the Android code.

If you want to add the manual push button override and web page configuration then the next two sections will provide two more sketches to do that.

Adding the Manual Push Button Override code

The sketch, InlinePowerSwitch_withPushButton.ino, includes the push button code. It needs an additional library DebouncedSwitch.zip from Debouncing Switches in Arduino. At the top of the sketch there is an == extra library and definitions for push button == section. At the bottom of the loop() method under <<< Your other loop() code goes here is the extra code that checks the push button, debounces it and toggles the relay output when the push button is released.

Compile and download this sketch to the OLIMEX-EVB board and then when you connect using pfodApp and press the push button, you will see the display on your mobile update with the current state of the Power Switch. If you don't want web page network configuration, which makes it simple and secure to connect the Power Switch to a new network, then you can stop here.

Adding Web Page Network Configuration

The sketch, InlineWiFiPowerSwitch.ino, has both the manual push button override and the web page network configuration code. In the code section = start of pfodESP8266WebConfig settings = defines the includes and variables need for the web page configuration. In the setup() method, the code = pfodESP8266WebConfig config in Access Point mode = has the code that checks if the push button is pressed when the power is applied.

If push button is pressed when the board starts up, then the unit is put into Access Point mode with an SSID pfodWifiWebConfig . To provide a secure WiFi connection for setting your local network's SSID and password, the pfodWifiWebConfig uses the pfodSecurityCode as a password. That is the QR code you have already attached to the power switch.

The configuration process is as follows:-

Turn off the Power Switch
Hold down the push button while re-applying the power.
Scan the QR code into your phone using a QR scanner. I use QR Droid Private, there is also a free version. Copy the password to the clip board.
Open the WiFi settings on your mobile and find the
pfodWifiWebConfig Access Point.
Press and hold that label to bring up the Modify network menu item. Click on Modify network and paste in the password. (remove the www.pfod.com.au from the end if that gets copied also)
Save and connect to
pfodWifiWebConfig You are now connected to the ESP8266 board.
Open in a web browser

Fill in the web page with the values for your WiFi network and the IP and portNo you want to use for this power switch. Then press Configure to save these value in the ESP8266's EEPROM. The ESP8266 responds with this confirmation web page.

The power cycle the power switch to connect to your local WiFi network ready to be connected to via pfodApp.

During normal power up, when the push button is NOT pressed, the code
pfodESP8266WebConfig::BASIC_STORAGE *storage = webConfig.loadConfigAndJoinNetwork();
server = WiFiServer(storage->portNo);

connects to your local WiFi network using the values stored in EEPROM from the web page configuration.

The Open Office file, LabelForCheapWifiShield.odp, provides a template for printing out a label for your power switch with the SSID and password and web page config URL. Just replace the QR code image with your own.

SAFETY NOTE: Once you have finished testing the push button operation and the web page configuration using a 5V power pack, you can screw the enclosure together and only then apply mains power to the completed unit.


That is the Inline WiFi Power Switch completed with manual push button and web page network configuration and connected to the Internet with 128bit security.

With the web page network configuration you can easily move the power switch to another site and connect it there. If you want a different display on your mobile, you can use the free pfodDesignerV2 to create one and generate the Arduino code for your.

The pfodDesignerV2 also has options to pulse the relay on for a specified time, from 1sec to 50 days. Pulsed Outputs for Arduino using pfodApp tutorial shows you how to do that. Again all the Arduino code is generated for you.

AndroidTM is a trademark of Google Inc. For use of the Arduino name see http://arduino.cc/en/Main/FAQ

pfodDevice™ and pfodApp™ are trade marks of Forward Computing and Control Pty. Ltd.

Forward home page link (image)

Contact Forward Computing and Control by
©Copyright 1996-2015 Forward Computing and Control Pty. Ltd. ACN 003 669 994