Create project Need help?
Show more (5)
ClemensValens

Wi-Fi controlled thermostat / timer / switch [160269 & 160631]

Status: Finished
819
8
votes
February 2, 2018
160269-wifi-thermostat-avatar.jpg

Features

  • 250 VAC, 10 A (2500 VA) relay with normally open (NO) and normally closed (NC) contacts
  • 6 switching presets
  • 7 timers
  • 3 custom (user) programs
  • Manual override
  • User interface on smartphone, tablet or computer
  • Can be connected to an existing Wi-Fi network
  • micro USB connector for 5 V power supply

Switching presets

The firmware features six switching “programs” that cover the most common scenarios and even some uncommon ones. Two programmable temperature levels TLOW and THIGH determine the switching points of these programs.
 
Nr Switch ON Switch OFF
0 T < TLOW T ≥ TLOW
1 T > THIGH T ≤ THIGH
2 T > TLOW and T < THIGH T ≤ TLOW or T ≥ THIGH
3 T < TLOW or T > THIGH T ≥ TLOW and T ≤ THIGH
4 T > THIGH T < TLOW
5 T < TLOW T > THIGH

Custom programs

For the situations that are not covered by the six main presets, three custom programs have been defined. They are empty in the firmware and must be written by the user. To make this exercise as painless as possible, the interface between the webpage and the firmware has already been implemented by reserving three entries in the webpage’s preset list: ‘Custom 1’, ‘Custom 2’ and ‘Custom 3’. Of course, you are free to modify this, but if you keep them, you will not have to write HTML and Javascript and you won’t have to reprogram the ESP-12F’s file system, a time-consuming job. All that must be done is adding a bit of code to the ESP’s main program loop in the Arduino IDE.

Timer options

Each timer can be assigned to a day of the week, and for each timer the start and stop time can be set. For a week this means setting up seven timers. However, if your workweek is from Monday to Friday and your weekends are Saturday and Sunday, and if you can live with one preset for the workweek and one for the weekend, then you only have to set up two timers: one as ‘Workweek’ and the other as ‘Weekend’. For cases where the thermostat has to repeat the same thing every day, a timer can be set to ‘Every day’. Finally, timers can be programmed to fire somewhere in the future with a programmable duration by setting the start and stop date and time with a calendar control (mode 'Once', see table below).

Up to seven timers can be programmed and as they run in parallel, they may overlap. Setting several timers on the same day is an option too, as is programming up to seven future events. All in all, the timers are pretty flexible.

 
Nr Mode
0 Once
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
8 Every day
9 Workday (Mon, Tue, Wed, Thu & Fri)
10 Weekend (Sat & Sun)
 

The circuit

At the heart of the thermostat sits an ESP-12F module (see schematic drawing in download section below). This is the same thing as an ESP-12E module but with pins 17 to 22 (that we do not need). Inside the ESP-12F module lives an ESP8266EX, one of the only chips in the industry that does not have a fancy title (something like Highly Integrated Wi-Fi-Capable System-on-Chip would have been suitable) together with 4 MB serial flash memory.

The ESP module takes care of everything. It reads the four pushbuttons, it controls the three LEDs, it communicates with the temperature sensor and switches the relay on and off.

The power supply is built around voltage regulator IC3. D2 provides reverse polarity protection. The ESP module consumes quite some current when its Wi-Fi interface is busy and this can produce interference on the supply line. R11, C6, C7 form a filter to ensure that especially IC2 sees a relatively clean supply.

K3 is the serial port connector and is compatible with a 3.3 V “FTDI” serial-to-USB converter cable. Note that such cables carry 5 volts on their VCC pin even though their signals have 3.3-volt levels, which is why R12 is not mounted by default. It is available, however, in the case the serial port is to be used with some other circuit that is to be powered from the thermostat’s power supply (or vice versa).

As temperature sensor the popular DS18B20 1-Wire digital thermometer in a TO-92 package was chosen. It can be mounted on the PCB, but it is also possible to connect it with wires for more freedom.

The relay has a standard footprint that can be had from several manufacturers. However, before buying one that fits on the board, make sure that its contacts can handle the load they have to switch. It must have a 5 V coil, and, for switching the specified 2500 VA, it needs contacts rated for a carry (not max) current of at least 10 A @ 250 VAC. Please note that the photos in this article show a previous board version with a 5 A relay which is much smaller.

Because 2500 VA is a lot of power to switch the PCB has 100-mil-wide traces between the relay contacts and connector K2. Together with a standard copper thickness of about 35 µm this is good for some five amps while keeping reasonably cool. Since we want more, these traces have no solder mask and must be fattened with a layer of solder. Not a lot is needed, 0.5 mm is sufficient.

Pushbuttons

Although there are four pushbuttons on the board, only two (S1 & S2) are available to the user. With S1 the thermostat can be switched between manual and thermostat mode. In manual mode the device functions as a simple On/Off switch, useful in e.g. startup or emergency conditions. In this mode the relay state is toggled with S2.
S3 and S4 are used to put the ESP module into programming mode using a secret handshake:
  1. press S4 (Reset)
  2. while pressing S4, press S3 (Flash)
  3. release S4 (Reset)
  4. release S3 (Flash)
If you executed this sequence correctly, the tiny blue LED on the ESP module will flash briefly, signaling that the module can now be programed over its serial port.

LEDs

LED1 indicates the state of the Wi-Fi connection. When it is blinking the thermostat is not connected to anything; when LED1 is continuously on the thermostat is connected. Two possibilities in this case:
  1. the thermostat is itself an access point (AP) and a device (smartphone, computer, etc.) is connected to it
  2. the thermostat is connected to an access point (AP)
 The fancy pushbuttons S1 and S2 have built-in LEDs. When S1LED is on, the thermostat is in manual mode; when the LED is off, it is in thermostat mode. S2LED indicates the state of the relay. When it is lighted, the relay is active (NO contact is closed); when it is off, the relay is released (NC contact is closed).

Enclosure & construction

The board was designed to fit in a cheap ABS plastic enclosure with battery compartment, a 1593Qx from Hammond Manufacturing. This 112 × 66 × 28 mm (4.4 × 2.6 × 1.1”) enclosure is available in grey (x = GY), black (x = BK) and translucent blue (x = TBU). When the temperature sensor is mounted on the PCB it can be pushed over so that its head will hang inside the battery compartment. With the lid removed the air can flow around it. If the sensor is mounted on wires, the battery compartment provides enough space to put the sensor in when the thermostat is not used.

A drilling template is available below together with a front panel inlay design to help you build a nicely finished project. Only three holes have to be drilled.

The inside height of the enclosure is specified as 21.7 mm, the standoffs are 4.5 mm. The relay has a specified height of 15.7 mm. Mounted on a standard 1.6-mm-thick PCB it is 0.1 mm too high. This can be adjusted by shortening the standoffs a little bit or by using a thinner PCB (1 mm for instance). Also, the top shell has four standoffs, one of which hinders the relay and so it must be removed. Luckily this is easily done with a 10 mm drill or so.

After mounting the relay do not forget to apply a 0.5-mm-thick (or more) layer of solder to the traces that run from the relay to K2. It may be necessary to shorten the standoff under the relay to compensate for this extra solder.

Extenders (5 mm and 8 mm) are available for the pushbuttons. With 5 mm extenders the pushbuttons just stick out of the top of the enclosure.

Mounting the LED requires some manual adjustments. To do this properly, first solder its anode pin—easiest to solder/unsolder—and adjust the height until you are satisfied. Then solder the other, grounded, pin.

The enclosure has a removable side panel which has to provide access to the connectors. Because we are dealing with line voltages it is definitely & highly not recommended to simply throw it away. The milling template also has a template for the cutouts needed for this small panel. Yes, it is small, but with a bit of effort it can be done and keeps the thermostat safe.

For the USB opening we found that a slot the size of the tip of the mating connector was enough to provide good contact. If it doesn’t for you, know that micro USB charger cables with extra-long tips (8 mm instead of 5 mm) can be had easily.

Software

The program for the thermostat is written as an Arduino sketch with setup and loop functions. It is quite a big program due to the inclusion of a webserver and Wi-Fi access point, but most of that can be considered black boxes. The thermostat functionality is implemented in the function loop and this is where you can add Custom presets. All pretty straightforward indeed. Everything can be downloaded below. Currently there are two versions, one with the timers and one without.

A note on internet browsers

The presented HTML code (thermostat+timer version) has been verified with the W3C Markup Validation Service and was found free of errors and warnings, except for the date/time pickers that are not supported by every internet browser on the market. It worked fine on our test computer with Google Chrome (version 63.0.3239.84) and Microsoft Edge (version 41.16299.15.0). We have not succeeded in making the UI work in Internet Explorer 11 (11.125.16299.0), even after trying all sorts of solutions found on the internet. We therefore do not recommend this browser. If you have a suggestion to fix this problem, please let us know.

Keep in mind that the thermostat’s UI uses JavaScript and might conflict with your browser’s security settings.

Setting up the software development environment

The thermostat program was written using the Arduino IDE. Because the ESP-12F is not an Arduino, the IDE must be extended with a Boards package to add ESP8266 support. The procedure to do this is quite simple but requires an Internet connection.

It starts from the ‘File’ menu by opening the ‘Preferences’ dialogue of the Arduino IDE. Copy the URL http://arduino.esp8266.com/stable/package_esp8266com_index.json (one line, no spaces, beware of typing errors) into the ‘Additional Boards Manager URLs’ box of the ‘Preferences’ dialogue. Close it when done.

Open the Boards Manager (‘Tools -> Boards’). In the upper left corner of the window that opens select ‘Contributed’. Look for ‘esp8266 by ESP8266 Community’ in the list that appears, click on it and then click the ‘Install’ button. The IDE will download the needed files and copy them to the right location. When terminated, close the window.

Now the ‘NodeMCU 1.0 (ESP-12E Module)’ will be available somewhere in the ‘Boards’ menu, under the header ‘ESP8266 modules’; select it. Set the ‘CPU Frequency’ to ‘80 MHz’ and the ‘Flash Size’ to ‘4M (3M SPIFFS)’. The ‘Upload Speed’ should be set as high as possible to reduce upload times. The maximum achievable speed depends on the serial-to-USB converter used.

Filesystem upload tool

After installing the Boards package a second IDE extension must be installed, needed to load the webpage in the ESP module’s serial flash memory. The procedure is as easy as storing a file in a new folder. When done (restart the IDE if you kept it open during the installation) you should have an entry ‘ESP8266 Sketch Data Upload’ in the IDE’s ‘Tools’ menu.

Additional library

Finally, it is necessary to install a 1-Wire library to allow communication with the DS18B20 sensor. Install it with the IDE’s library manager (‘Sketch->Include Library->Manage Libraries…’).

Flashing the firmware

Connect the thermostat by means of a serial-to-USB converter to the computer. Also connect the thermostat’s micro USB connector to a 5-volt power supply. Download the thermostat/timer sketch and data open it in the Arduino IDE. Do the flash-enable handshake (see above: press S4, then S3, release S4, then S3). Select the correct board and serial port and launch a sketch upload. The sketch should compile & upload without errors & warnings.

The next step is to upload the webpage and everything else that resides in the sketches subfolder ‘data’: again do the flash-enable handshake and then select ‘ESP8266 Sketch Data Upload’ from the IDE’s ‘Tools’. Uploading should start and will take a while (several minutes) because the image has a size of 3 MB.

Using it

Now that the firmware and webpage have been installed on the thermostat, it is time to try it out. The first test is to power the thermostat and try the ‘Manual’ button (S1). Pressing it should toggle its built-in LED. Press it to make its LED light up. Now press the ‘Output’ button (S2) to toggle its built-in LED. This should also toggle the relay, which is audible. While you are doing this, LED1 should blink at a rate of 0.5 Hz.

On to Wi-Fi

Open the Wi-Fi settings on a smartphone, tablet or computer and look for the network ‘Wi-Fi Thermostat’. When using a phone, it may be necessary to switch off mobile data first. Connect to it (no password is needed, LED1 should stop blinking and remain on), then open a browser and navigate to the address 192.168.4.1. The thermostat’s webpage should appear. Wait five seconds to let it receive & update the current temperature. If it does, all is fine and it is possible to change the settings. Click or tap (“clap”) ‘Submit’ to send the new settings to the thermostat. It can take up to five seconds for the webpage to reflect the changes, so please be patient.

Hook it into an existing network

One section of the webpage lets you configure the thermostat for an existing Wi-Fi network. Fill in its SSID and passphrase and clap the corresponding ‘Submit’ button. You must then clap the ‘Restart’ button to make the changes become effective. Note that the first restart using this button after programing the ESP module will result in a crash. This is not our fault but a known bug somewhere inside the ESP module or libraries. If this happens, simply power cycle the device and everything will be fine.

If, after a restart, LED1 lights up continuously, the thermostat has connected successfully to the network (to be sure, first disconnect all devices that may have reconnected to the thermostat’s AP). To find out what the thermostat’s IP address is, connect to its AP. The webpage will display it. You can also check your network’s AP’s connected devices list.

Forget

To remove the thermostat from the network, clap the ‘Forget’ button. This will erase the persistent memory of the device where its settings are stored. All its settings will be reset to their factory default values. Clap the ‘Restart’ button to restart the thermostat.

Where to go from here?

Although this article presented a thermostat, nothing prevents you from making something else out of it. What we have here is basically just a Wi-Fi-controlled relay applied to temperature control. It is, of course, only a matter of replacing the temperature sensor by some other sensor to transform the thermostat into something completely different. The software is easy to modify—all the hard parts have been pre-mashed—and can be adapted to lots of other applications. Let your imagination run free, to throw in a cliché, and use this design as the starting point of a new project.

Read the full post
Show less
Products
Images
Wi-Fi controlled switch/thermostat/timer
Assembled PCB.
Wi-Fi controlled switch/thermostat/timer
DS18B20 temperature sensor
ESP8266 boards package
ESP-12E selection in Arduino IDE
Schematics
Software
CAD
Other

Loading comments...