Home Automation with ESP8266 Part II - how to control the wireless switch

In the previous post, I've described how to build a wireless relay switch based on an ESP8266. As this included a lot of testing, I just like to share a bit of the experience I've made during doing this.

For the examples given below lets assume that the IP of the WLAN switch is 192.168.0.75

With the scriptings which can be downloaded in the previous post, the ESP8266 will deliver a website with an ON and OFF button, when its IP address is typed as URL into a web browser.

Beside switching it manually on and off in the web browser, it is also possible to do that directly from e.g. a bash script (I can only talk about linux here - similar commands might exist for Windows or other operating systems).

Switching from a script or command line

To switch the relay on from a bash script or command line the following command is needed:

wget http://192.168.0.75/?pin=ON1 -q

and to switch off again:

wget http://192.168.0.75/?pin=OFF1 -q

While using a web browser I've noticed that the switching took always a certain time between clicking the button until the relay was actually reacting. I assume that the time is caused either by the browser or the parsing of the http-protocoll with string operations on the ESP8266.

To speed up switching, the scripts also support a mode for sending a direct plain command via TCP without all the http overhead. In this case the relay switches immediately.

The supported commands are: "ch1on", "ch1off" and "ch1?"
The first 2 commands are confirmed with an OK sent back by the ESP8266. The third command will be replied either with "ON" or "OFF", depending if the relay is switched on or off.

The commands can be send to the ESP8266 like this:

echo -n "ch1on" | netcat 192.168.0.75 80 -w 10

The first part is piping the command without line feed (-n) to netcat. Netcat is opening a tcp connection to the IP and port (80) given and waits for max 10 seconds. The results is printed on the command line.

Switching using an iPhone

As I've built in the past also other devices which can be controlled based on IP communications I've tested a few iPhone apps for remote controlling IP devices. The only app which remained after several tries is currently "Mote" from worriedcat.com.
It's not the cheapest thing, but the only one with a build-in GUI editor which worked for me and support for all kinds of protocols. 

To configure it, I first created a controller in Mote defining the IP address, port and the protocol which is needed:

As next step I've added a device where we can define, which controller should be used.
(I've used for the controller and the device the name Test Switch, which might be a bit confusing)

Tapping on the created device allows then for designing buttons on a panel:

For each button I've created a command, using the direct TCP commands mentioned above:

In the command as device the device is displayed which we created in the first place. So the app knows to what address and port and by what protocol this command is to be send when the button ON-button is pressed, as the device again contains the controller which is to be used.

For the "Status"-button the command need to be configured with the value "text" for the attribute display. (Not for ON and OFF, as we don't want to have a pop-up showing OK all the time.) Then the Mote app will show the reply of the command sent to the ESP8266:

So in the end the app shows a screen like this one with three buttons, sending three different commands and display the device answer in case the status is requested:

Any other ideas or suggestions how to controll the ESP switch? Any questions, comments?
Feel free to ask here. 
:-)

Comments