A short description (mostly notes to myself) how to make a sonoff device work with fhem and alexa..
Some pictures
Hardware
- A sonoff device. There are various, I used a Sonoff Basic initially, got it from here
- Pin strip 2.54 with 4-5 pins (one is unused, 4 is enough)
- A ftdi usb/serial converter - most should work, it shoul dprovide 3.3V, gnd, rx and tx
- An Amazon Echo or echo dot
Software used
You will have to get:- FHEM obviously
- Arduino IDE for programming - here I used the latest zip for non admin install (version 1.8.5)
- mosquitto as an MQTT broker - downloaded the latest source - v1.4.14
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:arduino-1.8.5 --portable] ----packages\esp8266\hardware\... ----sketchbook\ ------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 sonoffYou 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.
- Configure module - you can set the type, and what additional HW you have connected to pins
- Configure wifi - self explanatory
- Configure MQTT -> you can redefine the host ip, and port. the client (aka sonoff) user and pass: I used the default DVES_USER, and the topic (this is the one you'll access later from fhem). I used "sonoff_kitchen" as the topic.
- Configure Other -> set emulation to Belkin WeMo (single device) or Hue Bridge (multi device) - I used WeMo
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:
make 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:- for the broker:
define myBroker MQTT 192.168.1.3:1883 attr myBroker room MQTT
- and for the sonoff:
define Sonoff_bucatarie MQTT_DEVICE attr Sonoff_bucatarie IODev myBroker attr Sonoff_bucatarie devStateIcon ON:FS20.on OFF:FS20.off attr Sonoff_bucatarie publishSet ON OFF cmnd/sonoff_kitchen/POWER attr Sonoff_bucatarie room MQTT attr Sonoff_bucatarie stateFormat Lumina attr Sonoff_bucatarie subscribeReading_Lumina stat/sonoff_kitchen/POWER attr Sonoff_bucatarie subscribeReading_Sensor tele/sonoff_kitchen/SENSOR attr Sonoff_bucatarie subscribeReading_Status tele/sonoff_kitchen/STATE attr Sonoff_bucatarie webCmd ON:OFF