---
title: Smart Home and Dumb Devices
---

***
[🗂 back to index](/)

***

So, I wanted to make my home smarter: rig up the lights so that I could
control them remotely. I wanted the system to have some nice properties:

- Sustainability: as little maintenance as possible -- avoid batteries, firmware updates and cloud connections;
- Reliability: I should be able to turn the lights on and off even when WiFi, home-assistant server and my phone are dead;
- Hackability: APIs that are easily accessible via WiFi.

Sounds simple enough, right?

After a brief look around the internet, I had a rough idea of the components that I need:

- A kinetic 433MHz light switch, akin to [this one](https://www.amazon.com/Powered-Required-Waterproof-Receiver-AC110-260V/dp/B075JDR553) but without the relay;
- A smart relay that can be triggered either via WiFi or 433MHz.

I went with the AS-6M light switch from the link above and [a Sonoff RFR3
relay](https://sonoff.tech/product/wifi-smart-wall-swithes/rfr3), bought
from a local supplier.

## The AS-6M Switch

The switch feels alright, I guess. Action is definitive and doesn't take
too much force. It can be disassembled relatively easily by first unclipping
the frame and then the button. It looks like this inside: ![Kinetic light
switch internals](./switch-inside.jpg)


## The Sonoff Relay

The relay made a great initial impression. The box is made of nice plastic,
and the cover can be unclipped without a screwdriver. Wire connectors are
easy to use, you only need a philips screwdriver to connect it up.

However, the controls of the relay are unimaginably horrible. According
to [the manual](http://dl.itead.cc/BASICR3/BASICR3-user-manual.pdf), in
order to reset the device and switch it to "pairing" mode, one needs to
hold the button down for 5 seconds; and to pair a new RF433 remote, one
needs to hold the button

> until the red remote-controlled LED indicator flashes once

Here's the catch: the red light flashes after you hold the
button for about 5 seconds, and when you let go of the button one of the
three things happen, seemingly chosen at random:

- Nothing
- The relay switches
- The blue "wifi" LED starts blinking either quickly or in a pattern.

After reading the manual twice, I still have no idea what the blinking
light is supposed to represent. I'm guessing that one of the patterns means
it has been "reset", and the other one means it's looking for a remote?
Hard to say.

Anyways, I didn't want to use the proprietary app that Sonoff suggests,
so I tried to put the relay in ["DIY mode"](http://developers.sonoff.tech/sonoff-diy-mode-api-protocol.html).
How do I do it? You guessed it:

> Long press the button for 5 seconds to enter Compatible Pairing Mode (AP)

Ummmm… So, now there are three actions associated with holding the button
down for 5 seconds?

After some fumbling around by pressing the button down for 5 seconds a
number of times, I managed to get it into the AP mode. I think what I did
was reset the relay by holding the button down until the blue LED lights,
check that the blue LED does two short flashes and a long one, then press
the button down for 5 seconds again until the blue LED starts quickly and
regularly flashing. The `ITEAD-<...>` network showed up on my laptop. The
rest of it went according to the DIY manual: visit http://10.10.7.1, log
in to my wifi network (I've set up a separate network with a separate firewall
zone, so that those "smart" devices can't access the internet), wait for
it to connect.

Now, how does one query and control the relay? There's a helpful [repository
with the documentation](https://github.com/itead/Sonoff_Devices_DIY_Tools),
but I'll save you the time and give a quick rundown.

The device advertises itself via mDNS. On Linux, mDNS discovery is done
with Avahi. Make sure you have it installed and run `avahi-daemon` as root.
To get the information from all Sonoff devices present on your network,
run `avahi-browse -ltpr _ewelink._tcp` (`-l` to ignore local services,
`-t` to terminate after dumping all the devices, `-p` to produce machine-readable
output, `-r` to resolve the records).

You can then parse the output (it's separated with `;` which are escaped
if found in TXT records). We're most interested in fields 8, 9 and 10 --
IP address, port, and the device information. In the 10th field, you can
see a `data1` record, which contains information about device's state (in
the case of my switch, the only interesting bits are `switch` and `startup`).
Also note the `deviceid` record, which will be useful later.

My device has IP 192.168.8.120, port 8081, and deviceid 1000ed8684. Substitute
your own values in the rest of the article.

There's also a second way to query the device, and it's really strange.

The full REST-ish API is described in the documentation repo mentioned above, but in short:

```
$ # Query all the device information (data1 from above)
$ curl -X POST -d '{"deviceid":"1000ed8684","data":{}}' 192.168.8.120:8081/zeroconf/info
{"seq":41,"error":0,"data":{"switch":"on","startup":"off","pulse":"off","pulseWidth":500,"ssid":"Keenetic2","otaUnlock":false,"fwVersion":"3.6.0","deviceid":"1000ed8684","bssid":"ec:43:f6:d7:dd:a2","signalStrength":-51}}
$ # Turn the switch off
$ curl -X POST -d '{"deviceid":"1000ed8684","data":{"switch":"off"}}' 192.168.8.120:8081/zeroconf/switch
{"seq":41,"error":0}
$ # Turn the switch on again
$ curl -X POST -d '{"deviceid":"1000ed8684","data":{"switch":"on"}}' 192.168.8.120:8081/zeroconf/switch
{"seq":42,"error":0}
$ # You get the idea...
```

Yes, this is how the API is supposed to be used. It always uses POST, and
has separate endpoints that nevertheless use a common JSON structure.

What is absolutely mind-blowing to me is that the device seems pretty well-engineered
other than its interfaces to the outside world. The separation between
tracks is good and there are anti-tracking slots:

![Sonoff RFR3 relay track separation](./relay-separation.jpg)

the box is well-made, and the software seems to be working reliably.

But let's not forget that the interface is excruciatingly awful. I don't
understand how an engineer who made this thought "yep, this is what we're
going to ship". And while adding more buttons would increase the cost and
the complexity of the device (components are quite crammed on the circuitboard
as it is), making the REST API better would take literally on the order
of _minutes_, with no additional cost.

## Interoperability

Now that I've set up the WiFi part of the relay, it's time to pair it with
the remote, right?

Well, after a lot of 5 second button presses and frustration, I can tell
you that the two are probably incompatible. I don't have any other 433MHz
receivers or transmitters handy (but one transmitter is on the way), so
I can't debug this further. Poking a multimeter in the kinetic switch shows
about 600mV between the power contacts right after the click, so I'm assuming
it's working fine. The relay also appears to be working, so the reasonable
assumption to make is that the two just don't work together.

To be continued after the battery-powered transmitter arrives.

## Home Assistant

Eventually, I would like to control all of my lighting and other appliances
via home-assistant. It has a nice web interface, a mobile app, and API
with a CLI command for scripting. I might write a simple GTK app for it
in the future.

For now, here's how to set up home-assistant with sonoff integration on
NixOS: <https://github.com/balsoft/nixos-config/commit/e54fd18fbbd01f41e1b96aee912add157c13930e>
.

***
[🗂 back to index](/)
