FHEM + Sonoff + MQTT/mosquitto + Amazon echo/Alexa

About me
Friends' pages:
Email me:
alex AT joni.ro


A short description (mostly notes to myself) how to make a sonoff device work with fhem and alexa..

Some pictures


Software used

You will have to get:

Flashing the Sonoff

I followed the instructions found at: the Tasmota wiki/Arduino IDE. Caveat: I had to initially add the package_esp8266com_index.json to Additional Boards Manager URL, then install the latest version (2.3.0) and only after that I did the file copy described in the previous paragraph. I ended up with these folders:
------librariers\(all libs from the tasmota source)
------sonoff (the sonoff source itself) </pre>]

After this, you have to edit the portable\sketchbook\sonoff\user_config.h to add your SSID/pass, MQTT gateway IP and other pertinent stuff (TLS yes/no, etc).

Then you can start the Arduino IDE, load the sonoff.ino (same folder), and verify the build (aka compile).

If all is well you can proceed with flashing, making sure you have selected:

Board: "Generic ESP8266 Module"
Flash Mode: "DOUT"  !!!!
Flash Frequency: "40MHz"
Upload Using: "Serial"
CPU Frequency: "80MHz"
Flash Size: "1M (no SPIFFS)"  !!!!
Debug Port: "Disabled"
Debug Level: "None"
Reset Method: "ck"
Upload Speed: "115200"
Port: Your COM port connected to sonoff

You should now have a sonoff which connects to wifi, gets an ip (check your dhcp server) and you can access it's webinterface at that ip address.

Configuring the Sonoff

Open the sonoff webpage, and you get a nice interface to see and toggle it's state, but also a chance to edit it's configuration.

After configuring and saving the sonoff/Tasmota will restart with the new settings.

Alexa integration

Now you can use Alexa (e.g. the alexa app) to scan for smarthome devices, and the new device will come up. I created a new group (called it Kitchen) and added the Sonoff it discovered (the name was Sonoff1, defined in Config/Other -> Friendly Name1) to that group. Now you can easily turn on/off the group with voice commands. Trying to say alexa turn on sonoff1 was a bit harder, as it understood Sonos most of the times, and complained about missing Sonos devices ;)

MQTT stuff

Up until now the system should work by itself, but without FHEM integration.
In order to work with FHEM we need to bridge the sonoff through an MQTT broker.
I used mosquitto (dl link above), which I didn't find as a package for my ancient NAS running debian 5/lenny. Compiling mosquitto was pretty straight-forward, I had to install: cmake libssl-dev uuid-dev
I had to disable TLS-PSK, as the ssl version I had was also very old and I'd get compile errors
I also had to comment #WITH_SRV=yes - that's for server lookup, also didn't work - some missing headers/libs.

After that it's just:

sudo make install

Next I created a runscript for mosquitto (/etc/init.d/mosquitto) available here. After changing /etc/mosquitto.conf.sample to mosquitto.conf, adapting it (had nothing to configure really) I managed to run the mosquitto broker.

Immediately I saw the sonoff connecting to it, and using mosquitto_sub (command line subscriber) I could subscribe to it and watch the current state)

FHEM stuff

I followed the wiki entry from fhem, but had to make some changes. Here's the final definitions I used in fhem: