Optical rain gauge sensor connected to openHAB with rain fall calculation for 1h, 24h and 30 days

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:

  1. Count the tipping bucket impulses and sum them to get the total tipping bucket count.
  2. 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.
  3. Calculate the amount of rain fallen for at least the last hour, the last day and the last month as derived values.
  4. 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

  1. Go to the Config tab of the web interface and set the unit name. In my case it is regensensor, the german word for rain sensor. This name is also used as the host and system name and affects logs and derived configurations.
  2. 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.
  3. 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 using Client IP block level with value Alow Local Subnet and entering allowed range with Access IP lower range and Access IP upper range limits.
  4. 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.

  1. Go to the Controllers tab of the web interface.
  2. Press the Add button on the first controller row.
  3. Choose the entry Home Assistant (openHAB) MQTT for the protocol used by this controller.
  4. Choose either Use IP address or Use hostname for the connection address to the MQTT broker.
  5. Type in the address of the broker in the next field (Controller IP or Controller Hostname).
  6. Type in the port, usually 1883, for the MQTT broker.
  7. Leave the other settings to their default.
  8. Do not forget to enable the controller by checking Enabled at the bottom of the page.
  9. Press the Submit button to confirm your settings.
  10. 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:

  1. Select the device type from the selection box and set it to Generic Pulse Counter
  2. The page changes now to the configuration of the counter
  3. Type in a name for your application. I used regenkippwaage here, which is the German word for a tipping bucket
  4. Select the GPIO pin GPIO-14 (D5) that will connect to the pulse counter task.
  5. 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.
  6. Set the Counter Type to Delta. 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 the PersistenceExtension.
  7. Set the Mode Type to Falling. 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.
  8. Enable the checkbox `Send to controller’ which is preset to controller #1. The controller we configured on the controllers tab.
  9. Set the Interval to 60 seconds. The task will then send every minute a message to MQTT.
  10. The task will send the three values count, total and time. I’ve set the decimals for the first two values to 0 and to 2 for the time.
  11. 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:

  1. Change the name of the task to regenerkennung. This is the German word for rain detection.
  2. Set the GPIO pin to GPIO-12 (D6)
  3. Let the Send to Controller checkbox disabled.
  4. 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.

  1. Go to the Tools tab
  2. Press the Advanced button in the System section.
  3. Check the Rules checkbox in the Rules Settings section.
  4. Check the Use NTP checkbox in the NTP section.
  5. Enter a suitable NTP Hostname. In my case I use the German NTP pool 0.de.pool.ntp.org. See the NTP pool for your region or use the generic one 0.pool.ntp.org.
  6. Enable the DST checkbox in the DST Settings region. This will give you the time with respect to the daylight saving time.
  7. 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)
  8. Optionally you can set the Latitude and Longitude values, if you need sunrise and sunset calculations.
  9. 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.

6 Likes

Sounds very interesting. I would like to have a look at the setup and configuration. The information if and how much it rains would be helpful for me.
I would be happy if you can add it.

Hey Dadman,

I’ve edited my post and added the ESPEasy configuration. The openHAB part will follow in a few days.

Thanks a lot! I will need some time to read it.

We would like to see the openhab application if possible