Hi Alvaro, many thanks for your prompt answer. Please find the code below, my project is about a Water Consumption Monitor. Hope code is easy enough to understand. I would also like to point out that exactly the same code run without any problem for a few hours and the issue arose once the device was disconnected and reconnected (just to change the power source)
//#define DEBUG
#define DISABLE_TLS
#include <BridgeSSLClient.h>
#include <ThingerYun.h>
#include <Process.h>
Process date; // process used to get the date
int hours, minutes, seconds; // for the results
int lastSecond = -1; // need an impossible value for comparison
int lastMinute = -1; // used to avoid sending more than one reading at the same time, start with
// impossible value
// Setup of Thinger.io used as Dashboard and to send email in case of critical consumption
#define USERNAME “OldNerd”
#define DEVICE_ID “Yun”
#define DEVICE_CREDENTIAL “------------”
ThingerYun thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
float ValLitros; //Count how many Liters are consumed by the hour
float TotalDia; //Keeps track of the daily consumption by adding the above value
// Flow Sensor handling variables
byte sensorInterrupt = 4; // 4 = digital pin 7 on the Yun
byte sensorPin = 7;
volatile float pulseCount; // keeps track of the pulses coming from the flow sensor
void setup() {
//Serial.begin (115200);
pinMode(LED_BUILTIN, OUTPUT);
// initialize bridge
Bridge.begin();
thing[“led”] << digitalPin(LED_BUILTIN);
thing[“litros”] >> outputValue(ValLitros);
thing[“TotalDiario”] >> outputValue (TotalDia);
// run an initial date process. Should return:
// hh:mm:ss :
if (!date.running()) {
date.begin(“date”);
date.addParameter(“+%T”);
date.run();
}
// interrupt setup
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
TotalDia = 0;
// The Hall-effect sensor is connected to pin 7 which uses interrupt 4.
// Configured to trigger on a FALLING state change (transition from HIGH
// state to LOW state)
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
checktime();
// check if it is time to send a new reading, for testing will send one reading per minute
//if ((minutes == 00) || (minutes ==15) || (minutes == 30) || (minutes == 45))
//{
if (minutes != lastMinute)
{
if (minutes == 0)
{
pulseCount = 0;
}
else
{
pulseCount = pulseCount + random (300); //for testing only it is simulating sensor readings
}
ValLitros = pulseCount;
// send email waring if the water consumption is higher than a given value
if (ValLitros > 20000)
{
thing.call_endpoint("EP1",thing["litros"]);
}
TotalDia = TotalDia + ValLitros ;
thing.handle();
lastMinute = minutes;
if (minutes == 0)
{
pulseCount = 0; //reset counter to start new cycle
}
// at the start of every day reset the Daily Total
if (hours == 1)
{
TotalDia = 0;
}
}
//}
//delay (3000);
thing.handle();
}
void checktime ()
{
if (hours <= 9) {
//Serial.print(“0”); // adjust for 0-9
}
//Serial.println(hours);
if (minutes <= 9) {
//Serial.print("0"); // adjust for 0-9
}
//Serial.println(minutes);
// restart the date process:
if (!date.running()) {
date.begin("date");
date.addParameter("+%T");
date.run();
}
//if there’s a result from the date process, parse it:
while (date.available() > 0) {
// get the result of the date process (should be hh:mm:ss):
String timeString = date.readString();
// find the colons:
int firstColon = timeString.indexOf(":");
int secondColon = timeString.lastIndexOf(":");
// get the substrings for hour, minute second:
String hourString = timeString.substring(0, firstColon);
String minString = timeString.substring(firstColon + 1, secondColon);
// convert to ints, returning hours and minutes to be used
hours = hourString.toInt();
minutes = minString.toInt();
}
}
/*
Interrupt Service Routine
*/
void pulseCounter()
{
// Increment the pulse counter
pulseCount = pulseCount + 1;
}