Home Automation with ESP8266 Part I - how to build a wireless switch




It has been only recently that I've got to know about this integrated WLAN module ESP8266.
Not quite sure, why this didn't appear earlier on my radar, as it is a quite powerful and very cheap basis for everything you might want to do with a microcontroller which is connected via WLAN.

If you google it, you will find a lot of information about it, including alternative firmware projects
and a variety of different code examples how to program that tiny thing. So this project here is about combining and enhancing this code examples to a fully working remote, relay based switch.
I decided for a relay to be able to switch on and off all kind of loads like inductive things as well as LED bulbs. (LED bulbs sometimes will not turn off correctly if switched electronically by a TRIAC)

As an aside: another important prerequisite for a "real-life-applicable" home automation switch is a really small power supply. I've experimented quite a lot with different approaches including a capacitor based power supply - but it turned out that as soon as the sizing is sufficient enough to source the ESP8266 module plus a relay, the components are becoming so big that a small switching power supply is still far less space consuming than most other solutions.

To begin with, I've got the ESP8266 as well as the power supply from here:
Banggood ESP8266
Banggood Power Supply
It is pricewise quite good I think, but you need some patience until you get it delivered, depending on where you are located.


Flashing with alternative firmware - nodemcu


I've flashed the ESP8266 with the alternative nodemcu firmware, which allows to run own scripts (lua) on the chip including a simple file system. The original firmware supports only running the ESP8266 with at-like commands, so you would always need a second microcontroller to do something meaningful with it.

The nodemcu firmware can be found here.
I didn't want to build the firmware myself, so I have used this webpage: nodemcu-custom-build
It is a great help and make things very easy compared to install the whole tool-chain for this platform to compile.

The next step is to flash the device with the nodemcu firmware.
For that I've used this howto: flashing-the-nodemcu-firmware-on-the-esp8266-linux-guide
As the ESP8266 is running on 3.3V, also the serial ports are working in the same voltage range, which means direct connection to the RS232 interface of a normal computer would destroy the ESP8266.
For flashing the easiest way turned out to be to connect it to a Raspberry PI: it's a linux platform and it provides 3.3V supply as well as the serial signals with the right voltage.
Connections of the GPIO of Raspberry I found here.
Connector description of all versions of ESP8266 I found here.

As I saw many different information how the ESP8266 gets into flash mode, at startup
CH_PD, RST, GPIO2 needs HIGH level (connect with 3.3V),
GPIO0 needs LOW level (connect with ground). This switches the ESP8266 into flashing mode when powered on.

For a normal start of the ESP8266 the inputs need to be like:
CH_PD, RST, GPIO0, GPIO2 all on HIGH level. After ESP8266 started then GPIO0 and GPIO2 can be used as normal I/O.

How the lua script for the switch works and how to upload it


For uploading the lua scripts onto the ESP8266 I've used the ESPlorer, which can be download here.

The scripts I've put together (download link further down) are based on this one for setting up the WLAN and this one for the web server. I have combined the examples and did several changes and extensions to

  • make it more stable, 
  • to show the IP address after linked to the WLAN while configure the switch in a browser,
  • to support additional direct (and faster) commands via tcp than http only,
  • to have a working re-connect in case WLAN gets lost,
  • cope with a few issues when switching to access point mode and/or back to client only and
  • support a push button for switching on/off the relay locally parallel to switching it via WLAN.

This is how the scripts are working:

  1. init.lua gets started automatically after startup of the ESP8266. The relay (light) will be turned on for 3 seconds. If the push button is pressed until the relay goes off, the script connect.lua will be started and the ESP8266 will become an access point
    (ssid: "myhotspot" pw: "12345678")
  2. After connecting to this WLAN with e.g. a notebook the ESP8266 will be reachable on 192.168.4.1 in the web browser and will show a dialog to configure the WLAN access.
    If credentials are typed in and submitted, a "check-ip"-button gets visible, which will display the IP the ESP8266 gets from the newly configured WLAN. (it is possibly necessary to press the button a few times until ESP is done with dhcp and got its new IP)
    The connection details will be saved in the file "connected" on the ESP8266.
  3. After switching the ESP8266 off and on it will again either checking if the button is pressed (see 1.) if not it will check if a file with the connection details of the WLAN exists. If so, it will start the script connected.lua (otherwise starting the connect.lua again).
  4. The script connected.lua runs a web-server and also reacts to direct commands sent via TCP on port 80. (which works a bit quicker, as it saves processing the http overhead of a webpage)
    The supported commands are: "ch1on" "ch1off" "ch1?" (without linefeed or CR!)
    Beside that the relay can be switched on and off by the push button. (S1 in the circuit layout)

    Additionally the connected.lua script checks regularly if the WLAN connections works and reconnects if necessary and pings the default gateway every minute to keep the WLAN connection alive. (I'm not sure, but it seems that if the WLAN connection is not used for awhile, it is necessary to do whatever kind of handshake between ESP8266 and the WLAN router which causes a longer reply time for a request after a couple of hours. A regular ping solved that.)
All the lua scripts mentioned above can be downloaded here: ESP8266-wlan-switch

It is important to understand that the ssid and the password of the WLAN are saved in clear text on the ESP8266. That means that someone who gets physical access to the ESP8266 and knows about RS232/serial communication could potentially read it out of the hardware.


How to connect a relay and a push button to the ESP8266


As the GPIO0 and GPIO2 need to be on high level during ESP8266 start-up (otherwise it will go into flash mode) the relay connection was slightly different than usual. Mainly because I only had NPN transistors at hand...

With the voltage divider made out of 2 resistors on GPIO0 it gets slightly above 2volts when being configured as input, which is enough to be recognized as logical 1. After the scripts starts to run and GPIO0 becomes an output port it is still possible to switch the relay on and off via the transistor.

Soldering it and squeeze everything into a small box


The few additional components like the 3.3v regulator, transistor, resistors and relay I've soldered on a very small piece of pcb.


On the backside it got a bit tight as well. But there was not enough space for the diode on the component side. The thin cables are for power, the button and an additional LED in parallel to the relay. The thick wires connecting to the contacts of the relay.

Theoretically it would have been possible to make it even more compact (by turning the connector of the ESP8266 the other way around, so the module is above the relay), but I wanted the print antenna of it not to be covered by the relay or the powersupply.

Here you see how it operates if switched by the webserver buttons. There is a small delay between the mouse click and the switching sound of the relay. This doesn't happen if remote controlled by the direct TCP commands, as mentioned above.

Any questions or improvement ideas - feel free to comment. :-)


Comments

  1. Impressive work .. :)) I am sure these will make way to my home automation setup replacing expensive zwave ... How many relay can we control with single esp8266 ?

    ReplyDelete
    Replies
    1. Depends on the version. With ESP8266-01, like the one used here, you have 2 IOs, so 2 relays if used as direct IO-port. But with the ESP8266-12 at least 8 I would think. If that is not enough one could connect a shift register as port expander.

      Delete
  2. Hello. Nice work. Unfortunately I'm unable to download the ESP8266-wlan-switch files.

    ReplyDelete
    Replies
    1. I've just tested it, the link seems to work. :-) (the one above the picture with the circuit)
      It's a google drive. Do you get any error message?

      Delete
  3. Just what I've been looking for but too lazy to put all the hours into it :).
    Did you try to use a AQH2223 SSR (0.5$ on Aliexpress or similar) to switch a LED bulb ( I know you mentioned it could be problematic but it would reduce the size, maybe fit all in a 5v usb power supply? Total cost would come out ~6$ (5V PS - 1.5$, SSR 0.5$, ESP 3$, push button with LED 1$) + blank switch face plate.
    Next project: LED dimmer??? :)

    ReplyDelete
    Replies
    1. No, didn't try it with SSR yet, mainly due to reasons you've mentioned. For simple on/off I don't want to have the issue that I always need to consider what kind of load I'm switching.
      From cost perspective it would be slightly cheaper than relay, indeed. But the relay you can find sometimes for eg 0.90 USD, so it is not such a big impact. But that is not all in the end: you also need to add up enclosure, the board, resistors, voltage regulator, proper power supply, cabling and terminals to connect whatever load you want to drive with your device.
      Dimmer is something I'll probably have a look at later on as well. Currently I've designed a small pcb for the simple on/off switch.

      Delete
  4. Getting lua: not enough memory on a ESP8266-01, any ideas?

    ReplyDelete
    Replies
    1. Difficult to say, as I could only guess, when you are getting this error message. Before/after uploading scripts? During running them? ...

      Delete
  5. Hello there,

    Thank you very much for your work and for sharing it.

    I would like to modify your design because instead of permanently switching loads on or off, I need the ESP to simulate pressing a momentary switch (a membrane type button which needs to be pressed and released in order to activate a heating system in my home).

    Can you suggest me a way of doing this with your design?


    Thanks

    Milos

    ReplyDelete

Post a Comment