Rain Gauge code and ESP32 question

Code for "tipping bucket" Rain Gauge is not working on ESP32.

Trying to get jurs code from: Rain Gauge topic to work with an ESP32.

jurs code:

#define TENMINUTES (600*1000L) // ten minutes are 600000 milliseconds
#define REEDPIN 32  //changed for RSP32
#define REEDINTERRUPT 0

volatile int pulseCount_ISR;

void reedSwitch_ISR()
{
  static unsigned long lastReedSwitchTime;
  // debounce for a quarter second = max. 4 counts per second
  if (labs(millis()-lastReedSwitchTime)>250) 
  {
    pulseCount_ISR++;
    lastReedSwitchTime=millis();
  }
}

void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("Total\tCurrent");

  pinMode(REEDPIN,INPUT_PULLUP);
  attachInterrupt(REEDINTERRUPT,reedSwitch_ISR, FALLING);
}

unsigned long lastSecond,last10Minutes;
long lastPulseCount;
int currentPulseCount;

void loop() 
{
  // each second read and reset pulseCount_ISR
  if (millis()-lastSecond>=1000) 
  {
    lastSecond+=1000;
    noInterrupts();
    currentPulseCount+=pulseCount_ISR; // add to current counter
    pulseCount_ISR=0; // reset ISR counter
    interrupts();
    Serial.print(lastPulseCount);Serial.print('\t');Serial.print(currentPulseCount);
    Serial.println();
  }

  // each 10 minutes save data to another counter
  if (millis()-last10Minutes>=TENMINUTES) 
  {
    last10Minutes+=TENMINUTES; // remember the time
    lastPulseCount+=currentPulseCount; // add to last period Counter
    currentPulseCount=0;; // reset counter for current period
  }
}

Thank you jurs for providing your code in the referenced topic.

Just "0 0" in Serial Monitor; when executing jurs code on ESP32.

What needs to be done to work with ESP32?

Tried this:

#define FIVEMINUTES (300*1000L) // ten minutes are 600000 milliseconds
#define REEDPIN 32
#define REEDINTERRUPT 0 

volatile int pulseCount_ISR = 0;


portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;



void IRAM_ATTR reedSwitch_ISR()
{
  static unsigned long lastReedSwitchTime;
  // debounce for a quarter second = max. 4 counts per second
  if (labs(millis()-lastReedSwitchTime)>250) 
  {
    portENTER_CRITICAL_ISR(&mux);
    pulseCount_ISR++;
    
    lastReedSwitchTime=millis();
    portEXIT_CRITICAL_ISR(&mux);
  }
}

void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("Total\tCurrent");

  pinMode(REEDPIN,INPUT_PULLUP);
  attachInterrupt(REEDINTERRUPT,reedSwitch_ISR, FALLING);
}

unsigned long lastSecond,last5Minutes;
long lastPulseCount;
int currentPulseCount;

void loop() 
{
  // each second read and reset pulseCount_ISR
  if (millis()-lastSecond>=1000) 
  {
    lastSecond+=1000;
    portENTER_CRITICAL(&mux);
    //currentPulseCount+=reedSwitch_ISR(); // add to current counter
    pulseCount_ISR=0; // reset ISR counter
    portEXIT_CRITICAL(&mux);
    Serial.print(lastPulseCount);Serial.print('\t');Serial.print(currentPulseCount);
    Serial.println();
    
  }

  // each 5 minutes save data to another counter
  if (millis()-last5Minutes>=FIVEMINUTES) 
  {
    last5Minutes+=FIVEMINUTES; // remember the time
    lastPulseCount+=currentPulseCount; // add to last period Counter
    currentPulseCount=0;; // reset counter for current period
  }
}

Still only "0 0" in Serial Monitor.

Just starting to use ESP32 with interrupts.

William

I'm guessing you have the wrong pin. You should always map your desired pin to the interrupt like this:

attachInterrupt(digitalPinToInterrupt(GPIOPin), function, mode);

so you might try

attachInterrupt(digitalPinToInterrupt(REEDPIN),reedSwitch_ISR, FALLING);

Don't have an ESP32 so can't say for sure...

Doesn’t make a difference on ESP32:

#define digitalPinToInterrupt(p)    (((p)<40)?(p):-1)

Made following changes; as commented in source code, interrupt is working now.

#define FIVEMINUTES (60*1000L) // one minutes are 60000 milliseconds  <----changed for testing
#define REEDPIN 32
#define REEDINTERRUPT 0 

volatile int pulseCount_ISR = 0;


portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;



void IRAM_ATTR reedSwitch_ISR()
{
  static unsigned long lastReedSwitchTime;
  // debounce for a quarter second = max. 4 counts per second
  if (labs(millis()-lastReedSwitchTime)>250) 
  {
    portENTER_CRITICAL_ISR(&mux);
    pulseCount_ISR++;
    
    lastReedSwitchTime=millis();
    portEXIT_CRITICAL_ISR(&mux);
  }
}

void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("Total\tCurrent");

  pinMode(REEDPIN,INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(REEDPIN),reedSwitch_ISR, FALLING);  // <----- changed to this line

}

unsigned long lastSecond,last5Minutes;
long lastPulseCount;
int currentPulseCount;

void loop() 
{
  // each second read and reset pulseCount_ISR
  if (millis()-lastSecond>=1000) 
  {
    lastSecond+=1000;
    portENTER_CRITICAL(&mux);
    currentPulseCount+=pulseCount_ISR; // add to current counter  <--changed to this line
    pulseCount_ISR=0; // reset ISR counter
    portEXIT_CRITICAL(&mux);
    Serial.print("lastPulseCounter:  " + (String)lastPulseCount);Serial.print('\t');Serial.print("currentPulseCount:  " + (String)currentPulseCount);
    Serial.println();
    
  }

  // each 5 minutes save data to another counter
  if (millis()-last5Minutes>=FIVEMINUTES) 
  {
    last5Minutes+=FIVEMINUTES; // remember the time
    lastPulseCount+=currentPulseCount; // add to last period Counter
    currentPulseCount=0;; // reset counter for current period
  }
}

ESP32 External interrupts article

William

oqibidipo:
Doesn’t make a difference on ESP32:

#define digitalPinToInterrupt(p)    (((p)<40)?(p):-1)

But the original code had the pin defined as 32, but set the interrupt as REEDINTERUPT which was 0.

Code presented here is in initial development.

“Tipping bucket” rain gauge code for number of bucket dumps in one hour and one day written for use with ESP32. Initial testing appears to be accomplishing task correctly. Plan to integrate code into existing project; project topic: Internet Data Logger and Dynamic Web Server

“Internet Weather Data Logger and Dynamic Web Server” web site: Project website

William

Rain_Gauge_One.zip (1.36 KB)

Output Serial Monitor.txt (19.1 KB)