Rain guage and anemometer together

Hi i need to measure rain and wind. But together this sketch

no working only measure rain and wind no. But why? I tryed change pins but unsuccessful.
… Thanks…

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

WidgetLCD lcd(V15);
const int rainGauge = 5; //D6  //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

String mymin = " minutes";
char auth[] = "";
char ssid[] = "";
char pass[] = "";   // Set password to "" for open networks.
SimpleTimer timer;




//wind
 const int hallPin=2;
 volatile byte half_revolutions;
 unsigned int rpm, ms, kmh, maxMs, h1_2, m1_2, h3_4, m3_4, h5_6, m5_6, h7_8, m7_8, h9_10, m9_10, h11, m11, h3, m3, d1_2, d3_4, d5_6, d7_8, d9_10, d11, d3;
 unsigned long  count1_2, count3_4, count5_6, count7_8, count9_10, count_11, count_3;
 unsigned int den = 86400;
 unsigned int hodina = 3600;
 unsigned int minuta = 60;
 String zacatekMereni = "20.02.2017";
// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
  //get data stored in virtual pin V0 from server
  Blynk.syncVirtual(V30);
}
// restoring counter from server
BLYNK_WRITE(V30){
  //restoring int value
  maxMs = param[0].asInt();
  count1_2 = param[1].asLong();
  count3_4 = param[2].asLong();
  count5_6 = param[3].asLong();
  count7_8 = param[4].asLong();
  count9_10 = param[5].asLong();
  count_11 = param[6].asLong();
  count_3 = param[7].asLong();
}



void setup()
{
  attachInterrupt(rainGauge, rain, RISING); //interrupt to catch pulses sent from rain gauge
  half_revolutions = 0;
   rpm = 0;
   ms = 0;
   kmh = 0;
   
  Serial.begin(9600); 
  
  
  pinMode(hallPin,INPUT);

  
  
  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



  


   timer.setInterval(3000L, mereniRPM); //kazdy tri vteriny vypocte ze signalu udaje
   timer.setInterval(600000L, zaloha); //kazdych 10 minut ulozi data na blynk server
   timer.setInterval(60000L, vypocet); //kazdou minutu vypocita udaje a posle na blynk

}

void zaloha(){
Blynk.virtualWrite(V30, maxMs, count1_2, count3_4, count5_6, count7_8, count9_10, count_11, count_3);
 }
 
void mereniRPM(){  
     rpm = (half_revolutions*20)/3;
     ms = half_revolutions/7;
     kmh = half_revolutions/2;
     
if (ms > maxMs) maxMs = ms;
if ((ms == 1)||(ms == 2)) count1_2 += 3;
if ((ms == 3)||(ms == 4)) count3_4 += 3;
if ((ms == 5)||(ms == 6)) count5_6 += 3;
if ((ms == 7)||(ms == 8)) count7_8 += 3;
if ((ms == 9)||(ms == 10)) count9_10 += 3;
if (ms >= 11) count_11 += 3;
if (ms >= 3) count_3 += 3;
     
//     Serial.println(half_revolutions,DEC); //pocet signalu za 3s. (3 signaly na otacku)
     Blynk.virtualWrite(V1, rpm);
     Blynk.virtualWrite(V2, ms);
     Blynk.virtualWrite(V3, kmh);
    // Blynk.virtualWrite(V4, maxMs);

     half_revolutions = 0;
}

void vypocet(){
    d1_2 = count1_2 / den;
    h1_2 = (count1_2 % den) / hodina;
    m1_2 = ((count1_2 % den) % hodina) / minuta;

    d3_4 = count3_4 / den;
    h3_4 = (count3_4 % den) / hodina;
    m3_4 = ((count3_4 % den) % hodina) / minuta;

    d5_6 = count5_6 / den;
    h5_6 = (count5_6 % den) / hodina;
    m5_6 = ((count5_6 % den) % hodina) / minuta;

    d7_8 = count7_8 / den;
    h7_8 = (count7_8 % den) / hodina;
    m7_8 = ((count7_8 % den) % hodina) / minuta;

    d9_10 = count9_10 / den;
    h9_10 = (count9_10 % den) / hodina;
    m9_10 = ((count9_10 % den) % hodina) / minuta;

    d11 = count_11 / den;
    h11 = (count_11 % den) / hodina;
    m11 = ((count_11 % den) % hodina) / minuta;

    d3 = count_3 / den;
    h3 = (count_3 % den) / hodina;
    m3 = ((count_3 % den) % hodina) / minuta;

  if (m1_2 < 10) Blynk.virtualWrite(V5, d1_2, " d  ", h1_2, ":0", m1_2);
  else Blynk.virtualWrite(V5, d1_2, " d  ", h1_2, ":", m1_2);
  
  if (m3_4 < 10) Blynk.virtualWrite(V6, d3_4, " d  ", h3_4, ":0", m3_4);
  else Blynk.virtualWrite(V6, d3_4, " d  ", h3_4, ":", m3_4);
    
  if (m5_6 < 10) Blynk.virtualWrite(V7, d5_6, " d  ", h5_6, ":0", m5_6);
  else Blynk.virtualWrite(V7, d5_6, " d  ", h5_6, ":", m5_6);
    
  if (m7_8 < 10) Blynk.virtualWrite(V8, d7_8, " d  ", h7_8, ":0", m7_8);
  else Blynk.virtualWrite(V8, d7_8, " d  ", h7_8, ":", m7_8);
    
  if (m9_10 < 10) Blynk.virtualWrite(V9, d9_10, " d  ", h9_10, ":0", m9_10);
  else Blynk.virtualWrite(V9, d9_10, " d  ", h9_10, ":", m9_10);
    
  if (m11 < 10) Blynk.virtualWrite(V10, d11, " d  ", h11, ":0", m11);
  else Blynk.virtualWrite(V10, d11, " d  ", h11, ":", m11);
    
  if (m3 < 10) Blynk.virtualWrite(V11, d3, " d  ", h3, ":0", m3);
  else Blynk.virtualWrite(V11, d3, " d  ", h3, ":", m3);

  Blynk.virtualWrite(V12, zacatekMereni);
}

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

void sendUptime()
  {
    
    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
      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");    
    
    }
  }


void loop()
{
  Blynk.run();
  timer.run();
}
void rpm_fun()
 {
   half_revolutions++;
 }


This is your third topic on this project :thinking: mostly us troubleshooting your bad code or wiring… no real Blynk relevant issues.

What have you tried on your end for troubleshooting your latest issue?

1 Like

You have no interrupt attached to your “hallPin”, so no matter how many pulses the anemometer is sending to your board, the code won’t do anything with them.

As I said before, you’ll need to manage your interrupt priority very carefully.
Pulses from your rain gauge will come much less frequently than from your anemometer. Missing one or two anemometer pulses is no big deal when its lashing it down with rain and blowing a gale, but a missed rain gauge pulse will make a significant difference to your stats. If you don’t believe me then put your weather station outside when its raining and see how frequent the wind pulses are compared to the rain pulses.

With two sources of interrupts then you’re probably going to have to use detachInterrupt() for the anemometer while you’re processing then rain gauge interrupt, then re-attach it again afterwards. This will ensure that your range gauge data is processed without being interrupted (see what I did there :stuck_out_tongue_winking_eye:) by a pulse from your anemometer.
The processing time of the void rain() function may seem insignificant at the moment, but when do finally realise that you wan’t to do more with this data than your current reset to zero at midnight then the processing time will increase.
Once you start to add-in wind direction, pressure, humidity and temperature readings then it becomes even more important to have your code well structured and your interrupts managed properly.

However, you seem to have ignored every piece of coding advice I’ve given you so far with this project so we’ll see what you do with this bit of guidance.

Pete.

2 Likes

Sorry but my latest problem is solved: problem was in bad used pin.
Now i founded new issues because i was think it is clearer and it is different problem…

Thanks. But i dont know how i am no good coder. So i used two ESP8266 devkit. One for anemometer and SI7021 and twice for rain guage. It is working. Only i dont know for start measurement wind i always have to click on the serial monitor in Arduino 1.6.5? 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>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

#include <SI7021.h>

float temp, hum;
bool SI7021_present = true;

#define I2C_SCL 4   //D2   // Barometric Pressure Sensor (BMP085)
#define I2C_SDA 5   //D1
SI7021 sensor;

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

 const int hallPin=3;
 volatile byte half_revolutions;
 unsigned int rpm, ms, kmh, maxMs, h1_2, m1_2, h3_4, m3_4, h5_6, m5_6, h7_8, m7_8, h9_10, m9_10, h11, m11, h3, m3, d1_2, d3_4, d5_6, d7_8, d9_10, d11, d3;
 unsigned long  count1_2, count3_4, count5_6, count7_8, count9_10, count_11, count_3;
 unsigned int den = 86400;
 unsigned int hodina = 3600;
 unsigned int minuta = 60;
 String zacatekMereni = "20.02.2017";

 SimpleTimer timer;

// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
  //get data stored in virtual pin V0 from server
  Blynk.syncVirtual(V30);
}

// restoring counter from server
BLYNK_WRITE(V30){
  //restoring int value
  maxMs = param[0].asInt();
  count1_2 = param[1].asLong();
  count3_4 = param[2].asLong();
  count5_6 = param[3].asLong();
  count7_8 = param[4].asLong();
  count9_10 = param[5].asLong();
  count_11 = param[6].asLong();
  count_3 = param[7].asLong();
}



void setup(){
   pinMode(hallPin,INPUT);
  //Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
 attachInterrupt(digitalRead(hallPin), rpm_fun, RISING);
   half_revolutions = 0;
   rpm = 0;
   ms = 0;
   kmh = 0;

   timer.setInterval(3000L, mereniRPM); //kazdy tri vteriny vypocte ze signalu udaje
   timer.setInterval(600000L, zaloha); //kazdych 10 minut ulozi data na blynk server
   timer.setInterval(60000L, vypocet); //kazdou minutu vypocita udaje a posle na blynk
timer.setInterval(50000L, sendUptime);
sensor.begin(SDA,SCL);
  Wire.begin(I2C_SDA, I2C_SCL);
 
}



void zaloha(){
Blynk.virtualWrite(V30, maxMs, count1_2, count3_4, count5_6, count7_8, count9_10, count_11, count_3);
 }
 
void mereniRPM(){  
     rpm = (half_revolutions*20)/3;
     ms = half_revolutions/7;
     kmh = half_revolutions/2;
     
if (ms > maxMs) maxMs = ms;
if ((ms == 1)||(ms == 2)) count1_2 += 3;
if ((ms == 3)||(ms == 4)) count3_4 += 3;
if ((ms == 5)||(ms == 6)) count5_6 += 3;
if ((ms == 7)||(ms == 8)) count7_8 += 3;
if ((ms == 9)||(ms == 10)) count9_10 += 3;
if (ms >= 11) count_11 += 3;
if (ms >= 3) count_3 += 3;
     
//     Serial.println(half_revolutions,DEC); //pocet signalu za 3s. (3 signaly na otacku)
     Blynk.virtualWrite(V1, rpm);
     Blynk.virtualWrite(V2, ms);
     Blynk.virtualWrite(V3, kmh);
     Blynk.virtualWrite(V4, maxMs);


     half_revolutions = 0;
}

void vypocet(){
    d1_2 = count1_2 / den;
    h1_2 = (count1_2 % den) / hodina;
    m1_2 = ((count1_2 % den) % hodina) / minuta;

    d3_4 = count3_4 / den;
    h3_4 = (count3_4 % den) / hodina;
    m3_4 = ((count3_4 % den) % hodina) / minuta;

    d5_6 = count5_6 / den;
    h5_6 = (count5_6 % den) / hodina;
    m5_6 = ((count5_6 % den) % hodina) / minuta;

    d7_8 = count7_8 / den;
    h7_8 = (count7_8 % den) / hodina;
    m7_8 = ((count7_8 % den) % hodina) / minuta;

    d9_10 = count9_10 / den;
    h9_10 = (count9_10 % den) / hodina;
    m9_10 = ((count9_10 % den) % hodina) / minuta;

    d11 = count_11 / den;
    h11 = (count_11 % den) / hodina;
    m11 = ((count_11 % den) % hodina) / minuta;

    d3 = count_3 / den;
    h3 = (count_3 % den) / hodina;
    m3 = ((count_3 % den) % hodina) / minuta;

  if (m1_2 < 10) Blynk.virtualWrite(V5, d1_2, " d  ", h1_2, ":0", m1_2);
  else Blynk.virtualWrite(V5, d1_2, " d  ", h1_2, ":", m1_2);
  
  if (m3_4 < 10) Blynk.virtualWrite(V6, d3_4, " d  ", h3_4, ":0", m3_4);
  else Blynk.virtualWrite(V6, d3_4, " d  ", h3_4, ":", m3_4);
    
  if (m5_6 < 10) Blynk.virtualWrite(V7, d5_6, " d  ", h5_6, ":0", m5_6);
  else Blynk.virtualWrite(V7, d5_6, " d  ", h5_6, ":", m5_6);
    
  if (m7_8 < 10) Blynk.virtualWrite(V8, d7_8, " d  ", h7_8, ":0", m7_8);
  else Blynk.virtualWrite(V8, d7_8, " d  ", h7_8, ":", m7_8);
    
  if (m9_10 < 10) Blynk.virtualWrite(V9, d9_10, " d  ", h9_10, ":0", m9_10);
  else Blynk.virtualWrite(V9, d9_10, " d  ", h9_10, ":", m9_10);
    
  if (m11 < 10) Blynk.virtualWrite(V10, d11, " d  ", h11, ":0", m11);
  else Blynk.virtualWrite(V10, d11, " d  ", h11, ":", m11);
    
  if (m3 < 10) Blynk.virtualWrite(V11, d3, " d  ", h3, ":0", m3);
  else Blynk.virtualWrite(V11, d3, " d  ", h3, ":", m3);

 Blynk.virtualWrite(V12, zacatekMereni);
}
void sendUptime()
{

  
   

float temperature = sensor.getCelsiusHundredths()/100;
int humidity = sensor.getHumidityPercent();


Blynk.virtualWrite(21, temperature); // virtual pin
Blynk.virtualWrite(22, humidity); // virtual pin
  
}


void loop(){
  Blynk.run();
  timer.run(); // Initiates SimpleTimer

 }
void rpm_fun()
{
  half_revolutions++;
 }