Project description
In my post “How to access a list of historic state values for an item?”, I tried to find a solution to connect a Hydreon solid state rain sensor RG-11, configured as a tipping bucket emulation, to openHAB with four use cases in mind:
- Count the tipping bucket impulses and sum them to get the total tipping bucket count.
- Scale the tipping button count with a factor to calculate the total amount of rain fallen since the counter was last started/reset. In my case the factor is 0.001 mm rain per tick.
- Calculate the amount of rain fallen for at least the last hour, the last day and the last month as derived values.
- With the first drops of rain alert with a rain alarm that can be used e.g. to close blinds or windows.
Inspired by also these two post:
I went now a different way.
I took the Hydreon solid state rain sensor RG-11 and configured it with mode #0, the tipping bucket emulation. In this mode the RG-11 outputs pulses of 50 mS per unit. The unit is a bucket size equivalent of either an imperial or metric size. In my case I choose the most sensitive value of 0.001 mm rain per pulse.
The pulses are fed to a nodeMCU V3 running ESPEasy release mega-20211005. There are two applications. One is just counting the pulses with a generic counter and outputs its values as deltas on each minute. The second application is taking the pulses in a 5 second window and if greater zero outputs an alarm flag indicating that it is raining. Both outputs are transmitted via MQTT to my openHAB instance.
The alarm is fed directly to a rain alarm item in openHAB. The pulses are fed to a “proxy” item for the raw tipping bucket deltas in openHAB. A rules monitors updates on the tipping bucket item and calculates the rain fallen by summing and scaling the deltas for 1h, 12h and 30 days.
Hardware
The rain sensor RG-11 needs a 12V DC power supply and gives his pules on a relay with two contacts. One is normally closed (NC) and the other is normally open (NO).
The pules are fed to a nodeMCU (ESPRESSIF SYSTEMS ESP8266 based microcontroller) on digital I/Os D5 and D6. Both are pulled up to VCC with an external resistor. I mounted that part on a small stripboard pcb.
The following diagram shows the electrical wiring
Fig. 1: Electrical wiring
Installation and configuration steps
ESPEasy
I’ve used a nodeMCU V3 board and flashed it with ESPEasy in the version mega-20211005, normal, with 4 MB size and 1 MB file syste size. So the file name I used to flash is in this case ESP_Easy_mega_20211005_normal_ESP8266_4M1M.bin
.
See the ESPEasy documentation on how to first time setup the controller with your credentials for your network.
Main configuration
- Go to the
Config
tab of the web interface and set the unit name. In my case it isregensensor
, the german word for rain sensor. This name is also used as the host and system name and affects logs and derived configurations. - If you have more than one rain sensors you can optionally set a unit number other than
0
. The unit number is appended to the unit name. - You can optionally restrict the access to the rain sensor by filtering using the
Client IP filtering
section. In my case I restricted access to my local net usingClient IP block level
with valueAlow Local Subnet
and entering allowed range withAccess IP lower range
andAccess IP upper range
limits. - Leave all other entries with the default and save your config by pressing the
Submit
button.
Fig. 2:
Config
page details
Controller configuration
A controller in ESPEasy is an something that can act on sensor readings, or more precisely events in the ESPEasy terminology. Mostly controllers are about communication to external systems. In my case I want to use a controller that can send my sensor readings to an MQTT instance connected to openHAB. Here are the necessary steps.
- Go to the
Controllers
tab of the web interface. - Press the
Add
button on the first controller row. - Choose the entry
Home Assistant (openHAB) MQTT
for the protocol used by this controller. - Choose either
Use IP address
orUse hostname
for the connection address to the MQTT broker. - Type in the address of the broker in the next field (
Controller IP
orController Hostname
). - Type in the port, usually 1883, for the MQTT broker.
- Leave the other settings to their default.
- Do not forget to enable the controller by checking
Enabled
at the bottom of the page. - Press the
Submit
button to confirm your settings. - Press the
Close
button to close the page.
Fig. 3, Controller configuration
Fig. 4, Result of a configured MQTT controller
Hardware configuration
There is nothing special about the hardware configuration and you can leave all entries to their defaults. Even if you want to extend your project with I2C devices, e.g. to measure temperature or humidity, the defaults should work. This is also the reason why I placed the two applications on the I/O pins D5 and D6.
Devices
On the Devices
tab you configure the two applications that are the main part of the functionality. A device is an active component that triggers some events or actions and can be connected to a controller.
You have to configure two devices. One device does the counting of the tipping bucket input and the other is responsible to give the alert when rain starts falling.
When opening the page you can add up do 12 devices or more precisely tasks, numbered 1 through 12 organized as a table layout. In the first column you can press the Add
button to add a task. Follow the steps outlined below to configure the first device:
- Select the device type from the selection box and set it to
Generic Pulse Counter
- The page changes now to the configuration of the counter
- Type in a name for your application. I used
regenkippwaage
here, which is the German word for a tipping bucket - Select the GPIO pin
GPIO-14 (D5)
that will connect to the pulse counter task. - Set the
Debounce Time (in Sec)
to 75. This is the best value I could find empirically to guarantee a good connection to the RG-11 sensor. - Set the
Counter Type
toDelta
. This means that the task counts pulses until it is sent to the controller and then resets the count to zero. I chose this value to have a very simple approach in open HAB to calculate the amount of fallen rain just be summing the delta pulses using thePersistenceExtension
. - Set the
Mode Type
toFalling
. That means that the pules are counted when the IO goes from high to low. This is due to the fact that the input pin is normally high, because of our pull up resistor, and changes to low for 50 mS on each pulse, because we use the normally open output of the RG-11 as shown in the electrical wiring shown above. - Enable the checkbox `Send to controller’ which is preset to controller #1. The controller we configured on the controllers tab.
- Set the
Interval
to 60 seconds. The task will then send every minute a message to MQTT. - The task will send the three values
count
,total
andtime
. I’ve set the decimals for the first two values to0
and to2
for the time. - Press the
Submit
button to confirm the device configuration.
Fig. 4, Device type selection
Fig. 5, Configuration of the tipping bucket task
The second task is configured nearly identical with the following exceptions:
- Change the name of the task to
regenerkennung
. This is the German word for rain detection. - Set the GPIO pin to
GPIO-12 (D6)
- Let the
Send to Controller
checkbox disabled. - Set the
Interval
to 5 seconds. This will assure that starting rain will be recognized within five seconds.
Now you should see the result as shown in the figure below.
Fig. 6, Configured tasks/devices
Enabling Rules.
The last part is to configure some rules that do the alerting part. In the default, the rules are disabled in ESPEasy and must be enabled. Also I configured an NTP time source that will allow me to see the correct timestamps in messages and logs. For that follow the steps outline below.
- Go to the
Tools
tab - Press the
Advanced
button in theSystem
section. - Check the
Rules
checkbox in theRules Settings
section. - Check the
Use NTP
checkbox in theNTP
section. - Enter a suitable
NTP Hostname
. In my case I use the German NTP pool0.de.pool.ntp.org
. See the NTP pool for your region or use the generic one0.pool.ntp.org
. - Enable the
DST
checkbox in theDST Settings
region. This will give you the time with respect to the daylight saving time. - Enter the offset of your time zone into
Timezone Offset (UTC +)
. This is the offset in minutes from your time zone to UTC without daylight time saving. For Germany, e.g., this value is 60 (UTC +1h) - Optionally you can set the
Latitude
andLongitude
values, if you need sunrise and sunset calculations. - Press the
Submit
button
Fig. 7, Advanced tool settings.
Rules
For the rain alerting on the first drops of rain, I use the ESPEasy rules system. Rules are short scripts that can be triggered on events or actions. Go to the Rules
tab and enter the following script into the editor field of the preselected Rule Set 1
:
on MQTT#Connected do
// Reset timer #1
timerSet,1,0
// Reset rain alarm flag
let,1,0
// Reset rain alarm
Publish %sysname%/regenalarm/state,0
endon
on Rules#Timer=1 do
// Reset rain alarm flag
let,1,0
// Reset rain alarm
Publish %sysname%/regenalarm/state,0
endon
on regenerkennung#Count>0 do
// Set or retrigger timer for 30 Minutes
timerSet,1,1800
// Check if rain alarm was already sent
if [int#1]=0
// Set rain alarm flag
let,1,1
// Set rain alarm
Publish %sysname%/regenalarm/state,1
endif
endon
If you want to use other names for regenerkennung
, etc. please adapt this script to your naming conventions. But keep in mind, that e.g. the value regenerkennung
must match the task name you assigned in the devices configuration. The name regenalarm
is the German word for rain alert and is used for the published topic name to openHAB. This you can choose freely to your own will.
Hints
That’s it for today. In a few days I will extend this article with the openHAB part. Please stay tuned until then.