#127 Home Automation without coding: Sonoff , MQTT,Alexa, Nod-Red

#127 Home Automation without coding: Sonoff , MQTT,Alexa, Nod-Red

I wanted a nice automation system for my lab
using the things I built in my last videos. So, I use my Raspberry Zero with Node-Red
and Mosquitto as the “brains”, some Sonoffs as switches for my LED lamps, and some Wemos
Minis as PIR sensors to make sure, that the lights are on and off at the right time. And I want to use one of the new “frameworks”
to program the Sonoffs. And of course, I also want to have some “modern”
stuff. This is, why I wanted to include Amazon’s
Alexa into the whole play. I like this concept where my orders are followed
without arguing… Very rare, these days. This alone should be worth the 60 dollars
for the Amazon echo box. And, not to be forgotten, the whole thing
should be IOTappstory compatible… Is this a good idea? Do these frameworks really reduce the programming
effort? How can we make Node-Red behave how we want
it? In this and the next video, we will learn
a lot about these components and at the end, you will have some additional knowledge to
make an educated guess for your own projects. And we will even write some lines of java
script to augment existing functions in Node-Red. So, let’s start. First an overview of the final system: There
are two LED lamps, one for the main work space and one for the bench. To detect where I work, I have two PIR sensors. My newly created Pi Zero is in the middle
and runs Mosquitto MQTT broker, Node-Red, SQlite and webadmin. As an additional system, I recently added
an Amazon Echo Dot. What is the scenario? If I sit in front of my computer, I only want
the main lamps on. If I work on the bench, I want all lamps on. This should be switched by the PIR sensors,
but I also want the ability to override the sensors from my computer, or with Alexa. If I ask e.g. Alexa to keep the lights on
or off, I want an immediate reaction. All devices are, of course, connected via
MQTT and the Raspberry Zero should be able to log whatever messages I want to log into
a SQlite database. And I want to monitor the Pi Zero using Webadmin. Today, we will build, program, and test the
devices and in the next video, we will connect them to the Mosquitto broker and implement
the logic in Node-Red. Frequent viewers remember, that I made some
videos on how to program the Sonoff devices. Each program was a special build for a particular
purpose. In the meantime, some frameworks like Tasmota
or Espurna submerged. They provide an infrastructure to run Home
Automation devices and it should be easier and faster to program these devices than writing
individual programs for each one. So, my first decision was to test one of these
frameworks. Because I like the style of tinkerman, I will
use his Espurna framework. This framework can read switches or sensors,
can transfer the readings either to the built-in LEDs or relays, or transfer the readings via
MQTT to other devices. Also built-in is an interface to Alexa and
some other cool stuff we will not cover here. You just have to select, which device you
want to use. The rest is done by the compiler. Espurna also has a web-interface to change
some values during operation. If your device is not foreseen by tinkerman,
you can define your own. As an example for a new device, I will use
Espurna to build our PIR sensor. A big advantage of such frameworks is, that
you can change the device from for example a normal Sonoff Switch to a Sonoff dual. Then, the only thing would be to select another
device, compile the code, and flash it to the dual switch. Cool. So, let’s start with the programming of
the Sonoff switch. Here you have two possibilities, as usual,
a complicated and an easy one. Either you chose the complicated way and you
follow the link in the comment and download the Espurna project from Github. Tinkerman uses many libraries and you have
to install all of them, which is some work. But fortunately, I did all this stuff for
you and uploaded a ready-made file to IOTappstory.com. So, the easy way is
Just four easy steps: 1. Flash and run ESP_Loader to get the MAC address
of your device 2. Prepare your device and your project on IOTappstory.com
3. Enter and save credentials of your Wi-Fi on
your ESP and EXIT 4. Reboot and automatically download sketch from
IOTappstory.com You find a detailed description in IOTappstory.com. I include a link in the comments. Because I do this frequently, I built a small
device to do that. Those of you which do not know, how to do
it, please watch video #93. Please make also sure, that you use the version
1M-128k for the Sonoff. And please run your Serial Monitor and note
the MAC address of your device. Now, you can log into your account on IOTappstory.com,
go to control panel and devices, and create a new device. Give it a name, and select the Sonoff switch
as a basis. The only thing you have to add is the MAC
address from before. Save the device and create a project. I name it “lab light”. In the project, you select the app you need,
which is the “Sonoff Standard Espurna”, and select your device you created before. I added here a second device for the bench. All devices in this project will always get
the same firmware delivered over the air. Save the project and make sure, it is “online”. Done. As soon as you switch your Sonoff off and
on again, it will get the firmware delivered over the air. And if I update the sketch, you will always
get the newest version. You can follow the update process in the serial
monitor, if you want. Now, you should see a new Wi-Fi network. Because this is not your home Wi-Fi network,
most people use their mobile phones to do that. A new screen should show up and you can enter
the Wi-Fi credentials of your home network. If no screen appears, connect to After you entered your credentials, press
save, and wait for at leat 10 seconds. Then, you press exit and your ESP reboots. Now, it should connect to your Wi-Fi and download
the sketch from IOTappstory.com. and log into your device using admin and admin. The first step is to change your password
and then, you can give your device a name. In the future, the MQTT topics automatically
will use this name to distinguish between different switches. Here, you find also the IP address for future
reference. Add you home Wi-Fi network or networks and
enable “Alexa”, if you want to use it later on. In the MQTT tab, you have to provide the IP
address of your MQTT broker. For our tests, I use cloudMQTT. Because CloudMQTT uses credentials, you have
to enter them here. For my private broker, I leave these two fields
empty. If we go now to the Websocket UI of cloudMQTT,
we should see the first messages of our Sonoff switch. This is really cool. Not one line of coding… If you search now for the topic with the name
“relay”, and copy-paste it into this “send” topic field, add a “1” as a message and
press “send”, you should hear the relay on your Sonoff click. If not, send a “0” and you will hear it. If your serial monitor is still connected,
you should also see the messages coming. So, communication in both ways work and we
can continue with the next step. I started with a Wemos mini board, connected
a PIR sensor to pin D5, a red LED to pin D6 and a green one to D7. This was the whole soldering. And then, I printed a small 3D case and ready
was the PIR sensor. Before we close the case, we also have to
connect the Wemos board with a USB cable, and flash the ESP-loader, this time, the 4M-1M
version. Then, we go back to iotappstory.com and create
a new device. Exactly as with the Sonoff switch before. Next, we create a second project, called “LAB
PIR sensors” and select the device created before. The app this time is “PIR Sensor Espurna”. Save the data, and reset the Wemos. After a minute or so, your Wemos mini is a
fully functional PIR sensor, and you again can connect to it using your smartphone. Enter also a new password, the Wi-Fi credentials,
and the CloudMQTT credentials. Now, you should also find the first messages
of your PIR sensor on CloudMQTT. But this is not all. As said before, I want to use Alexa. So, we have to add a few more steps. I assume, you already have your Echo registered
at Amazon. After logging in, we go to the “Smart Home”
tab and press the button “discover devices”. After a while, you will find the board names
in the list. They are “WeMo Switches”. This has nothing to do with our “Wemos”
boards. Wemo is a commercial brand which developed
an interface to Alexa. And a hacker wrote a code to emulate these
Wemo products with our devices. This is how the brave new world works… BTW, this library is called “Fauxmo” and
is already implemented in Espurna. If we ask now Alexa to switch our board on,
we see the message on CloudMQTT. So, we just created a PIR sensor, which acts
also as a Gateway to Alexa. No additional hassle or cost. Big thanks to Xose alias tinkerman! One remark to Alexa: Do not call your board
“switch”. And I wanted to name the second lamp “bench”. But she did not like my swiss accent and did
not understand. So, I decided for “table” which seems
perfect for her. Also with the Swiss accent! Summarized,
• we started to build a fully functional home automation system consisting of two PIR
sensors and two Sonoff switches • We used the Espurna framework to save
us a lot of time. This framework is configurable for many Sensors
and switches and saved us a lot of time • To further reduce the effort, I ported
Espurna to IOTappstory.com and created two apps on IOTappstory.com which can be used
by everybody • To start with a new device, we had to
download the respective ESP-loader and flash it to the device. This is a one-time effort
• After that, we created our devices on IOTappstory.com and linked the device with
the app into a project. If we activate the project, our device always
gets the newest version of the firmware, because, during boot, it calls “home” and downloads
any new version if necessary • Because Espurna emulates a Wemo switch,
we were able to connect our PIR sensor to Amazon’s Alexa. Now, we can also control our lamps with our
voice In the next episode, we will dig into node-
red and learn some cool stuff there. The only remaining thing is to ask Alexa:
Alexa, did you like this episode? I hope, you have an opinion. And you do not forget to press the “like”
button accordingly. Bye. Alexa, please sing
a song!

58 thoughts to “#127 Home Automation without coding: Sonoff , MQTT,Alexa, Nod-Red”

  1. And this is how skynet began….

    Great stuff btw. Learned a lot.

    PS: For people that don't have alexa there are guides on how to duplicate the functionality with a raspberry pi. Or if you are proficient with python you can do DIY speech recognition with the SpeechRecognition package.

  2. I think you know, you could have run 'alexa' in the raspberry pi (just attaching a microphone).
    Really well done btw!!!! Congratulation and thank you for sharing your projects with us!

  3. great video, as always!
    have you ever heard of a smart light bulb called LYT8266? based on the ESP8266. a friend recently gave me one, but the manufacturer barely gives any documentation.

  4. hi Andreas!
    what about Alexa, how did you get it? As far as I know, it's sold only in USA, UK and maybe Germany… and, did you have to do special mods to you amazon account to have it working in Switzerland? Do Amazon make regional restrictions on its use?

  5. Hi Andreas,

    Tried creating an account on iotappstory (thinkware) but won't let me login. Followed reset password etc, but still no luck.

  6. I can't keep up with technology. Its moving faster than I can learn! Anyone else have this problem?
    By the time I finish something. I have to had learned 10 things to do that one thing. By the time I am done, something new comes along and bam… all over again!
    It has been my experience, that if you want your orders followed, get married… I am sure your wife will follow your orders to the letter! (Devil's advise)

  7. I am pretty sure I saw a project that used a Pi to do the Alexa thing as well. You might be able to use that and make a complete solution in one go.

  8. Great video Andreas!
    Guess getting things done without arguing is the reason why so man men use Alexa 😉
    I also started using the fauxmoESP library (and still have 9 devices running that way).
    But in the meantime I switched to implementing real Alexa skills, because you the have more options that way (the fauxmoESP is limited to say switch ON/OFF).
    This is done by a LAMBDA function (this is java script) implemented in Amazon, calling back to a WebService in my house.
    This way I was able to implement (translated to english) things like "Alexa query home status" .. and she is replying with some system parameters in my house … or "Alexa query callers" which goes to my DSL router (a Fritzbox), reading out the latest callers while I'm absent and telling them to me.
    Since I'm diabetic I implemented a few functions where I can tell Alexa to store the amount of insuling I injected per voice and querying later .. so she tells me … 5 entries injected 1 hour and 10 minutes ago.
    Very handy!

  9. Well done Andreas, more inspiration. I am still waiting for LoRa hardware and in an idle moment last week ordered a USB microphone to add AI to a not very busy Raspberry Pi3. Parallel projects (well sort of) in Ireland and Switzerland. Thanks again!

  10. Andrea's I wish you had done this last year. When I was having problems with the sonoffs and Node-red. I must reprogram the boards for Alexa. I wrote my own code which is crap but works. Do you have to upgrade the tasmato software from the IOTstore cloud server? Also great episode I don't know where you fine the time mate. Wind turbine blades together may send a photo soon.

  11. Jetzt hab ich wieder Zeit Deine spannenden Videos zu schauen – der Stress ist vorbei, gestern war Prüfung und heute war die Zulassung im Briefkasten 73 DL8CY

  12. I see you put a cap across your Wemos mini. What symptoms does this cure? I notice I have occasions where I have to request upload several times. Does the cap fix that?

  13. Hallo Andreas,
    Ich han d idee gha, en server zum downloade vo chliine dateie zmache zB es beld aber well ichs ned über miis nodemcu zum laufe bring han ich dänkt du de König vom esp8266 chönntisch doch es schpanneds video dezue mache wie me SPIFFS zäme met emne Http server im heimnetzwerk mache cha. En spezielle NAS so zäge.
    Und no en frog, ben ech de einzigscht schwiizer do?

  14. At 7:10min you select the app we need but you did not say how we get this app into our own selection list. I have tried from http://iotappstory.com/firmware-item/67/sonoff-standard-espurna# but clicking the "Add to My Apps" link does nothing.

  15. Hi Mister Swiss Accent
    First of all, i would like to thank you for your excellent video tutorials. i know how much time you are spending on this stuff. Big Thumbs up.

    Encouraged by this video i started to build my own automation system. But to be honest this stuff nearly screwed me up. Being a newbie it showed to be tough work to get my system running. I spend nearly 3 weeks to set up the raspberry, the development tools and to build my own firmware.
    I got stuck nearly every problem that could occur during the build process. Fiddling with linux was one of the issues.

    Due to some basic programming experience i thought that it would be easy to build my firmware.Wrong assumption.

    The build process was a trial and error approach due to the spare information for the firmware build. Most annoying was the fact that there is a plethora of information in the internet. But as a newbie i could not figure out if this information was useful for managing my problems. Most items i found were outdated, wrong context or even worse just false.
    Finally i found 2 major information that helped me out.

    I would like to ask you if it is possible from your side to provide us with a video showing a brief overview of setting up the environment, the build process and TOOLS (i.e. Platformio vs. Arduinoi) and hints to avoid the pitfalls for the build process.
    One of my major time consuming issue was to figure out how to configure the right Arduino Development Environment. Newbies will find really nothing starting for the first time.

    Thanks again

  16. Hi Andreas,

    hopefully you can help me out.
    I installed the Raspberry Pi Zero W DietPi with fixed IP address
    Starting from a PC I get Peter's menu and unning Node_RED, SqlLite, etc works fine. MQTT messages flow constantly in that environment. Settings in Node-RED for a MQTT node: Server:, Port 1883, no SSL, Client ID blank, Security: blanks, use legacy MQTT 3.1 support selected.
    I want to add Sonoff MQTT functionality. After several trials I ended up with Arendst Sonoff-Tasmota code, which loads fine on the Sonoff Basic. I get the Sonoff Basic menu on my PC or smart phone and I am able to manipulate the Sonoff using the console and the commands from
    It uses the PubSubClient for MQTT from knolleary.net and the latest Sonoff-Tasmota firmware 5.1.320170520.
    The standard sonoff.ino code is used on the Sonoff. In user_config.h is defined:
    #define MQTT_HOST ""
    #define MQTT_Port 1883
    #define MQTT_USER ""
    #define MQTT_PASS ""

    My problem: I cannot connect via MQTT to the RPi (the WiFi connection is OK). Error message is:
    MQTT: Attempting connection…
    mDNS: Query done with 0 mqtt services found
    MQTT: Connect FAILED to, rc 5. Retry in 10 seconds
    rc= 5 means: MQTTCONNECT_UNAUTHORIZED – the client was not authorized to connect.
    So I guess the client MQTT_USER is not really empty or the RPi does expect some user information.
    Do you have any clue how to get connected from the Sonoff to the RPi DietPi MQTT?
    Thanks in advance.


  17. May be of interest for you: AWS Greengrass for IoT: https://aws.amazon.com/blogs/aws/aws-greengrass-run-aws-lambda-functions-on-connected-devices

  18. Is there a forum connected to iotappstory ? My ESP8266 contacts iot but i get: 500 ESP MAC not configured for updates

  19. Hi Andreas,

    I finally got time and tried to install this on a Sonoff but run into a problem after a new firmware was downloaded from IoTAppstury.com.
    A lost my connection to my local WiFi and the user/password for mobile-access don't work.

    My workflow and console aoutput is below.

    Do you have a clue what's wrong?


    1) Flash Sonoff_Loader (1M-128k).bin with ESP8266 Flash Downloader
    2) Green LED on Sonoff is blinking
    3) Disconnect power
    4) wait 20 sec
    5) Connect power
    6) Got the message:
    !!!!!!!!!!!!!!! Please press reset button. ONLY FIRST TIME AFTER SERIAL LOAD! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    7) Disconnect power
    8) Enter SSID, password and name AP-name: IP-phone
    9) Device downloads Receiving sonoff-standard-espurna-1.6.9-p67.bin and Receiving lab-light-espurna-1.6.9-2061.spiffs.bin
    10) AFter reset it can no loger access my WiFi and starts up it's own accesspoint instead
    11) The AP-name is also not changed to IP-phone
    12) Trying to connect via mobile phone but it won't accept any credentials (tried: admin, fibonacci, SONOFF_07E452)

    I have also tried to compile my own Sonoff Espruna with same result. One differense is that I dont't have a spiffs.bin in that case (don't know how to make one).

    When I press the button on the Sonoff the status for the relay changes but no WiFi-connection 🙁

    Some output from serial monitor: (at first I have WiFi-access but after sketch update no WiFi access)
    Connected to WiFi… 🙂
    —————— Writing Config ——————————–
    Stored Odi_Network
    Going to IOTappStory.com
    MAC: xx:xx:xx:xx:xx:xx
    IP =

    Updating Sketch from…
    Update_server iotappstory.com
    UPDATE_URL /ota/esp8266-v1.php
    FIRMWARE_VERSION IOTappStoryLoader V1.2
    SKETCH_UPDATE_FAILD Error (6): Update error: ERROR[6]: Stream Read Timeout
    Updating Sketch from…
    Update_server iotappstory.com
    UPDATE_URL /ota/esp8266-v1.php
    FIRMWARE_VERSION IOTappStoryLoader V1.2

    Updating SPIFFS from…
    Update_server iotappstory.com
    UPDATE_URL /ota/esp8266-v1.php
    FIRMWARE_VERSION IOTappStoryLoader V1.2
    Returning from IOTAppstory

    ets Jan 8 2013,rst cause:1, boot mode:(3,7)

    load 0x4010f000, len 1384, room 16
    tail 8
    chksum 0x2d
    csum 0x2d
    ESPurna 1.6.9
    [email protected]

    ChipID: 07E452
    CPU frequency: 80 MHz
    Last reset reason: Software/System restart
    Memory size: 1048576 bytes
    Free heap: 32960 bytes
    Firmware size: 443344 bytes
    Free firmware space: 450560 bytes

    [SETTINGS] EEPROM size: 4096 bytes
    [SETTINGS] Settings size: 3085 bytes
    [SETTINGS] Saving
    [WEBSERVER] Webserver running on port 80
    [RELAY] Number of relays: 1
    [BUTTON] Number of buttons: 1
    [LED] Number of leds: 1
    [LED] Led auto indicator is ON
    Wi-Fi not connected…
    [MAIN] Time: Time not set
    [MAIN] Uptime: 20 seconds
    [MAIN] Free heap: 29080 bytes
    [MAIN] Power: 3127 mV
    [WIFI] Creating access point
    [WIFI] MODE AP ————————————–
    [WIFI] SSID SONOFF_xxxxxx
    [WIFI] PASS admin
    [WIFI] IP
    [WIFI] MAC yy:yy:yy:yy:yy:yy
    [WIFI] ———————————————-
    [MDNS] OK

  20. Please help! In the Movie at 8.20. You should reboot the Sonoff to start the upload of the espurna firmware to the device. It only goes back to the config screen. I don't see any activity on the serial monitor as decribed in the movie. Is config of my device wrong in IOT maybe?

  21. Andreas… im having a marathon of your videos right now.. Very good videos ! I should have seen these videos a long time ago..
    Please keep it up!.

  22. You should not use spaces in host names. You can use A-Z, 0-9 – and _. Upper or lower case doesn't matter for the name, but usually by convention one usually uses lower case for host names.

  23. Thanks for the video Andreas, I learned a lot. What I don't understand: Is your override node always giving out what you put in, ie a string or a number? I am trying something with BigTimer, works fine, but puts out a string when in manual mode and a number when in auto mode. Gruess us Bärn

  24. Thank a lot for this wonderful series! It opened my eyes to a lot new topics! Your DietPi image even worked on my RPi B+, thanks a lot for that too! And what you can do with Node Red really blew my mind! Cheers from Austria!

  25. Hi all, i tried to flash 2 sonoff basic with the IOPAppstory loader and now both are Dead. Im still able the reflash them with everything i want, but nothing happens. Even a simple Blink example does not work. Has anyone done a backup of the original Firmware and can can provide it to me? I searched tons of hours with no luck, and so i ask hereThanks a lotThomas

  26. Hi Andreas, how to initiate the Sonoff to download a selected Programm of IotAppstory.com. On my build PIR Sensor is a button on D3. Reset Button on Sonoff does not work. Thanks Thomas

  27. Hi Andreas, I have made the PIR-sensor with the same components as in your video, including the tantalum capacitor across the ESP8266 on the WeMos board. It works but I get lots of false triggers and I don't have a cat to blame 🙁 Have you seen this also?

  28. Hi again, I am planning to use the Sonoff T1 switch and I discovered that I have to use a newer version of Espurna than the current 1.6.9. New newest version (as 2017-08-25) seems to be 1.9.0. Was it much work to make it IoTAppStory-compatable and could you give me a hint how to do that upgrade?

  29. Great job,thank you for sharing it. I prefer to use everything in my home network, not outside that. More security and more reliability…

  30. I was able to use Esptool and flash two Sonof Switches. They accept my credentials and come up with a new IP address. I have never been able to get the screen that allows me to enter the Iotappstory ID. I flashed with the latest loader bin. Anyone have any suggestions.

  31. While watching this video, my Echo went nuts every time you said "Alexa" 🙂  Thanks for a nice, comprehensive video (as always).

  32. Dr. Spiees, thank you for the excellent channel. I echo the comments below … where do you find the time to do this and still have a regular job.

    Anyway I have been through this video and #93 and #34 and …. When I load IASLoader and compile it using an ESP8266-201 I get

    Arduino: 1.8.5 (Windows 8.1), Board: "Generic ESP8266 Module, 80 MHz, ck, 26 MHz, 40MHz, DIO, 1M (512K SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200"

    Build options changed, rebuilding all
    Y:arduinocodelibrariesIOTAppStory-ESP8266srcIOTAppStory.cpp: In member function 'int IOTAppStory::dPinConv(String)':

    Y:arduinocodelibrariesIOTAppStory-ESP8266srcIOTAppStory.cpp:526:56: error: 'D0' was not declared in this scope

    if (orgVal == "D0" || orgVal == "16") return D0;


    Is it because I am using the ESP8266-201? I don't have a Wemos.

  33. To all readers: if you haven’t done already please give this and others video’s of Andreas a Like 👍. He is really great!!

  34. Nice Andreas! I’m just starting to get into Node Red. I’m trying to create a two way switch with Tasmota T1 touch. Right now it works one way but not both ways. Meaning A switch controlling B switch but I can’t seem to get B controlling A

  35. Hi Andreas,
    Will this work with google mini as I don’t have an Alex?
    Thank you for your help I love your vids and look forward to Sundays having a coffee and learning something new.


  36. Hello Andreas, I'm trying today, in 2019 (about 3 years after you published this video), to follow your explanations and build my own Sonoff Basic and PIR_sensor. Just to know, your ESPURNA bin file is not anymore on IOTAppStory. So, I need to recompile myself and also, if I get your "https://github.com/SensorsIot/Espurna…", it use a first version of IOTappStory …. After 3 years, it's totaly normal but just to say, it was not so easy (for me). Thanks a lot for your job!

  37. This is one of my ho,me lamps using Sonoff S20 upgraded with i4things example – feel free to switch it ON or OFF 🙂 it actually goes On or OFF 🙂


    example : https://github.com/i4things/NodeAPI/tree/master/examples/ESP8266-01/1CH_RELAY/thing

  38. Hi, I am just starting out with IoT and do enjoy your videos. In this video you say you have saved all the necessary libraries (ESP Sensor Espurna 1.6.9) on the IOTAppstory website. I cannot find these anywhere on the website. I have even registered but still with no luck. Can you point me in the right direction please. Kind regards…

  39. Das Problem ist, dass diese Anweisungen für „Cracks“ gemacht sind, die sowas sicher sowieso können.
    Leider wieder nix für „Beginners“.

  40. Perhaps you should consider running a Mycroft instead of Alexa or Google home. They claim to be more privacy conscious.

Leave a Reply

Your email address will not be published. Required fields are marked *