In the first and second parts of this series we set up the hardware with the ESPEasy software. Now the hardware is set up, it is time to work out how to and exercise control over the I/O that will turn the AC power on and off.
As it turns out, ESPEasy makes this a simple process.
Starting a device really straightforward – just plug the Sonoff units into a powerpoint and they run! This was the reason for using these units in the first place (see this previous blog).
Configuring the WiFi switch
There are many references that explain how to configure ESPEasy, so I won’t repeat these here except for what is needed to do the testing described. For reference the example configuration below is for an S20 unit, but the method is extensible to other units with appropriate changes to GPIO addresses.
When the web browser is connected to the ESPEasy unit, the home page will display a menu similar to the one shown below. I would encourage you to look through each menu to discover the huge amount of functionality available.
To start this simple configuration, the first thing to do is select the Tools menu, select the Advanced option and Enable ‘Rules’. This will create an additional top level menu item that allows us to later define macro-like code to execute. A good introduction to Rules in ESPEasy is this video here.
The Advanced menu also lets you set the NTP server for time synchronisation, which is useful for time of day events. Save the changes by selecting the Submit button at the bottom of the screen.
The next thing is to select the Hardware menu. This is used to define specific hardware pins. Useful for this basic config is to set up the on-board LED as a WiFi status indicator by telling the ESPEasy software the GPIO to which the LED is connected. Once again, save the changes by selecting the Submit button at the bottom of the screen.
Operating the Relay
The S20 is now ready to do something (in a very crude manner).
In the Tools menu there is a Command edit box where ESPEasy commands can be entered, to be executed by the device. Commands are a feature of ESPEasy that enable control methods to be implemented. A list of commands can be accessed from the help ‘?’ icon next to the Submit button.
In the Command edit box, enter the command
and press Submit. The relay should click ON and the LED in the S20 will show it is engaged. The GPIO address to use is found in the Sonoff documentation or by internet search. I have listed the different addresses in the previous parts of this series.
Use “gpio,12,0” as the next command to turn the relay off. The web browser will also show the response sent by the ESPEasy software in the text box below the command.
If you look carefully, you will see that the command is passed to the device through the url as a parameter, following “?cmd=” something like this
where the %2C represents the ‘,’ character in the url. So making the ESPEasy do something is just a matter of inserting the appropriate command in the URL string. Nice!
However, the pushbutton currently does nothing and it would be useful if it acted like a local manual switch for the device.
A bit more sophistication
The next thing to configure is in the Devices menu. Here we are going to define two objects that correspond to the manual switch and the relay output.
Start by clicking the Edit button on the Task #1 row, select select ‘Switch Input – Switch’ from the list of known devices to see the the rest of the menu that looks like the figure below. The Name is arbitrary and is used to identify the device – ss this is the definition for the Pushbutton, this name makes sense. Similarly, this item only has one property and we have called it ‘State’.
Similarly configure the second device for the Relay output as follows. We won’t be using this Device in these examples, but it makes the definition complete:
Making the manual pushbutton ‘switch’
Next we have to define some rules to activate the relay when the pushbutton is pressed. These local control rules are implemented in a macro type language entered under the Rules menu we activated earlier. Enter the rules shown below:
Note the syntax is very ‘object-like’ and uses the names we defined for the device. Basically, if the Pushbutton transitions to state 1, we turn on the relay using the command we tested earlier, and if 0 we turn it off. Because we specified the switch as a pushbutton, ESPEasy takes care of keeping track of the switch’s toggle status, and the Rules are called for the right transitions.
Even more sophistication
The next step is to eliminate the need to specify the address on the command line so that other devices remain unconcerned with local details.
We can do this by using a modified set of rules that exploit the ‘event’ command by adding ‘On’ and ‘Off’ events to Hide the GPIO addressing details. The Pushbutton events are changed to reference this same event, and we have added an additional System event that initialises the GPIO to Off when the device boots up. The changed scripts are shown below:
The addition of the the ‘On’ and ‘Off’ events allows the ESPEasy command execution changed to “event,on” and “event,off”, effectively hiding all local details. This creates a consistent system-wide interface for all connected devices.
This configuration now sets up a nice interface to a home automation system, which is what we will be exploring in the next part of this series.