Please, proceed carefully following the tips published in this blog, specially when Main Power is involved. I'm not responsible for any damages caused by what is written in this blog.
Thank you

Thursday, 10 August 2017

Home Automation System - SONOFF and MQTT

Ok, it's time to get all the power of SONOFF.

Just to summarize, I've got a SONOFF Pow and a SONOFF Basic, both of them with TASMOTA firmware on board.

Using this fantastic firmware, I could control the device through serial comms, web api (http) and MQTT, a very good protocol for IoT purpose, thanks to its lightweight footprint. I spoke about MQTT in a previous post (see http://domoticsduino.blogspot.com/2017/07/home-automation-system-discovering.html)

I will use mostly the MQTT protocol; so, I have to setup SONOFFs to use it, entering the MQTT broker informations and the topics. As usual, the official Wiki pages help us (see https://github.com/arendst/Sonoff-Tasmota/wiki/MQTT-Features)

For those not able to run a local MQTT broker (Mosquitto is a good free broker), it is possible to use, only for testing purpose, some free public MQTT broker (example: Mosquitto Test Server, Hive MQ and Eclipse)

Browsing the configuration pages on the SONOFF web console, you could enter all the MQTT informations. You can open the SONOFF web console using your browser and typing the ip address in the address bar. A web page similar to the following one will be shown:

Clicking on "Configuration" ==>

We will use mainly the "Configure MQTT" option: the others are self-explained.

Here, I entered the Host (ip address or host name) and the Port (1883 is the MQTT default one) for the MQTT broker.

The Client ID field is a simple string which identifies the device on the MQTT broker. It will be better to be univocal among all the devices.

User and Password fields are used for broker authentication (highly recommended!!!)

Topic and Full Topic fields are the most important one for the MQTT protocol. Here you can find some information about them: https://github.com/arendst/Sonoff-Tasmota/wiki/MQTT-Features#mqtt-topic-definition

- Topic: normally used to identify the device; it will be part of the MQTT Full Topic. It will be better to be univocal among all the devices.

- Full Topic: is the complete topic used for MQTT messages.

Inside Full Topic, we could use two variables called %topic% and %prefix%.

%topic% will be replaced by one of the following system variables, according to the real situation: Topic, GroupTopic, ButtonTopic, SwitchTopic e MqttClient.

%prefix% identifies the type of message and could take three distinct values, corresponding to the folloing system variables: Prefix1, Prefix2 and Prefix3

- Prefix1 = cmnd, used for command messages
- Prefix2 = stat, used for status messages
- Prefix3 = tele, used for telemetry messages

When I speak about system variables, I mean a list of configurable TASMOTA properties for every device. There are so many...Here you can find the complete list: https://github.com/arendst/Sonoff-Tasmota/wiki/Commands#command-overview

Here is my SONOFF Basic configuration:

Topic = sonoffbasic01

Full Topic = tasmota/garden/light/%topic%/%prefix%/

For Topic and Full Topic I followed a naming convention, in order to understand the mean of a message, just reading its topic.

The Topic, as said before, identifies the single device; so I used the type of device followed by a progressive number. For example, the topic of the SONOFF Pow will be sonoffpow02. The same topic string will be used also as network name and mqtt client id for the device.

The naming convention for the Full Topic is: firmware/zone/type/%topic%/%prefix%/

I would like to clarify that these are not rules but only suggestions. I chose this naming convention, you could use another one. It's up to you...

The final step is to enable MQTT protocol on the device, looking for the MQTT enable flag in the Configure Other page.

Well, finally now I'm ready to test my SONOFF with MQTT.

Saturday, 5 August 2017

Home Automation System - OpenHAB 2 on Raspberry PI 3

It's time to setup my Home Automation System. I chose OpenHAB v.2, see my previous post http://domoticsduino.blogspot.com/2017/07/home-automation-system-discovering.html)

My target is to install OpenHAB v.2 and all related services on my brand new Raspberry PI 3; reading the official documentation (http://docs.openhab.org/installation/openhabian.html), I discovered OpenHABianPI, a custom operating system based on Raspian Lite, but optimized for OpenHAB. So I flashed on SD-CARD, with Etcher, a complete pre-configured image, downloaded from https://github.com/openhab/openhabian/releases. Then I plugged the SD-Card in the Raspberry slot and power it up, after connected a LAN cable (there is also a procedure to connect the raspberry direct to WiFi...http://docs.openhab.org/installation/openhabian.html#wifi-setup). The setup of OpenHABianPI automatically starts during boot and you can follow it through SSH. In about ten minutes the system will be ready.

The first thing to do is run the openhabian-config tool, which will let us to setup the complete system.

sudo openhabian-config

01 - Update and 02 - Upgrade System will keep the system updated and you will run them frequently.


- 10 - Apply Improvements ==> run all the submen├╣

- 20 - Optional Components ==> 21 - Log Viewer, 22 - Mosquitto (the MQTT broker) and 23 - Grafana (not mandatory but it's a very nice dashboard tool)

- 30 - System config ==> all optional except 34 - Change Passwords which will secure your system; 37 - Move root to USB;  is a very useful choice...it lets you to use an external USB Hard disk, instead of SD Card. I configured both my raspberries in this way, but I followed a manual procedure (see https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=44177&hilit=usb+stick+operating+system)

- 40 - openHAB related ==> 43 - Reverse Proxy very useful option to expose OpenHAB system on the internet and access it from everywhere; it will configure NGINX as SSL reverse proxy - pay attention that with this option your system will be exposed to internet and so if not well secured could permit to everyone to take control of your home devices.

Ok, OpenHAB is ready to use. I suggest you to take a look at the official documentation, tutorials and forum on the OpenHAB website http://docs.openhab.org/ https://community.openhab.org

The official youtube channel is https://www.youtube.com/channel/UC7OK88DW0La_BJlcXZg8ydQ/feed

Just another useful info: OpenHAB could use a lot of services to persist data; one of these services uses InfluxDB, a NOSQL database. It lets OpenHAB to store historical data series which could be displayed as charts or tables. Grafana, the open source dashboard tool, is perfectly integrated with InfluxDB. You could also use the standard MYSQL RDBMS as a persistence service. I will use InfluxDB, and so I will install it on the same raspberry as openHAB (see https://docs.influxdata.com/influxdb/v1.3/introduction/installation/)

Let's summarize my system:

- OpenHAB v 2.1
- Mosquitto MQTT Broker, with authentication
- Grafana
- InfluxDB, as OpenHAB persistence service
- Reverse proxy SSL with authentication, through NGINX , to expose OpenHAB and Grafana on the internet, in a secure way

If you are not able to setup and mantain reverse proxy, NAT and port forwarding, you could use My OpenHAB service, which lets you to exposed on the internet a local OpenHAB installation; registration is required.

Ok, we are ready to use our Home Automation System

Thursday, 3 August 2017

Home Automation System - Configuring SONOFF Basic

Priority to the youngest...the just arrived SONOFF Basic

The first thing to do is testing it with no modification...in this way, if the device is bad, we could return it. The procedure is the standard one, explained here.

First, plug the device to the main power; the SONOFF Basic, differently from the SONOFF Pow, has INPUT and OUTPUT terminals splitted, one on each side and has no earth connection.

I connected the INPUT side to a standard plug and the OUTPUT side to a lampholder...

I connected the plug to a wall outlet and the SONOFF turned on. Now I have to download the EWeLink App on my android tablet. During the first setup, I created a new EWeLink account. Then I followed the pairing procedure (see https://www.itead.cc/wiki/EWeLink_Introduction). The app suggested me what to do in order to add the device; the following step was to store the wifi SSID and password to join my home network; the last step was to give a name to the device. Ok, now I can take control of the device through EWeLink app.

Ok, the device is working as expected with the default firmware. Now I may update it with the TASMOTA firmware, using the same procedure as the SONOFF Pow, described in and old post (http://domoticsduino.blogspot.com/2017/07/home-automation-system-first-sonoff.html); it's important to unplug the device from the main power when connected to serial interface.

The main difference is that the serial interface is located in the middle of the device, near the button, and it has 5 PIN (not 4 as the SONOFF Pow). The PIN located far from the button is GPIO0 and it is not part of the serial interface.

Here is the USB-Serial converter, connected to the SONOFF

And here is the WiFi configuration panel, as the result of the procedure already followed for the SONOFF Pow (https://github.com/arendst/Sonoff-Tasmota/wiki/Button-Usage).

As you can see, the TASMOTA firmware let us to manage two WiFi connections; so, if the first one is not reachable or its signal is very low, the device switches to the second one, and vice versa. Very good feature...

Now, finally I will try the MQTT protocol...

Tuesday, 1 August 2017

Home Automation System - New mailbag...

In the meantime, time passes by and a new mailbag is arrived; it contains new toys for my Home Automation System: a Basic SONOFF and a Raspberry PI 3.

The Basic SONOFF is a simple WiFi switch, as the SONOFF Pow, which we already know, but without the power measurement chip. In simple words, it's a WiFi controlled relay.

The Raspberry PI 3 is the world famous mini PC (see https://www.raspberrypi.org/products/raspberry-pi-3-model-b):

- Quad Core 1.2GHz Broadcom BCM2837 64bit CPU
- BCM43438 wireless LAN and Bluetooth Low Energy (BLE) on board
- 40-pin extended GPIO
- 4 USB 2 ports
- 4 Pole stereo output and composite video port
- Full size HDMI

I will use it as an OpenHab v.2 server, with some addons as the MQTT broker...

I already own a Raspberry PI 2, but it's already running OSMC, a very nice Operating System which transforms PC in Media Center.

With the Raspberry PI 3 I received also a good "starter-kit": a 3A power supply , a transparent case and some heatsinks (see https://www.amazon.it/dp/B01DDFFOYK/ref=cm_sw_r_cp_dp_T2_UUqGzb73MSE2T)

In the next posts, I will write some words about the Raspberry PI 3 and OpenHab v.2 setup

Wednesday, 26 July 2017

Home Automation System - Configuring the SONOFF with Tasmota Firmware

I'm ready to test TASMOTA firmware, just uploaded on my SONOFF POW.

You can find the upload procedure in my last post (http://domoticsduino.blogspot.it/2017/07/home-automation-system-first-sonoff.html). It was fine...From now on we couldn't restore the original firmware and we couldn't use the EWeLink App anymore

Hoping that the SONOFF will power up :)

Now, I will plug all the things together...paying attention to the Main Power

I use the following diagram (thanks to the official site...):

To perform the first test, I used a plug connected to the INPUT terminals of the SONOFF (L = Line, N = Neutral, E = Earth). Then I took an old lamp holder, connected to the OUTPUT terminals. Last thing to do...find a load...here is it...an old 100W bulb.

I connected the plug and the SONOFF powered up. The blue led flashed a couple of times and then shut off.

The first thing to do is configure the WIFI connection; there are two ways to do this task: the one I followed is performed using the web console; the second one is performed writing the WIFI SSID and PASSWORD on a config file, prior to compile and upload the firmware (here you can find more detail https://github.com/arendst/Sonoff-Tasmota/wiki/Upload#preliminary-configuration)

So, I use the capabilities of the push button to transform the SONOFF into an Access Point and access it through the web console

Following this guide

I discover how to use the button. In particular, the "4 short presses" procedure permits me to activate the access point and the integrated web server, so it becomes reachable and configurable.

Then I push the button 4 times and a new Wifi connection brings on...SONOFF XXXX...I join this network using a PC and browse on a browser. The configuration panel is in front of my eyes

I typed the WiFi details, named the device, saved and restart.

After a couple of seconds, the SONOFF joined my home WIFI network and takes a DHCP assigned local IP.

Through this IP, I could get the configuration panel again.

Here is it:

Clicking on Toggle SWITCH the bulb lights up...the web page display the current power consumption...another click and the bulb turns off. The device is working as expected...thanks god :)

Reading the TASMOTA docs, I found a procedure to calibrate it.

See https://github.com/arendst/Sonoff-Tasmota/wiki/Sonoff-Pow#calibration

In the next step I'll try the MQTT protocol.

Stay tuned

Sunday, 23 July 2017

Home Automation System - The first SONOFF - Uploading firmware

Ladies and Gentlemen, I'm glad to introduce you my first sonoff: the SONOFF POW

After a rapid test with EWeLink App I'm sure the device is working well; I'm ready to upload the TASMOTA firmware, to enhance the sonoff functionalities, in particular the MQTT protocol.

I borrow from Piero the USB-Serial adapter, because mine is still "in transit"...

Here is it: one thing to check is the jumper near the 4 serial pin...it has to be in the 3.3V position...

I open the website https://github.com/arendst/Sonoff-Tasmota/wiki, download the software and follow the step-by-step procedure.

I like Arduino IDE and so I decided to use it, instead of PlatformIO, so I follow the section "Arduino IDE Preparation" on wiki (https://github.com/arendst/Sonoff-Tasmota/wiki/Prerequisite#arduino-ide-preparation).

Now, it's time to solder the serial pins on the sonoff, so the board will be removed from the plastic case. In the Sonoff POW the serial interface is at the end of fhe board, near the phisical button.

See Hardware Preparation

You have to pay attention at the serial connections between adapter and board...

TX -> RX
RX -> TX

Thanks to Piero again, I can use a connector with colored cables which is already configured for the sonoff mapping

Using a MicroUSB - USB cable, I plug the adapter in the PC and install the FTDI driver. The sonoff must be restarted in "programming mode", keeping its button pushed for 2 or 3 seconds.

During this phase (and everytime the serial connection is plugged), the sonoff MUST BE disconnected from Main Voltage

If everything is OK, the device is ready for the firmware upload process.

Using the Arduino IDE, in just a couple of minutes the firmware will be uploaded on device (see Upload).

Now we are ready to play the game :)

Saturday, 22 July 2017

Home Automation System - Discovering sonoff

In the last few months, thanks to Piero's suggest, I discovered some small device called SONOFF, by iTead https://www.itead.cc/smart-home.html

In simple words, they are low-cost wifi controlled relays. A basic switch is less than 5 euro!

The following products, among others, are the most interesting, for my purpose:

Sonoff Basic: a standard switch - https://www.itead.cc/smart-home/sonoff-wifi-wireless-switch.html

Sonoff Pow: a standard switch with power consumption measurement - https://www.itead.cc/smart-home/sonoff-pow.html

I bought them on eBay, for testing purpose.

These devices are easily configurable and ready to use; just connect them to the power. The onboard firmware let them be controlled by a mobile APP (iOS and Android), called EWeLink. A step by step procedure helps user to pair the device to the home wifi network (mandatory...). The devices will be controlled directly through internet, using an Amazon AWS server managed by iTead.

This is a simple guide: https://www.itead.cc/wiki/EWeLink_Introduction

This is a simple and rapid mode to operate with these devices; but if your home network is offline, they stop working.

Furthermore, to get all the capabilities of these devices, you need a custom firmware, because the original one is not so open. For example, you cannot integrate sonoff devices in a Home Automation System like OpenHAB or Domoticz

So, my first step was googling for "Sonoff Firmware"...Bingo!

To be simple, Sonoff is a device built on ESP 8266 chip, so it's easily programmable (as Arduino board) and you can find many free custom firmware: you choose it according to your needs or experience...

I chose this one:

Sonoff-Tasmota firmware: https://github.com/arendst/Sonoff-Tasmota

At the first glance, it seems the most complete, for my purposes..It allows to control the device by MQTT, Web and Serial Communication.

Please, take a look at the official Wiki page:


About MQTT, I can suggest you to take a look at the official website: http://mqtt.org. It is a very useful protocol to manage IoT messages.

In a nutshell, MQTT is a connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is based on a central service, called broker, which receives published messages and forward them to the subscriptors. Every message is published on a particular "topic" and only the subscriptors of that topic will received it.

So, we have to manage another service, the MQTT broker. But don't worry about that, it's very simple and also Raspberry PI is able to run this service (Mosquitto https://mosquitto.org is one of the best...). Just for testing purpose you can use a free public MQTT broker, but I discourage you to use it for your real Home Automation System

After choosing the firmware, just upload it on the chip.

WARNING...you will lose the original firmware and you will not be able to upload it back; will no longer be possibile to use the EWeLink app again

The custom firmware will be upload using the serial interface on the sonoff, through a 3.3V USB-Serial adapter. I bought one on banggood. You could upload the firmware using the Arduino IDE or PlatformIO. Please follow the step by step guide provide by the firmware developers.

Before, we spoke about Home Automation System. I suggest you 3 system I tried:

Home Assistant - https://home-assistant.io

There are many others...the final choice it's up to you. Internet has lots of information and guide to assist you...

I chose OpenHab v.2; in my opinion it's the most versatile one (but I could be wrong :) ).

Shortly I will publish the results of the first tests performed with SONOFF

So, please, Stay Tuned

Friday, 24 February 2017

Rf 433 Mhz data "sniffing"

It's been a long time when I wrote my last post...My job and family matters keep me far from my hobbies...priorities won

As some of you could remember (see post http://domoticsduino.blogspot.it/2016/03/data-acquisition-photovoltaic-production.html), I acquired the production of my photovoltaic plant using the OWL CM160 wireless receiver, plugged into the USB port of Raspberry.

Unfortunately, this system was not so stable as I wished, because of the USB driver...and so Raspberry needs to restart...not so convenient...isn't it?

I found an internet blog where someone was able to sniffing wireless data packets trasmitted by the sensor to the OWL, through a simple wireless receiver, as the one I used to receive the gas sensor data...(see post http://domoticsduino.blogspot.it/2015/08/data-acquisition-receiver.html)

This it the article:


It's using a CM180 wireless receiver, but the explanation works well for the CM160 too.

These systems use the Oregon Scientific communication protocol...My CM160 use the version number 3...

Here is a doc about Oregon Scientific protocol...


Data is transmitted using che Manchester OOK (on off keying) code at 433 Mhz

So I connected my wireless receiver to an Arduino UNO and I downloaded the sketch which allows me to decode the OOK transmission. Playing with bits I was able to decode the transmitted value of the solar power...

Some example in hexadecimal chars:

2a8081bf01501a011900 ==> power 434 W
2a81817f02d0fdf71800 ==> power 627 W
2a81813f0980f3b71300 ==> power 2366 W

Output on the serial port

The next step will be the transmission of received data to central server

Sniffing all the 433 Mhz packets, I noticed that there are packets with a different format...these packets may be transmitted by other sensors. One of these sensor could be the temperature gauge, placed outside the house. At this time I'm not able to decode this particular signal...maybe in the future??