Sonoff R2 Enclosure & ESPHome
The Sonoff Basic R2 is a great switch for home automation. In this Instructable I will show you how to configure it for use with ESPHome and then print a custom case so that it looks a little nicer in your living/bedroom environments.
Besause is is not possible to upload FreeCAD files to the Instructable website, I have create a DropBox share here. This is only necessary if you wish to modify my design. Just one disclaimer, I am not an expert at FreeCAD and so I am sure there are much better ways that this design could have been created :)
Supplies
Parts
- Sonoff Basic R2
- FTDI
- Flat head, countersunk screws x4 (choose colour to match printed case)
- Female header pins (4P)
- Female - Male dupont jumbers x4
- Small cables ties x2
- 3D Printer (I used a Tevo Tornado)
Please note, none of these links are affiliate links. I simply share them for convienence.
Software
- HomeAssistant (The Instructable assumes that you already have HomeAssistant up and running. If you have not, here is a tutorial.)
- ESPHome addon installed in HomeAssistant. The above HomeAssistant tutorial also included setting up ESPHome.
- FreeCAD (If you wish to modify the drawings)
Prepairing the Snoff
While not necessarily essential, add the header makes things much easier and does not get in the way of the casing so it is worth doing. See attached photo for placement.
Using the the dupont jumbers, connect the Sonoff to the FTDI as follows:
- VCC - VCC
- GND - GND
- TX - RX
- RX - TX
Plug the FTDI into an USB port. This should result in another COM port appearing. While there are a number of ways to check what the new port number is, I find the easiest and quickest is, before plugging the FTDI in, to simply run the Arduino IDE and check what ports appear. Then plug the FTDI in and go and see what new port has appeared.
If no new port shows up then you will need to google as to how to install your FTDI driver.
From there, go to the ESPHome Dashboard in HomeAssistant and add a new device. The below is the code that I used:
esphome:
name: smartplug-rumpus
esp8266:
board: sonoff_basic
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: !secret ekey-sp-rumpus
ota:
password: !secret ota_password-sp-rumpus
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Device Specific Config
binary_sensor:
- platform: gpio
id: push_button
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
internal: true
on_press:
# Prevents unintended LED lit states.
if:
condition:
- switch.is_off: relay
then:
- switch.turn_on: blue_led
- switch.turn_on: relay
else:
- switch.turn_off: relay
switch:
# The relay switches on the red side of the LED when active.
- platform: gpio
name: "smartplug-rumpus"
pin: GPIO12
id: relay
on_turn_off:
if:
condition:
- switch.is_on: blue_led
then:
- switch.turn_off: blue_led
# With this we can control the blue side of the LED.
- platform: gpio
id: blue_led
pin:
number: GPIO13
inverted: True
The first install will need to be done via USB. This can usually be done via the Home Assistant, but needs to be using Chrome or Edge. In my case I had issues trying to use an old (already programmed) device so was forced to manually download the firmware and then flash it using the ESPHome Flasher. When doing so, I needed to hold the button down on the Sonoff until the flashing started. Going forward, once complete, I could update the Sonoff wireless.
The Sonoff can now be used with your HomeAssistant to automate switching a device on or off.
Printing the Enclosure
Putting It All Together
Remove the Sonoff from it's original white case that it comes in and install it in the 3D printed case.
The case fits together very tightly, with not much room to attach the electrical cables. These will need to be cut as short as possible and then pushed firmly into the case (only thin cable ties will fit). If the cable is too think, you may also need to drill the hole through which the cables pass, out a little.
As such, once wired up and with everything in place, small cable ties need to be tightly attached (as in the photo) to help prevent the cord from being pulled out of the socket.
Two things to be careful about:
- By referring to the original casing, make sure that you wire the power to the input pins and the light to the output pins. As the PCB does not appear to be labelled, you will need to double check by referring to how the PCB sat in the original casing.
- Be careful not to damage the push button when installing the board i.e. ensure that it lines up with the hole before you pushing everything together. In my haste, I did not do this and managed to break part of if off of the board.
Remember that you will be working with high voltages so ensure that they cables are both secure and not going to be cut by the case (or wear through at the entrance holes).
The base plate can then be screwed in place. I used 8mm M3 screws, with a flat countersunk head, but these would also have worked.
Using the Sonoff
I use these Sonoff Switches in the following situations:
- Depending of the time of day and presence of people, automatically toggle lamps on or off in our main living areas.
- Linked with a wake-up alarm time, switching my beside light on.
- Because our HRV is not a smart device, I use one of these to turn it off when our air conditioners turn on (so that the HRV is not pumping hot air into our rooms while the air conditioners are trying to cool them).