Rain gauge - ESP8266 Devkit - Blynk

Hi. I find a good sketch for measurement Rain . I have rain gauge for station WH1080.
Thanks.
sSRAZKY

The principal is the same as the anemometer that you’ve posted about in another thread.
When the bucket tips it creates a pulse which you can detect via an interrupt on a pin.

Obviously the anemometer creates many pulses per second when the wind speed is high, but the pulses from the rain gauge are much less frequent, even when it’s raining very heavily.

One tip of the bucket equals 0.2794 mm of rain. You need to decide what rainfall statistics you’re wanting to measure - Rainfall in the past 60 minutes, 24 hours, week, month etc. then create variables to hold this data and routines to throw away the old data that you no longer need. An array with 60 elements that hold the last 60 minutes worth of rainfall data is the way that I do it. Same for the past 24 hours worth of rainfall.

The problem you’ll have is monitoring these interrupts as well as those for your anemometer, as well as maintaining the Blynk heartbeat.

Missing one pulse from the anemometer isn’t a big deal, but missing one tip of the rain gauge would be a problem so you need to prioritise the rain gauge interrupt processing over the anemometer.

Pete.

2 Likes

And this sketch is possible?
Thanks

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>


const int rainGauge = 12;   //Pin for rain gauge which has a resolution of .15mm

float rainAmount = .3;     //variable to hold .3mm when bucket tips
float totalAmount;          //variable to hold total amount of rain that has fallen

String mymin = " minutes";

char auth[] = "XXXXXXXXXXXXX";
char ssid[] = "NotTelling";  // Your WiFi credentials.
char pass[] = "NorHere";     // Set password to "" for open networks.

SimpleTimer timer;


void setup()
{
  attachInterrupt(rainGauge, rain, RISING); //interrupt to catch pulses sent from rain gauge
  Serial.begin(9600); 
   
  Blynk.begin(auth, ssid, pass);
  while (Blynk.connect() == false) { 
  // Wait until connected
  }
    pinMode(rainGauge, INPUT_PULLUP);
    
    timer.setInterval(1000L,rainupDate);    // set interval (1sec) to read rain gauge totals and send to Blynk app
}
void rainupDate(){
  Blynk.virtualWrite(V13,totalAmount); // send rain amount to Blynk app
}

void sendAllData(){  // this function sends all sensor data to Blynk app
   
      
  }


void sendUptime()
  {

  void rain(){
      int buttonState = digitalRead(rainGauge);
      if(buttonState==0){  //it is raining
      digitalWrite (ledPin,HIGH);   //LED for visual feedback that pulses from rain gauge are being received
      totalAmount += rainAmount; //add up the total amount of rain that has fallen and put it in variable totalAmount
      Serial.print("this is the total rain that has fallen ");  //Print out to serial monitor
      Serial.print(totalAmount); 
      Serial.println(" mm");    
      digitalWrite(ledPin,LOW);
    }
 

 }
  
void loop()
{
  Blynk.run();
  timer.run();
}
1 Like

You’d need to try it and see what results you get. It looks rather odd to me with one function inside another:

and no way to reset the rainfall amount.

Pete.

So i tryed this sketch:

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example runs directly on ESP8266 chip.
 *
 * You need to install this for ESP8266 development:
 *   https://github.com/esp8266/Arduino
 *
 * Please be sure to select the right ESP8266 module
 * in the Tools -> Board menu!
 *
 * Change WiFi ssid, pass, and Blynk auth token to run :)
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>

#include "DHT.h"
#include <RCSwitch.h>
#define DHTPIN 10
#define DHTTYPE DHT22

DHT dht(DHTPIN,DHTTYPE,11);
RCSwitch mySwitch = RCSwitch();
WidgetLCD lcd(V11);
const int rainGauge = 12;   //Pin for rain gauge which has a resolution of .15mm
const int ledPin = 4;
float rainAmount = .3;     //variable to hold .3mm when bucket tips
float totalAmount;          //variable to hold total amount of rain that has fallen
long rssi;  //radio signal strength indicator
String mymin = " minutes";

char auth[] = "";
char ssid[] = "";
char pass[] = "";
SimpleTimer timer;


void setup()
{
  attachInterrupt(rainGauge, rain, RISING); //interrupt to catch pulses sent from rain gauge
  Serial.begin(9600); 
  Wire.begin(2,14);  //I2C pins for barometer
  
  dht.begin();
  mySwitch.enableTransmit(0); // Transmitter is connected to ESP8266 Pin #0
  Blynk.begin(auth, ssid, pass);
  while (Blynk.connect() == false) { 
  // Wait until connected
  }
    pinMode(ledPin, OUTPUT);
    pinMode(rainGauge, INPUT_PULLUP);
    
    timer.setInterval(1000L, sendUptime);   // set interval and function to call
    timer.setInterval(1000L,rainupDate);    // set interval (1sec) to read rain gauge totals and send to Blynk app
}

void rainupDate(){
  Blynk.virtualWrite(V13,totalAmount); // send rain amount to Blynk app
}

void sendUptime()
  {
    // This function sends Arduino up time every 1 second to Virtual Pin (V4)
    // In the app, Widget's reading frequency should be set to PUSH
    // You can send anything with any interval using this construction
    // Don't send more that 10 values per second
    Blynk.virtualWrite(V4, millis() / 1000);
    lcd.print(0,0,"Time connected");
    lcd.print(0,1, millis()/1000/60 + mymin);
  }

  void rain(){
      int buttonState = digitalRead(rainGauge);
      if(buttonState==0){  //it is raining
      digitalWrite (ledPin,HIGH);   //LED for visual feedback that pulses from rain gauge are being received
      totalAmount += rainAmount; //add up the total amount of rain that has fallen and put it in variable totalAmount
      Serial.print("this is the total rain that has fallen ");  //Print out to serial monitor
      Serial.print(totalAmount); 
      Serial.println(" mm");    
      digitalWrite(ledPin,LOW);
    }
  }

BLYNK_WRITE(V12){  // this function returnes WiFi signal strength
  rssi = WiFi.RSSI();
  int pinData = param.asInt(); 
  if(pinData==1)
  {
    lcd.clear();
    lcd.print(0 ,0,"Wifi Strength");   
    lcd.print(0 ,1,rssi);
  }
 }

 bool isFirstConnect = true; // Keep this flag not to re-sync on every reconnection

  // This function will run every time Blynk connection is established
  BLYNK_CONNECTED() 
  {
    if (isFirstConnect) 
    {
      // Request Blynk server to re-send latest values for all pins
//      Blynk.syncAll();
      // You can also update an individual Virtual pin like this:
      Blynk.syncVirtual(V0);
      Blynk.syncVirtual(V1);
      Blynk.syncVirtual(V2);
      Blynk.syncVirtual(V3);
      Blynk.syncVirtual(V4);
      Blynk.syncVirtual(V11);
      Blynk.syncVirtual(V13);
      isFirstConnect = false;
    }
  }
  

void loop()
{
  Blynk.run();
  timer.run();
}

/*Remote control of switches
-------------------------------------------------*/

  BLYNK_WRITE(V5){
    mySwitch.send(14331050,24); //turn on switch 3A
  }
  BLYNK_WRITE(V6){
    mySwitch.send(14331042,24); //turn off switch 3A
  }
  BLYNK_WRITE(V7){
    mySwitch.send(14331052,24); //turn on switch 2A
  }
  BLYNK_WRITE(V8){
    mySwitch.send(14331044,24); //turn off switch 2A
  }
  BLYNK_WRITE(V9){
    mySwitch.send(354206,24);  //turn on switch 1B
  }
  BLYNK_WRITE(V10){
    mySwitch.send(354198,24); //turn off switch 1B
  }

Is it good for measurement rain?

THanks.

I guess it depends on what you’re trying to achieve. As I said before, I want to see rainfall in millimetres over various time periods - the last minute, rolling 60 minutes, rolling 24 Hours etc.
For that reason I use arrays and write the values into the arrays. Take the rolling 60 minute total as an example of how I do this…
I use a variable to store the rolling 60 minute total and a pointer to tell me which array element to write into.
Once I’ve run for 1 hour, all the array element are populated with data and I have a value for the total rainfall in the past 60 minutes. My pointer tells me I’m going to write the next minute’s worth of data into the first array element (position 0). First I read the value that’s stored in that location and deduct it from my 60 minute rolling total. I then add the latest 1 minute value to my rolling total, and write that value into the position that my pointer points to (position 0).
This way, my rolling total always shows me how much rain has fallen in the past 60 minutes. I don’t have to keep resetting the value, it automatically does it for me.
After one hour (when my pointer says it’s at position 59) I write the 60 minute rolling total value into the 24 hour rolling total array.
If I wanted to I could keep cascading his and keep weekly, monthly and annual rolling totals (I don’t by the way).

Your code just keeps counting and it seems that the only way of zeroing the total is to reset the MCU.

Also, you seem to have some part-implemented DHT22 pressure/humidity sensor code kicking around in your sketch. If you plan to run this at the same time then be careful how often you interrogate the DHT22 sensor they don’t like to be read more frequently than every 5 or 10 seconds. If you’re not planning to use this code then you should delete it from your sketch.

At the end of the day it depends on what you’re trying to achieve with your rainfall statistics and you’ve not shared that vision yet.

Pete.

1 Like

@PeteKnight

The code @Jiri_Bam is showing is mine and zeros the rain amount at midnight every night via the Eventor widget. Has been working faultlessly with few modifications for quite a while now.

ok thanks but i dont know how i have setting the Eventor widget for zeros every night? Thanks.

probably with a daily timer in the Eventor.

image

1 Like

Hi. And your sketch have count every fall paddle ? Me no good working sometimes no count…


So problem was in pin. I change pin and now is all good.