Chat freely about anything...

User avatar
By Sirquil
#82587 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:

Code: Select all#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 for jurs code to work with ESP32?

Tried this:

Code: Select all#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
Last edited by Sirquil on Sat Oct 12, 2019 3:11 pm, edited 2 times in total.
User avatar
By Sirquil
#82594 Made following changes; commented in source code, interrupt works now.

Code: Select all#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
Last edited by Sirquil on Sat Oct 12, 2019 3:14 pm, edited 1 time in total.
User avatar
By Sirquil
#82636 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
Attachments
Serial Monitor output from project
(19.06 KiB) Downloaded 423 times
ESP32 project using Rain Gauge.rrupt
(3.24 KiB) Downloaded 510 times
Last edited by Sirquil on Sat Oct 12, 2019 3:15 pm, edited 1 time in total.
User avatar
By Sirquil
#82742 Update 6/8/2019

Current version has rain5min, rainHour, and rainDay working correctly.  rain5min "bucket" dumps are counted during 5 minutes period; rainHour and rainDay both update at 5 minute period.  rainHour resets to zero every hour.  rainDay resets to zero at Midnight.

Project includes the previous "Internet Weather Data Logger and Dynamic Web server; two web sites in one sketch, now with "Tipping Bucket" Rain Gauge functionality!

Attachment includes sketch, dataCollector.php, and readme file,

There is a print statement; easily commented out, that was used for seeing each interrupt as it is triggered.

Project was developed using an "Adafruit," Espressif ESP32 Development Board - Developer Edition, PRODUCT ID: 3269 a GY-BME280 breakout board, "NEO 8 Mn" GPS Module, and a "Tipping Bucket" Rain Gauge.

William
Attachments
Current project files
(54.35 KiB) Downloaded 597 times
Last edited by Sirquil on Sat Oct 12, 2019 3:15 pm, edited 1 time in total.