/!\ Vorläufig / Preliminary, work in progress /!\

Translate (Übersetzte) with Google

/!\ If you mouse click on a picture it will be expanded. You can go back to the web page with the back arrow of your web browser.

ioBroker-sonoffPOWR2

I wanted to visualize the collected energy of a little solar panel system (200 W).

The cost and effort should be low.

So, what components do I need for that, which play together?

  1. Hardware

    1. A computer (Linux server).

    2. An electric power (230 V) measuring device with WLAN connection, see sonoff POW R2 (ESP8266 device).

  2. Software which play together, with low effort.

    1. Operating system Linux, Ubuntu desktop, 64 Bit.

    2. Open source firmware for ESP8266 devices Tasmota.

    3. A data protocol to transfer the data, MQTT.

    4. A data broker, ioBroker.

    5. A time oriented database influxDB.

    6. A visualization software, Grafana

So, lets start with the hardware.

Hardware

Computer

My favorite is a Intel/AMD CPU laptop at home, which runs 24 hour.

  1. It must be possible to run Ubuntu 18.04/20.04 (64 bit).
    1. I am using a used Lenovo T430s laptop (with SSD) for about 160 EUR.

Solar Control Box

In order to measure the electric energy and to add a safety fuse, I build a Solar Control Box, see the photo below.

  1. The housing is a box with DIN Rail for 12 modules from amazon for about 12 EUR.

  2. The AC Energy monitor is Peacefair PZEM-022 for 230 VAC, with split core current measurement.

  3. The Safety switch is a 10 A fuse for 230 VAC.

  4. The Power measurement with WLAN connection is a Sonoff POWR2 with Tasmota firmware.

  5. The Sonoff POWR2 and the PZEM-022 modules are mounted on a 3D printed DIN rail case, V2.

attachment:Solar_Control_Box_IMG_2523.jpg

.

attachment:Sonoff-POWR2_Housing.jpg

Power Measurement

What fits good in this project is a sonoff POW R2 ESP8266 device, for about 20 EUR.

In order to deliver the power data directly to your computer you need an alternative software, open source firmware for ESP8266 devices Tasmota.

You can already buy a sonoff POWR2 device with the Tasmota software flashed on ebay for about 23 EUR. Unfortunately I entered wrong credentials for the access to the WLAN router, and it was not possible for me to restore the firmware to the initial state.

So I had to flash the sonoff POW R2 by myself.

Therefore you need an USB to TTL adapter with a supply voltage of 3.3 V for the flashing of the ESP8266.

For the firmware flashing you need about 300 mA at 3.3 V. Not all USB to TTL adapter can supply that.

So I bought an AMS117 3.3 V module on ebay for about 2 EUR which delivers maximum 800 mA. Because of the USB limitations it will be maximum 500 mA, so enough for flashing the ESP8266.

That 3.3 V regulator board I soldered below the USB to TTL adapter. See the picture below.

attachment:sonoff-POWR2_flashen_IMG_2481.jpg

.

.

.

.

The flashing is explained on the Tasmota homepage.

There is also a video (Sonoff Pow R2 mit Tasmota und ioBroker Einbindung) explaining it.

attachment:Sonoff-POWR2_Tasmotizer.jpg

/!\ If you have setup everything for flashing, check first the 3.3 V supply. If it does not work, swap the RXD/TXD lines on one side.

I have used the Python 3 software Tasmotizer from the Tasmota home page.

See on the right side the program window.

  1. Find out the communication port of the USB-TTL adapter. That depends on you operating system (Linux, MacOS, Windows).

Set a mark on Erase before flashing.

Then click on Tasmotize!. If it went OK, quit with a mouse click on button Quit.

Now lets show the configuration, which is difficult. One wrong paramter, and the chain will not work.

Configuration

attachment:Sonoff-POWR2_Tasmota_WiFi-parameters.jpg

attachment:Sonoff-POWR2_Tasmota_Configuration.jpg

After installation, briefly press the button on the Sonoff device 4 times. This will start the Wifi Manager and provide an access point with the IP address 192.168.4.1 and a web server that allows you to configure Wifi and MQTT parameters. You can then use your smartphone/tablet, etc. to access Sonoff's wireless network (wireless settings) and connect.

If Sonoff is in setup mode, you must connect to its access point. The best way to do this is to turn on the wireless in your phone. Go to the wireless network overview and look for an entry that begins with Sonoff-xxxx (xxxx = number). Select this hotspot, and route your web browser to this IP address.

You will see a WiFi paramter mask, as on the right screenshot.

Enter the SSID and password of your router for AP1. As a fail safe enter the same SSID also in field AP2, and leave the password field empty. Next enter a unique Hostname in your network and click on Save.

The sonoff device will do a software restart and tries to get per DHCP an IP-address from the router. Find the sonoff IP-address in your router and point the web browser to this IP-address. You will see the screenshot above for configuration.

attachment:Sonoff-POWR2_Tasmota_Module-parameters.jpg

attachment:Sonoff-POWR2_Tasmota_MQTT-parameter.jpg

Next click on Configure Module, see the screenshot on the right.

Tasmota has about 200 Module types on board. Here you have to select your own Module type Sonoff Pow R2 (43). Then click on Save.

Then click on Configure MQTT.

See the screenshot on the right.

In field Host you have to enter the IP-number from your host computer.

In the case of the software ioBrocker enter 1884, because 1883 is already in use.

In the field Client enter a suitable name for your sonoff application.

For security reason a user name and a password must here be given, which is also used in the ioBroker MQTT configuration.

At last a Topic name must be given.

Then click on Save.

attachment:Sonoff-POWR2_Tasmota_ON.jpg

Now you should see normal working screen.

In the center you see either ON or OFF.

If you click on button Console, you can see a log, which could help for debugging, especially for MQTT.

Button functions

The button on the Sonoff direct has different functionalities with the Tasmota firmware, depending on how often you press it in quick succession.

Important is ... to press it short and fast one after the other.

The button functionality has changed slightly with the different Tasmota versions. Please pay attention to the Tasmota version of your device!

Switching Sonoff on or off (press 1-2 times shortly)

By pressing the button once it can be toggled. The LED flashes 2x and sends an additional MQTT status.

If a device has a second channel (e.g. Sonoff Dual) this channel can be toggled by pressing the button twice. Again a MQTT status is sent.

WiFi Smartconfig mode (press 3 times briefly)

Press three times on the Sonoff to start the WiFi Smartconfig mode. This allows you to set the SSID and the corresponding password on an Android phone with the app ESP8266 Smartconfig. The MQTT server must still be configured in the user_config.h, if you compile (Arduino) the Tasmota software yourself. The LED flashes continuously during the process.

You can cancel the process by briefly pressing the button once.

WiFi Manager (press 4x briefly)

If you want to connect the device to your WLAN network, you can use the WiFi Manager. This is done after pressing the Sonoff four times. The device provides an access point under the IP 192.168.4.1. Once you connect to it, you can configure the wireless settings of your network under the IP (usually the browser with the IP is called automatically after connecting to the AP). MQTT parameters can also be defined here. The LED flashes continuously during the process.

Activate WPS mode (press briefly 5 times)

If you want to activate the WPS mode, you have to press the button 5 times. This starts the WPS mode to transfer the WLAN settings to the device. You have to start the WPS button on your router or on the surface of your router. The LED flashes continuously during this process.

You can cancel the process by pressing the button 1x briefly.

Restart Sonoff (press 6x briefly)

If you want to restart the relay, just press 6 times the button on the device to do so. All settings are retained!

OTA Download (press 7x briefly)

If you want to download the firmware via OTA, you have to press 7x briefly on the device. The LED lights up continuously during the process.

Reset Sonoff (depending on Tasmota version)

If there are any problems with the device, or if you want to reset the device to factory defaults, you can reset the Sonoff. All settings from user_config.h will be used. Depending on your Tasmota version you can do this as follows:

Data flow

In order to better understand how the data are processed until you get a chart over time, I will make a simple diagram.

Host 192.168.17.74  Ubuntu 18.04-64

TCP-Port
      1884                     8086        3000
sonoff - MQTT -> ioBroker -> influxDB -> Grafana -> time chart

      default 8888  8890->
 EBUS               <-8889            8086        3000
/dev/ttyUSB0 -> ebusd -> ioBroker -> influxDB -> Grafana -> time chart

Unfortunately on my Ubuntu server the default TCP_Port 8888 was already occupied. So, I changed it to 8890. The utility program ebusctl needs also the parameter -p 8890.

Software under Ubuntu

Install node.js

First install node.js

# Check for already installed
$ apt list --installed | grep nodejs
nodejs/now 14.9.0-deb-1nodesource1 amd64  [Installiert,aktualisierbar auf: 14.10.1-deb-1nodesource1]
# or
$ node -v
14.9.0  # actual for Ubuntu 18.04, 14.10.0 for Ubuntu 20.04)

# check node.js installer npm
$ npm -v
6.14.8   # actual for Ubuntu 18.04 and 20.04

# if not installed do:
$ curl -sLf https://deb.nodesource.com/current.x | sudo -E bash -
$ sudo apt-get install -y nodejs
$ node -v

Install ioBroker

$ curl -sLf https://iobroker.net/install.sh | bash -

Library version=2020-06-12
...
    ioBroker was installed successfully
    Open http://192.168.17.74 192.168.17.153:8081 in a browser and start configuring!

You need to re-login before doing anything else on the console!

Install influxDB

Next install influxDB, an open-source time series database, see here.

# for Ubuntu, in the terminal

$ wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -

$ source /etc/lsb-release

$ echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

$ sudo apt-get update

$ sudo apt install influxdb-client

# Test for daemon running, if ioBroker is already installed.
$ ps aux | grep influxd
iobroker  3156  0.1  1.5 937544 59724 ?  Sl   17:09   0:03 io.influxdb.0
influxdb  4429  0.1  1.0 1193480 41440 ? Ssl  17:23   0:03 /usr/bin/influxd -config /etc/influxdb/influxdb.conf

# Edit config file
$ nano /etc/influxdb/influxdb.conf
# after making a chang
$ sudo service influxdb restart

# influxdb shell
$ influx
Connected to http://localhost:8086 version 1.8.2
InfluxDB shell version: 1.8.2
> help
usage:
    connect <host:port>   connects to another node specified by host:port
    auth                  prompts for username and password
    pretty                toggles pretty print for the json format
    chunked               turns on chunked responses from server
    chunk size <size>     sets the size of the chunked responses.  
                          Set to 0 to reset to the default chunked size
    use <db_name>         sets current database
    format <format>       specifies the format of the server responses: json, csv, or column
    precision <format>    specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns
    consistency <level>   sets write consistency level: any, one, quorum, or all
    history               displays command history
    settings              outputs the current settings for the shell
    clear                 clears settings such as database or retention policy.  
                          run 'clear' for help
    exit/quit/ctrl+d      quits the influx shell

    show databases        show database names
    show series           show series information
    show measurements     show measurement information
    show tag keys         show tag key information
    show field keys       show field key information

    A full list of influxDB commands can be found at:
    https://docs.influxdata.com/influxdb/latest/query_language/spec/

Install Grafana

Last install Grafana, a multi-platform open source analytics and interactive visualization web application. Grafana gets the input data from influxDB.

# Installation under Ubuntu

$ sudo apt-get install -y adduser libfontconfig1

$ cd Install
$ wget https://dl.grafana.com/oss/release/grafana_7.1.5_amd64.deb
$ sudo dpkg -i grafana_7.1.5_amd64.deb

### You can start grafana-server by executing
$ sudo /bin/systemctl start grafana-server
oder
$ sudo service grafana-server start
or after a parameter change.
$ sudo service grafana-server restart

# Setup parameters
Edit 
$ sudo nano /etc/grafana/grafana.ini
protocol = http
http_port = 3000
# enable anonymous access
;enabled = false
enabled = true

# Call in a web browser (local network): http://192.168.17.161:3000 # OK 

Login: name: admin, password: admin, ENTER

Enter a new password.

Click on the + (button) on the left to create a new dashboard.

Configure influxDB

1. Configure instance influxDB, see the screen shot below.

attachment:ioBroker_influxDB_Adapterkonfiguration.jpg

.

Configure application

1. First you have to select the Adapter Sonoff, see below. Then click on the Info button

attachment:ioBroker_Sonoff_Adapter.jpg

.

2. Next click on the + (plus) button, in order to add to the Instances.

attachment:ioBroker_Sonoff_Adapter_Info.jpg

.

3. In the Adapterconfiguration you select the ioBroker IP-Address, the port number 1884 and a time limit of in this case 300 seconds. Last enter the Authetication values from the MQTT configuration of the Sonoff device, username, password, and repeat the password.

attachment:ioBroker_Sonoff_Adapterkonfiguration.jpg

.

4. In area Instances you sonoff.0 listed. The .0 is a counter, if you have several adapters of the same type. The green circle to the right tells, that the configuration and the data traffic are OK.

attachment:ioBroker_Sonoff_Instanzen.jpg

.

5. The objects are data points with some values. In the screen shot below I have shown all data points of the Sonoff values. If you want, that a value is stored in the data base influxDB you have to click the wrench in most right column of the value line. See the screen shot below.

attachment:ioBroker_Sonoff_Objekts.jpg

.

6. To the screen shot below: You have to click (activated) the point activated (Aktiviert). In order to keep the data volume small, you can mark store changes only (Nur Änderungen aufzeichnen). In order to debounce a signal you can define a debounce time, in my case 1000 ms.

In case you have very slowly changing values, you can define a minimum storage interval. In one case I set it to 300 s.

attachment:ioBroker_Sonoff_value_influxDB.jpg

.

Configure Grafana

1. Configure grafana visualization program.

influxDB is the default database, see the screen shot below.

attachment:Grafana_Configuration_influxDB.jpg

.

The default user is admin, and I did not change it.

In order to check the details of the influxDB parameters, below is the configuration mask:

attachment:Grafana_Data-Sources_influxDB.jpg

.

How to find the Dashboards, see below.

attachment:grafana_dashboards.jpg

.

To setup a new Dashboard (diagram) see the screen shot below.

Then edit all needed parameters.

attachment:Grafana_new_dashboard.jpg

.

attachment:Grafana_select_from_list.jpg

In order to make it more clear how to select the parameters to visualize, see the screen shot on the right.

When you click in the FROM field, a list drops down, showing all known measurement parameters (Data Points).

Then click on the wanted parameter, and if there are data values present, a curve in the diagram will be shown.

There is a little trap, if you have a measurement value, which does not change often (e.g. 30 min). When you start the diagram, there is only one value, and Grafana interprets that as No data.

In order to see a line you can set in field GROUP BY paramter fill from null to previous. Then you should see a line in the diagram.

.

.

How to edit the parameter, see below.

attachment:grafana_diagram_edit.jpg

.

How to add the right axis, see below.

attachment:Grafana_diagram_right-Y-axis.jpg

.

Grafana Power Diagram

Calculation of maximum solar power from www.electricitymap.org.

In our area the solar potential is read out from www.electricitymap.org about 600 W/m2.

The panel 100 W specification is achieved at 1000 W/m2.

I have about 1.2 m2 solar panel.

Maximum electric power seen: 135 W + 12% loss from inverter = 151 W 
Per m2 = 151 W / 1.2 m2 = 126 W/m2

So, the efficiency is about 126 W/m2 / 600 W/m2 = 21%

That looks realistic.

attachment:Grafana_sonoff-POWR2_solar_12h-cursor.jpg

.

attachment:Grafana_sonoff-POWR2_solar_12h-clouded.jpg

.

  1. Grafana

  2. Grafana is a multi-platform open source analytics and interactive visualization web application.

  3. influxDB.

  4. InfluxDB is an open-source time series database

  5. ioBroker

  6. Connect components for home automatizaion

  7. sonoff POW R2 (ESP8266 device)

  8. MQTT, Message Queuing Telemetry Transport, wikipedia

  9. MQTT

  10. node.js, JavaScript runtime environment,wikipedia

  11. npm, node package manager, wikipedia

  12. Tasmota

  13. Tasmota, available Modules

  14. Ubuntu desktop, 64 Bit

  15. #354 Tasmota vs ESPhome: Who wins? (DIY Sensors, ESP32, Deep-Sleep, etc.) Video Andreas Spiess

up to top

Contact Email, please enter your Email address

/!\ The entered Email address will not be published, or given away.

Comment this page
Name:
Comment:
Are you human?

List of pages in this category:

-- RudolfReuter 2020-09-14 04:36:46


Go back to CategoryHeizung or FrontPage

ioBroker-sonoffPOWR2 (last edited 2020-12-29 06:19:08 by RudolfReuter)