Hello,
I am going to make a sensor that reads temperature and flow data and sends them every x minutes to TTN. In between, I need to put the board into sleep mode to save more power. So, every time that there is a flow, the sensor should use interrupts to calculate flow, but the problem is that when my board is in sleep mode it does not wake up by external interrupt and calculates the flow. What can I do? I am using MKRWAN1310 board
#include <MKRWAN.h>
#include <Thermistor.h>
#include <NTC_Thermistor.h>
#include <ArduinoLowPower.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Initializations
#define SENSOR_PIN A1
#define REFERENCE_RESISTANCE 50 //The resistance of external resistance used in the circuit
#define NOMINAL_RESISTANCE 50
#define NOMINAL_TEMPERATURE 50
#define B_VALUE 3950
int flowPin = 0;
int ledPin=2;
double flowRate;
int minutes = 1; //measure temperature and flow every n minutes (actually n minutes * m miliseconds)
float cfu=1000;
float cfu_network=1000; //cfu/l : the concentration of tank outlet
float cfu_max=5000; //cfu/l : the maximum allowed concentration
float DO; //doubling time
float freq=60/minutes; //to adjust the doubling time based on measurement interval
volatile int count;
String appEui = "nnnnnnnnnnnnnn";
String appKey = "nnnnnnnnnnnnnnnn";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Class instances
LoRaModem modem;
NTC_Thermistor thermistor(SENSOR_PIN, REFERENCE_RESISTANCE,NOMINAL_RESISTANCE,NOMINAL_TEMPERATURE,B_VALUE);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Sensors setup
Serial.begin(115200);
pinMode(flowPin, INPUT);
pinMode(SENSOR_PIN, INPUT);
pinMode(ledPin,OUTPUT);
attachInterrupt(digitalPinToInterrupt(flowPin), Flow, HIGH);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Modem setup
while (!Serial);
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) {
Serial.println("Failed to start module");
while (1) {}
};
Serial.print("Your module version is: ");
Serial.println(modem.version());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
modem.minPollInterval(60);
}
void loop() {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Measurements
delay(minutes*6000);
digitalWrite(LED_BUILTIN,LOW);
count = 0; // Reset the counter so we start counting from 0 again
attachInterrupt(digitalPinToInterrupt(flowPin), Flow, HIGH);
delay(1000);
detachInterrupt(digitalPinToInterrupt(flowPin));
double flow=count/10;
Serial.print("Flow (L/min):");
Serial.println(flow);
double temp = thermistor.readCelsius();
Serial.print("Temperature (C):");
Serial.println(temp);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Risk evaluation
//based on REPORT Legionella and solar water heaters - SUTER consulting
//we consider that the temperature profile in tank is the worst condition: between 47 to 55,
//so the tank outlet cfu is equal to the network cfu
if (flow>0){ //reset concentration
cfu=cfu_network;
Serial.println("Tap was used");
}
else if (temp<25 || temp>=47) { //stable concentration
cfu=cfu;
Serial.println("Stable condition");}
else if (25 <= temp < 47){ //grow concentration
DO=0.5702*pow(temp,2)-43.3*temp+829;
cfu=cfu+cfu/(DO*freq); //DO is based on hour but here the timestep is 15 minutes
Serial.println("Growth condition");
}
float risk=(cfu/cfu_max)*100;
Serial.print("Increment= ");
Serial.println(cfu/(DO*freq));
Serial.print("concentration (CFU/L)= ");
Serial.println(cfu);
Serial.print("risk (%)= ");
Serial.println(risk);
if (risk>=80){
digitalWrite(ledPin,HIGH);
Serial.println("LED is ON");
}
else {
digitalWrite(ledPin,LOW);
Serial.println("LED is OFF");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Data sending
int err;
modem.beginPacket();
modem.print(temp);
modem.print(flow);
modem.print(cfu);
modem.print(risk);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
Serial.println("before sleep");
digitalWrite(LED_BUILTIN,HIGH);
LowPower.sleep(1*60000);
Serial.println("after sleep");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Functions
void Flow()
{
digitalWrite(LED_BUILTIN,LOW);
count++; //Every time this function is called, increment "count" by 1
}