Disclaimer

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

Wednesday, 11 October 2017

Home Automation System - OpenHAB v.2 and local network monitoring




Just to go deeper in the learning of the OpenHAB v.2 world, I tried to design a little and simple local network monitor tool.

For this purpose OpenHAB v.2 give us a 2.0 Binding, which can be setup directly through Paper UI: the Network Binding, which can be easily enabled in the Add-Ons section

As usual, the official documentation is very detailed: http://docs.openhab.org/addons/bindings/network/readme.html

This binding gives us a tool to perform network scans, using system ping or port scanning technique, to detect any device joining my network; so I could be informed when a known device is ONLINE or OFFLINE

The new Things objects will be devices identified by address ip; for each of them there will be two channels:

- Online: could be linked to Switch Items to show the network status of the device;

- Time: could be linked to Number Items to store the reply time of the device

With this binding just installed, the things will be automatically discovered and OpenHAB will notify us with the menù item INBOX, on the top left side of the UI (in case, you can force the discovering process using the "search" icon on top right)


Identified the network devices, with a couple of clicks I could create the Things and, through the Channels , also the Items

Here is my Thing -> Channels -> Item chain about my home router


And here is the configuration form for the "Home Router" Thing:


Beyond the configuration parameters described in the official docs (http://docs.openhab.org/addons/bindings/network/readme.html), I also used the one called "Location", just to identify the real location of the device.
I decided to use the floors and the garden as location choices. So my home router is at "first floor" location.

After all devices was added, the first test is to have a look at the "Control" menù, by selecting the "First Floor" location.
Here is the result:



Last step is adding this informations on my sitemap. So, using SmartHome Designer I edited the /etc/openhab2/sitemaps/test.sitemap. file

I added, just under the "General" section, a new frame called Network which contains the Items just created in Paper UI. Here are the lines:

Frame label="Network" {
Switch item=HomeRouter_Online  label="Home Router"      icon="switch"
Text   item=HomeRouter_Time   label="Home Router Time [%s ms]"       icon="network"
Switch item=LamanNAS_Online label="LamanNAS"        icon="switch"
Text   item=LamanNAS_Time label="LamanNAS Time [%s ms]"      icon="network"
}

(HomeRouter_Online/Time and  LamanNAS_Online/Time are the names of the items as we can see on Paper UI).

Here is the result:


Just to make all these things more useful, I could create a new rule for every device I would like to monitor...

So I added the following lines in the /etc/openhab2/rules/checks.rules file, where we can find the already known rule related to the TASMOTA firmware update:

rule "home router check"
when
Item HomeRouter_Online changed
then
logInfo("rules", "HomeRouter_Online online status changed")
sendNotification([user_to_be_notified], " HomeRouter_Online online status changed ")
end





Sunday, 8 October 2017

Home Automation System - Checking TASMOTA firmware updates with OpenHAB v.2





A little post about managing notifications about new TASMOTA firmware versions for SONOFF devices on OpenHAB v.2

As we already know reading the official TASMOTA documentation (https://github.com/arendst/Sonoff-Tasmota/wiki/Upgrade#firmware-binary-sources), @smadds user manages a public MQTT broker "sonoff.maddox.co.uk:1883", where he publishes the last version number of the TASMOTA firmware using topic sonoff-version.

So, in my setup, I just added an "item" linked to this value, through MQTT and then published it on my sitemap.

First thing to do was create a new MQTT connection (called sonoffmaddox-mqtt) to the @smadds broker, appending the following lines to the /etc/openhab2/services/mqtt.cfg file

sonoffmaddox-mqtt.url=tcp://sonoff.maddox.co.uk:1883
sonoffmaddox-mqtt.clientId=
sonoffmaddox-mqtt.user=
sonoffmaddox-mqtt.pwd=
sonoffmaddox-mqtt.qos=0
sonoffmaddox-mqtt.retain=true

Then I created a new file called general.itemsin the /etc/openhab2/items folder, in which I wrote down the following line:

String      SonoffTasmotaVersion      "SONOFF Tasmota Version [%s]"     <settings>
{mqtt="<[sonoffmaddox-mqtt:sonoff-version:state:default]"}


My item is called SonoffTasmotaVersion, its type is "String" and it is linked to the mqtt connection called sonoffmaddox-mqtt, topic sonoff-version. In this way, this item will be updated everytime someone will publish new messages on topic sonoff-version.


Last step: I added my new item to the sitemap, just to keep tracking of the last TASMOTA version.
So, I edited the /etc/openhab2/sitemaps/test.sitemap file, inserting, on the top, the item just created inside a frame called "general":

Frame label="General" {
    Text item=SonoffTasmotaVersion
}

Here is the result:



To be more useful, I created a new OpenHAB rule just to be notified every time the TASMOTA version will be updated.

So, I created the /etc/openhab2/rules/checks.rules file, and wrote down the following lines:

rule "tasmota version check"
when
Item SonoffTasmotaVersion changed
then

logInfo("rules", "New Tasmota Version " + SonoffTasmotaVersion.state)

sendNotification([user_to_be_notified], "New Tasmota Version " + SonoffTasmotaVersion.state)

end

The sendNotification function sends a notification to the specified user; this notification reaches the mobile devices where the OpenHAB App is installed and linked to the OpenHAB cloud (MyOpenHAB). Its configuration is very simple, just following the official guide: http://docs.openhab.org/addons/io/openhabcloud/readme.html

Now I have to wait for the next TASMOTA update just to check that all is working as expected.

Monday, 2 October 2017

Home Automation System - External buttons and switches on the SONOFF Basic with TASMOTA




I need to control SONOFF using either physical buttons and switches and not only through OpenHAB

The easiest way is to use a button directly connected to the physical button pins in the SONOFF. I already made a test with my first SONOFF Basic, as described in a previous post(http://domoticsduino.blogspot.com/2017/08/home-automation-system-sonoff-and-mqtt_19.html).

In this way we could activate and deactivate SONOFF as we do using the physical button. But I need to control the SONOFF through switches too. Reading the TASMOTA documentation, I discovered that I could connect a button / switch to the GPIO14 pin. Then I could tell TASMOTA what kind of object I connected...if a standard button or a switch.

To do so, I have to connect the button / switch to the GPIO14 pin and to the GND pin on the board; both of them are easily reachable. The GPIO14 pin is the one which is not utilized on the serial interface, and the GND is just beside it (already used for the serial communication).





When the hardware link are OK, I have to configure TASMOTA

In the configuration menù of the web interface, I choose 09 Switch1 as the new value for GPIO14 drop down list.



Then, I have to set SwitchMode parameter accordly to my needs, through the Console: if I will work with a button, I set up the value 3, otherwise I will put 0


And finally here is a video with my test



Saturday, 30 September 2017

Home Automation System - MQTT, SONOFF and OpenHAB v. 2




It's time to integrate MQTT and OpenHAB v.2.

As already said in a previous post (http://domoticsduino.blogspot.com/2017/08/home-automation-system-sonoff-and-mqtt_19.html), actually I have a SONOFF Basic already installed which drives a garden lamp. As we already know, it's already setup with MQTT and so let's start the OpenHAB integration.

I followed step by step the Tasmota wiki, in the integration section (https://github.com/arendst/Sonoff-Tasmota/wiki/openHAB) and so, as first step, I installed some ADDONS in OpenHAB:









Now I have to setup the MQTT connection to the MQTT broker which runs on the same Raspberry PI 3 as OpenHAB 2; so I have to edit the configuration file /etc/openhab2/config/mqtt.cfg directly through the linux shell (nano or vi) or using Eclipse SmartHome Designer, an advanced text editor optimized for OpenHAB v.2 configuration files. I used it. Its setup is very simple and then it needs to know the OpenHAB configuration files path. In my case I setup a network drive (called Z) for a samba share automatically defined by the installation of openHABianPI.

In particular, the configuration parameters I edited are the following ones:

openhabianpi-mqtt.url=tcp://localhost:1883
serve and port of the MQTT broker

openhabianpi-mqtt.clientId=openhabianpi
openHAB client Id for the MQTT connection

openhabianpi-mqtt.user=openhabian
Username for the MQTT connection

openhabianpi-mqtt.pwd=XXXXXXX
Password for the MQTT connection

openhabianpi-mqtt.retain=true
Defines if the broker should retain the messages sent to it

openhabianpi-mqtt is the logical name of the MQTT connection; in OpenHAB we could have many MQTT connections and in the items configuration we use this name as reference.

If you don't make mistakes, during the OpenHAB boot you may find log messages as following:


Ok, now I will configure items and the sitemap .

So, I created a file called sonoffbasic01.items in the folder /etc/openhab2/items in which I setup my new items.

I defined a "Switch" object ("light" category) which was linked to the full topic mqtt of the SONOFF Basic 01 tasmota/garden/light/%topic%/%prefix%/. Here is the syntax:

Switch Garden_Light_1Lamp "Garden 1-Lamp"      <light>
            (gLightSwitchGarden)
            {mqtt="
                     <[openhabianpi-mqtt:tasmota/garden/light/sonoffbasic01/stat/POWER:state:default],
                     <[openhabianpi- mqtt:tasmota/garden/light/sonoffbasic01/tele/STATE:state:JSONPATH($.POWER)],
                     >[openhabianpi-mqtt:tasmota/garden/light/sonoffbasic01/cmnd/power:command:*:default]"
             }

For info about syntax of .items files please see official documentation http://docs.openhab.org/configuration/items.html

In this way I created an item called Garden_Light_1Lamp linked to MQTT messages of topic defined in the {...} block

Then, I added this new item in my sitemap . So, I edited sitemap definition file, appending the following rows:

Frame label="Lights" {

Switch     item=Garden_Light_1Lamp        label="Garden Light 1 Lamp"       icon="switch"


}

After saved this file, OpenHAB updated the sitemap and the "lights" section appears




Here is a short video about my test:







Monday, 25 September 2017

Home Automation System - New Item: SONOFF Dev




A new mailbag...a brand new SONOFF Dev...bought directly from the iTead ecommerce website...for just 7 euros, shipping included.



In a few words it's an electronic device based on the famous ESP8266 chip.

It includes WiFi connectivity, 5 GPIO, 1 ADC, a serial port and 3.3V or 5V output pin. It already included a micro USB connector to plug in the PC or to connect an external 5V power supply. It supports Arduino IDE programming.

It has no default firmware and so you need a little programming knowledge.

In the next days I will do some test and then I'll try to integrate it in my Home Automation System, linking some sensors.

Wednesday, 20 September 2017

Home Automation System - It's time for OpenHAB 2




Ok, I played a lot with hardware device :), now it's time to get involved in the "software" area of my Home Automation Project. Who read my previous posts already knows that I chose OpenHAB v.2 as my Home Automation System software; it's already installed on my new Raspberry PI3, as described in a previous post:


For me this is a new "world" and so I will follow the official documentation (http://docs.openhab.org/introduction.html) and the community too (https://community.openhab.org/) where you can find a lot of useful infos.

In simple words (and based on what I understood), OpenHAB v.2 makes a link between the devices (real or virtual...), called "things", and logical entity, called "items". The items will be used to manage the "Home Automation System".


From www.openhab.org

To integrate the real or virtual devices (things) in OpenHAB v.2 you can use Bindings, which are objects that make OpenHAB v.2 able to communicate with external device (real devices or simple Webservice or data providers...)

"Items" integration inside the user interfaces will be done using "Sitemaps", while "Rules" permit to manage events and automation logics

All data which will transit over the OpenHAB v.2 Communication Bus can be stored through persistence engine called "Persistence"

I will try to explain all of these concepts as I move forward with my configuration.

So, my system is ready to be configured and used. This is my software configuration on Raspberry PI3

OpenHAB v 2.1
Broker MQTT mosquitto, with auth
Grafana
InfluxDB, as persistence engine of OpenHAB
Reverse proxy SSL with auth, using NGINX , to securely expose on internet OpenHAB Grafana.


The standard configuration (choosing the "standard package" at the end of the setup) of openHAB v.2 modules is the following one:

- BASIC UI: responsive UI for the standard user; it is built using Sitemaps

- PAPER UI: UI for configuration purpose, not completed yet (mmany configurations part will be done directly using configuration files)

- HABPANEL: user UI, optimized for tablet


There are lots of others modules; see the ADDONS section of PAPER UI.

To complete the overview, two words about native mobile App




Windows OpenHAB App, also for Windows 10, but it seems a bit "young".


Ok, it's time to let OpenHAB v.2 doing its stuff

Just to become familiar, I will try something easy; later I will try to integrate SONOFF with MQTT.

First of all, I follow all the beginner tutorial, just to become familiar with the environment and having no real device like LIFX lamp or KNX device, I decide to integrate speedtest and weather data, provided by external services.

For speedtest integration, I followed this great tutorial https://community.openhab.org/t/speedtest-cli-internet-up-downlink-measurement-integration/7611; so I learned as you can edit configuration files. A lot of OpenHAB areas are involved:

 - addons / bindings: to let OpenHAB v.2 exec shell commands
 - items: object creation as speedtest data storage "device"; 
 - sitemaps: user UI, linked to the items
 - rules: automation rule just to collect speedtest data automatically

In other words, OpenHAB v.2 will run speedtest-cli shell script (a textual interface for the speedtest service) at regular time interval (I set a 2 hours refresh), stored its result in item objects and then link these items to a user UI.

This is the result on a new sitemap (Basic UI)


For weather service integration, I chose OpenWeatherMap (already knows it)

So, I followed again the official documentation (http://docs.openhab.org/addons/bindings/weather1/readme.html) and reach the target was not so difficult. This is my final result:


For the files configuration, I used Eclipse SmartHome Designer (http://docs.openhab.org/configuration/editors.html#esh-designer), a graphical editor which provides some facility.

Well, with these test I verified OpenHAB v.2 is a working system. In short time my OpenHAB v.2 will be able to lights up my garden lamp (I hope...)...thanks to MQTT




Monday, 4 September 2017

Home Automation System - New items arrived...




During my configuration test, other "toys" arrived...

5 SONOFF Basic + a New Entry: 1 SONOFF Dual

The SONOFF DUAL is simply a double SONOFF basic: in fact it has two independent relays.

Here are my new toys, already labelled:


As usual, the first thing to do is test them with EWeLink, without open the case
The schematic is similar to the one of SONOFF POW; the terminal link are all on the same side 

There is no earth connection, but 3 pair of Phase / Neutral link, 1 for the INPUT Voltage and 2 for the OUTPUT.




Here is the activation procedure for my new SONOFF DUAL with EWeLink

Wednesday, 30 August 2017

Home Automation System - SONOFF Tasmota firmware upgrade using OTA




It's been a while since the first TASMOTA firmware upload and several updates have already come out.

Let's see the TASMOTA releases page

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

You can see that the latest version is 5.6.1 and I uploaded on my SONOFF 5.3.0 version, as you can see on the "information" page on the web console of the SONOFF


So, it's time to perform an upgrade.

Don't worry, I don't need to open,it, unplug from the main power, solder serial contacts and connect it to the PC...after the first installation of TASMOTA, I'm able to upgrade the firmware on the fly, using OTA (Over The Air)procedure; I have to tell it where it can download the new firmware and the game is over. Very comfortable functionality!!!

So, I followed the official wiki:

https://github.com/arendst/Sonoff-Tasmota/wiki/Upgrade#upgrading-via-ota

I chose the automatic download choice, so I have no need to download the new firmware and upload it to the device. I must only put the firmware url in a property of the SONOFF and start upgrade it. All these operations can be done directly from the console or through MQTT messages.

There is only one thing to check: the firmware size, because you can go out of memory if it's too big. There is a workaround for this: you can upload a minimal firmware and then the complete one.

Just a couple of notes:

- sometimes this procedure can go wrong...it seems that on some TASMOTA versions there is a bug with OTA (but I'm not sure about this). I tried 3 times to get it works.

- after upgrade SONOFF, it reboots with "access point" mode (I don't know if this is right or wrong...) and so I linked my PC to its wifi network, go into the web console (url 10.0.0.1) and just reboot it again



A special note here: subscribing to the topic sonoff-version on a public MQTT broker sonoff.maddox.co.uk:1883 you can get a message with the TASMOTA latest version (thanks to  @smadds)

Here is it, using mqtt-spy


Here is a video about my upgrade procedure:


Monday, 28 August 2017

Home Automation System - MQTT and RF433 to monitor photovoltaic production




Now I will try to integrate my RF433 Wireless Receiver (built on Arduino with Ethernet Shield) with MQTT, just to acquire my photovoltaic production data in my future Home Automation System

I don't write nothing more about my acquisition system; this is my old post about that:

http://domoticsduino.blogspot.com/2017/02/rf-433-mhz-data-sniffing.html

Now I would like to transmit data to my MQTT broker, for the future integration with OpenHAB

So, I have to edit the code of the Arduino sketch and let it to publish messages to the MQTT broker. In this time, the receiver publishes data to a HTTP REST Web Service

Well, using Arduino IDE (the same thing used for the SONOFF firmware upload), I open the sketch and edit the code.

I omit the technical details about the code because it's not the purpose of this post, but if you are interested in it, write me an email; I will be happy to share my code with you.

I chose the following MQTT topic:

arduino/groundfloor/rf433/solarenergy/state

I followed the same naming convention as the SONOFF :

[device]/[zone]/[type]/[data id]/state

The content of the message is simply the value of the production in Watt.

Here are the published messages, through mqtt-spy:



And here is the receiver:




Currently it's "mute", but the next step would be adding a led to monitor its status and eventually some environmental sensor...

Stay tuned...


Tuesday, 22 August 2017

Home Automation System - SONOFF and TASMOTA, small configuration error





Playing with MQTT and my SONOFF Basic, with TASMOTA firmware on board, I found a small mistake in the exchanged messages. In a few words, when I sent a MQTT message to power on or off the SONOFF (tasmota/garden/light/sonoffbasic01/cmnd/POWER ON), the device replied to me with the standard topic (stat/sonoffbasic01/RESULT) and not with the "fulltopic".

I immediately thought that the reason was a bug in the firmware and so I opened an "issue"on the GitHub page of TASMOTA.

In a few hours I found the solution, caused by a configuration error and not by a bug of the firmware.

In particular, I left the default value for "MqttClient" parameter, which assumed the same value as "Topic" parameter. For TASMOTA this is not OK. So I changed "MqttClient" parameter with a different value than "Topic" parameter and set mqtt-sonoffbasic01. Please, remember that this parameter must be unique in the entire system.

This is the link to the discussion:

https://github.com/arendst/Sonoff-Tasmota/issues/760

I must admit that in a few hours I found the solution to my problem, thanks to the big support of developer.

Theo Arends, THANK YOU VERY MUCH


Saturday, 19 August 2017

Home Automation System - SONOFF and MQTT together




I'm ready to test MQTT communication with my SONOFF Basic, also connected to a real button to light up my garden lamp.

I think it's important to be able to manage lights through standard buttons and switch, not only by Home Automation Software as openHAB and others, to avoid failures due to network problems.

So, I did another little hack to SONOFF: I soldered two little wire to its push button; these wires will be linked to a standard button on the wall.

Here are the wires connected to the button on the SONOFF


In this way, the standard button will activate the relays in the same way the button on the SONOFF or the APP will.

Last thing, to close the SONOFF with its case I have to make a little hole on the side to let the wires passing by:


Now, it's time to connect the SONOFF with the main power and link a standard button with the wires. In the INPUT pin I connected phase and neutral of the main power and in the OUTPUT pin I connected phase and neutral of the garden lamp.

Ok, after a little test all is working as expected. I pushed the button twice and lamp lighted up and down Thanks again to Piero who helped me to link the sonoff to the main power.

Now I can inspect the MQTT messages involved.

I use the web console, opening it through a internet browser, typing the ip address of the SONOFF in the address bar. Then, accessing the "Console" menù I can take a look at the MQTT messages.


The first thing I noticed is the telemetry messages, sended by device about every 10 seconds (this timeout is managed by the TELEPERIOD parameter); the message content is a JSON string, which contains many informations (POWER, the state of the relay is the most interesting one for me in this moment); the MQTT topic is tasmota/garden/light/sonoffbasic01/tele/STATE, following what I wrote in a previous post (http://domoticsduino.blogspot.com/2017/08/home-automation-system-sonoff-and-mqtt.html).

In the following screenshot, the marked lines show the MQTT messages sequence:


After I pushed the button for the first time. the relay went on the ON state and the system sent a MQTT message with topic tasmota/garden/light/sonoffbasic01/stat/RESULT, with content POWER: ON. This is the confirmation sent by the device; the command has been well understood. When the relay changed its state to ON, the device sent another MQTT message with topic tasmota/garden/light/sonoffbasic01/stat/POWER and value ON. Now the relay is active and the lamp is ON.

The next telemetry messages confirmed this too. The POWER variable now has the value ON.

Then I pressed the button for the second time: the relay changes its state to OFF and the system sent the following MQTT messages:
tasmota/garden/light/sonoffbasic01/stat/RESULT {POWER: OFF} and tasmota/garden/light/sonoffbasic01/stat/POWER OFF. Now the lamp is OFF.

We get the same behaviour using the TOGGLE button on the web interface.


or sending POWER ON and POWER OFF commands directly from the web console.


As last step, I powered on the lamp using the MQTT POWER ON message, using a standard MQTT client, connected to our broker.

Normally I use MQTT-SPY, a very useful java software which permits to send and receive MQTT messages.

My message must belong to the topic tasmota/garden/light/sonoffbasic01/cmnd/POWER and must have the ON value. The lamp will turn ON.