Rain gauge code

Hello friends, I have this code to read the data of a rain gauge, what it does is count the times that a magnet passes in front of a reed switch and calculates the fallen L / m2.
Can you help me make a code that works with Cayenne connected via MQTT?

const int SENSOR=D6;
const int umbraltiempo=300;
volatile int ISRContador = 0;
int contador = 0;
float litros=0;
long tiempocontador=0;
long tiempohora =0;

void setup(){
pinMode(SENSOR, INPUT_PULLUP);
Serial.begin(9600);
attachInterrupt(SENSOR,contadorLitros,FALLING);
}

void contadorLitros()
{
if(millis()>(tiempocontador+umbraltiempo))
{
ISRContador++;
tiempocontador=millis();
}
}

void loop() {
if (millis()<tiempohora)
{
tiempohora=0;
}
if (millis()-tiempohora>3600000UL) {
tiempohora = millis();
litros=ISRContador * 0.5;
Serial.print("Litros por m2 caidos en un minuto (L/m2): ");
Serial.println(litros);
ISRContador= 0;
contador= 0;
}
}

which device are you using to connect to internet. we have couple of example for different types of boards and devices. Select the appropriate code and add your rain gauge sensor code into it. Then you send the sensor data using Cayenne.virtualWrite(3, litros, "rain_level", "cm");

1 Like

I advanced until I had this code:

// This example shows how to connect to Cayenne using an ESP8266 and send / receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.

// # define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

const int SENSOR = 2;
const int thresholdtime = 300;
volatile int ISRContador = 0;
int counter = 0;
float liters = 0;
long counting time = 0;
long timehora = 0;

// WiFi network info.
char ssid = “Map”;
char wifiPassword = “456977”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “5a3596-8221-599f77add412”;
char password = “9a4f400584a42a4f47f840e68648”;
char clientID = “53921-599f77add412”;

unsigned long lastMillis = 0;

void setup () {
pinMode (SENSOR, INPUT_PULLUP);
Serial.begin (9600);
Cayenne.begin (username, password, clientID, ssid, wifiPassword);

attachInterrupt (SENSOR, counterLiters, FALLING);
}

void counterLiters ()
{
if (millis ()> (counter time + time threshold))
{
ISR Counter ++;
counting time = millis ();
}
}

void loop () {
Cayenne.loop ();

if (millis () <timetime)
{
timetime = 0;
}
if (millis () - tiempohora> 3600000UL) {
timehora = millis ();
liters = ISR Counter * 0.5;
Serial.print (“Liters per m2 dropped in one minute (L / m2):”);
Serial.println (liters);
ISR Counter = 0;
counter = 0;

Cayenne.virtualWrite (3, liters, “rain_level”, “cm”);
}
}

First I would like to know if it is well raised. I use ESP8266 - 07.
In the Serial Monitor I am informed of the error “ISR not in IRAM!”
From what I see it has to do with the handling of interruptions.
It happens that I have no idea if the code is correct and that is why the problem.
I will appreciate your attention.

can you provide a link to the device which you are using?

You mean this? - Cayenne

or this

Do not create mutiple post on same topic. You can continue to ask all your issue here.
try this code:

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

char ssid[] = "";
char wifiPassword[] = "";

char username[] = "";
char password[] = "";
char clientID[] = "";

const int SENSOR = D6;
const int umbraltiempo = 300;
volatile int ISRContador = 0;
int contador = 0;
float litros = 0;
long tiempocontador = 0;
long tiempohora = 0;

void setup() {
  pinMode(SENSOR, INPUT_PULLUP);
  Serial.begin(9600);
  attachInterrupt(SENSOR, contadorLitros, FALLING);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

}

void contadorLitros()
{
  if (millis() > (tiempocontador + umbraltiempo))
  {
ISRContador++;
tiempocontador = millis();
  }
}

void loop() {
Cayenne.loop();

  if (millis() < tiempohora)
  {
tiempohora = 0;
  }
  if (millis() - tiempohora > 3600000UL) {
tiempohora = millis();
litros = ISRContador * 0.5;
Serial.print("Litros por m2 caidos en un minuto (L/m2): ");
Serial.println(litros);
ISRContador = 0;
contador = 0;
  }
}
CAYENNE_OUT_DEFAULT()
{
Cayenne.virtualWrite (3, litros, "rain_level", "cm");

}

Thank you for your response, I ask you to be patient, I still do not know very well the rules of the forum.
I am using an ESP8266-07 board, maybe all the problems come for that.
I loaded the sketch you passed me.
The first problem appears with the definition of SENSOR, it tells me “‘D6’ was not declared in this scope”, I changed it to “const int SENSOR = 2;
After starting the system, in Cayenne Dashboar the device is Off Line and this appears repeatedly in the Monitor Serie:

>>> stack >>>

ctx: cont
sp: 3ffffdb0 end: 3fffffc0 offset: 01b0
3fffff60: feefeffe 00000001 3ffee928 3ffee990
3fffff70: 3fffdad0 00000001 3ffee928 3ffee990
3fffff80: 3fffdad0 00000000 3ffee960 401003da
3fffff90: feefeffe 00000000 3ffee960 40203243
3fffffa0: feefeffe feefeffe feefeffe 40205354
3fffffb0: feefeffe feefeffe 3ffe8594 401006c5
<<< stack <<<

F1⸮⸮⸮⸮ ISR not in IRAM!

Abort called

I don’t know how to continue …

i just modified the code you posted in the very first. i thought you had it working with esp8266-07. So first get your rain guage sensor working with esp-07 with out cayenne.

I reiterate that I am using ESP8266-07.
“const int SENSOR = D6;” It does not work with this device.
I tried this:

            > const int SENSOR = 2;
            void ICACHE_RAM_ATTR counterLiters ();

            void setup () {
            pinMode (SENSOR, INPUT_PULLUP);
            Serial.begin (9600);
            Cayenne.begin (username, password, clientID, ssid, wifiPassword);

            attachInterrupt (digitalPinToInterrupt (SENSOR), counterLiters, FALLING);
            }

Now the counter works, but does not calculate “litros”, it may have some bad parameter. I pass the complete code. It seems that it does not work in “void counterLiters ()”

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>



const int SENSOR=2;
const int umbraltiempo=300;
volatile int ISRContador = 0;
int contador = 0;
float litros=0;
long tiempocontador=0;
long tiempohora =0;

// WiFi network info.
char ssid[] = "Mapear 2.4 GHz";
char wifiPassword[] = "03472977";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "5a3596e0-f12b-99f77add412";
char password[] = "9a4f400584a42a06644668648";
char clientID[] = "a8ed11c0-f8c7b6757b1bf";



unsigned long lastMillis = 0;

void ICACHE_RAM_ATTR contadorLitros();

void setup() {
pinMode(SENSOR, INPUT_PULLUP);
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

attachInterrupt(digitalPinToInterrupt(SENSOR),contadorLitros,FALLING);
}


void loop() {
	Cayenne.loop();

 void contadorLitros()
{
if(millis()>(tiempocontador+umbraltiempo))
{
ISRContador++;
tiempocontador=millis();
Serial.print(ISRContador);

}
}

if (millis()<tiempohora)
{
tiempohora=0;
}
if (millis()-tiempohora>3600000UL) {
tiempohora = millis();
Serial.print(tiempohora);
litros=ISRContador * 0.5;
Serial.print("Litros por m2 caidos en un minuto (L/m2): ");
Serial.println(litros);
ISRContador= 0;
contador= 0;
 }
}
CAYENNE_OUT_DEFAULT()
{
Cayenne.virtualWrite (3, litros, "rain_level", "cm");

}

before trying the cayenne code, first see if you can get the rain gauge sensor working with esp-07. And looking at the code, it looks like it will never get upload and give a compile error.
Just remove the cayenne code and get the sensor reading on the serial monitor.