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
Showing posts with label firmware. Show all posts
Showing posts with label firmware. Show all posts

Saturday, 1 September 2018

Home Automation System - SONOFF 4 Channel integration with OpenHAB





In the previous post, I spoke about my SONOFF 4 channel, describing the physical installation.

Now, this device will help me to drive 4 lights: 1 internal and 3 exterior; of course, OpenHAB will be my control system and MQTT will be the message protocol.

Let's go to setup the SONOFF, using its web interface



The first step will be to setup the module type, using the menĂ¹ Configuration -> Configure Module



Then, in Configuration -> Configure MQTT, we will put the MQTT topic, splitted in TOPIC and FULLTOPIC as TASMOTA ask



In my case the device will use the following topic smarthome/sonoff4ch14/###. TASMOTA will replace ### with the type of message it will be sending (for example cmnd for commands, state for status information etc etc)

No more details on the other configuration options...they are the same as the other SONOFF

Now it's time for OpenHAB...we will add 4 items, one for each light.

Create a new file, called sonoff4ch.items, in the usual folder /etc/openhab2/items; in the file we will write the items configuration:


As you can see, MQTT topic is the same for all items. Only one thing changes...the property name, indexed by a number to indicate the relay number.

Finally, the last step: put the 4 items into a sitemap. So, write down 4 switch into the sitemap file

(these are the 3 items for exterior lights...)

And here is my complete sitemap:


Now let's push some buttons and see what happens.




Friday, 17 August 2018

Home Automation System - SONOFF 4 Channel




Finally I found a little time to continue my Home Automation System.

I already own a SONOFF 4 channel...I think the biggest one...


This device is able to drive 4 power outputs through a single Wifi connection. I will use it to power up my lights.

As well as input and output power connector,s it also has 4 pushbuttons which drive the 4 relays. Basically it's like 4 SONOFF Basic in a single device and with a single Wifi connection.

As usual I will use the TASMOTA firmware instead of the original one, which permits to use the device only through EWelink software. With TASMOTA firmware I'm able to easily integrate the device in OpenHAB using MQTT protocol.

Here is my SONOFF in flashing mode


As usual, it's easy to flash the firmware...just follow the official Wiki

Please, remember that this is an irreversible operation. You cannot recover the original firmware

In order to be able to control the SONOFF also through a traditional button, I decided to use the 4 pushbuttons integrated in the device... by connecting them appropriately to the buttons / wall switches ...

I never throw anything away and in this case a male / female old serial cable will be very useful to connect comfortably and plug and play the buttons / wall switches with the SONOFF buttons.

Thanks to Piero, we soldered the serial pins to the 4 pushbuttons on the SONOFF


getting the connector out of the SONOFF box


On the other side, we have an ethernet cable linked to the wall buttons


and we soldered to it the male serial connector


Ok, now it's time to configure the device to be integrated in OpenHAB through MQTT

Stay tuned...

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.items, in 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.

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:


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.


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.

******************************************************

A small update about a configuration error I found later

http://domoticsduino.blogspot.com/2017/08/home-automation-system-sonoff-and.html


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...

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 http://192.168.4.1 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...

VCC -> VCC
TX -> RX
RX -> TX
GND -> GND

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 :)