HomeWaterLeaksDetection
main.cpp File Reference
#include "Arduino.h"
#include "vector"
#include "SPI.h"
#include "SD.h"
#include "Setup.h"
#include "Pins.h"
#include "LimitsDefinition.h"
#include "DateTime.h"
#include "EmailSender.h"
#include "DailyOverview.h"
#include "FreeMemoryMeasurement.h"
#include "PulseCounter.h"
#include "LeaksController.h"
#include "ALeakDetectable.h"
#include "LeakDetectionConfig.h"
#include "HighLeakDetection.h"
#include "LowLeakDetection.h"
#include "TotalLeakDetection.h"

Go to the source code of this file.

Functions

void applyDefaultSettings ()
 
std::vector< String > split (String &data)
 
int createHighLeakDetection (String data)
 
int createLowLeakDetection (String data)
 
int createTotalLeakDetection (String data)
 
int setSettingsNotification (String data)
 
void loadSettings ()
 
void setup ()
 
void loop ()
 

Variables

PulseCounter pulseCounter (SENSOR_PIN)
 
std::vector< IControllable * > controllers
 
HighLeakDetectionhighLeakDetection
 
LowLeakDetectionlowLeakDetection
 
TotalLeakDetectiontotalLeakDetection
 

Function Documentation

◆ applyDefaultSettings()

◆ createHighLeakDetection()

int createHighLeakDetection ( String  data)

Definition at line 98 of file main.cpp.

98  {
99  std::vector<String> tokens = split(data);
100 
101  // make sure there's exactly 4 values
102  // in the file - limit,reset (normal ,alaram)
103  if (tokens.size() != 4)
104  return 0;
105 
107  &pulseCounter,
109  {strtoul(tokens[0].c_str(), NULL, 10), UNUSED, atoi(tokens[1].c_str()), HIGH_BYPASS_PIN},
110  {strtoul(tokens[2].c_str(), NULL, 10), UNUSED, atoi(tokens[3].c_str()), HIGH_BYPASS_PIN}
111  );
112  return 1; // everything's okay
113 }

References ALeakDetectable::High, HIGH_BYPASS_PIN, highLeakDetection, pulseCounter, split(), and UNUSED.

Referenced by loadSettings().

◆ createLowLeakDetection()

int createLowLeakDetection ( String  data)

Definition at line 118 of file main.cpp.

118  {
119  std::vector<String> tokens = split(data);
120 
121  // make sure there's exactly 4 values
122  // in the file - limit,reset (normal ,alaram)
123  if (tokens.size() != 4)
124  return 0;
125 
127  &pulseCounter,
129  {strtoul(tokens[0].c_str(), NULL, 10), strtoul(tokens[1].c_str(), NULL, 10), UNUSED, LOW_BYPASS_PIN},
130  {strtoul(tokens[2].c_str(), NULL, 10), strtoul(tokens[3].c_str(), NULL, 10), UNUSED, LOW_BYPASS_PIN}
131  );
132  return 1; // everything's okay
133 }

References ALeakDetectable::Low, LOW_BYPASS_PIN, lowLeakDetection, pulseCounter, split(), and UNUSED.

Referenced by loadSettings().

◆ createTotalLeakDetection()

int createTotalLeakDetection ( String  data)

Definition at line 138 of file main.cpp.

138  {
139  std::vector<String> tokens = split(data);
140 
141  // make sure there's exactly 4 values
142  // in the file - limit,reset (normal ,alaram)
143  if (tokens.size() != 4)
144  return 0;
145 
147  &pulseCounter,
149  {strtoul(tokens[0].c_str(), NULL, 10), UNUSED, atoi(tokens[1].c_str()), TOTAL_BYPASS_PIN},
150  {strtoul(tokens[2].c_str(), NULL, 10), UNUSED, atoi(tokens[3].c_str()), TOTAL_BYPASS_PIN}
151  );
152  return 1; // everything's okay
153 }

References pulseCounter, split(), ALeakDetectable::Total, TOTAL_BYPASS_PIN, totalLeakDetection, and UNUSED.

Referenced by loadSettings().

◆ loadSettings()

void loadSettings ( )

Definition at line 182 of file main.cpp.

182  {
183  #ifdef DEBUG
184  Serial.println("loading settings...");
185  #endif
186 
187  // if the SD card is not avaliable,
188  // apply the default settings of all
189  // three water leak detection algorithms
190  if (!SD.begin(SS_SD_CARD)) {
191  #ifdef DEBUG
192  Serial.println("ERROR - SD card initialization failed!");
193  #endif
195  return;
196  }
197 
198  pinMode(SS_SD_CARD, OUTPUT);
199  pinMode(SS_ETHERNET, OUTPUT);
200  digitalWrite(SS_SD_CARD, HIGH); // SD Card not active
201  digitalWrite(SS_ETHERNET, HIGH); // Ethernet not active
202 
203  // search for the settings file on the SD card
204  File settingsFile = SD.open(SETTINGS_FILE_NAME);
205 
206  if (settingsFile) {
207  // if the any of the lines of the settings file doesn't
208  // have the right syntax, automatically apply the default
209  // settings for all three water-leak detection algorithms
210  if (!createHighLeakDetection(settingsFile.readStringUntil('\n')) ||
211  !createLowLeakDetection(settingsFile.readStringUntil('\n')) ||
212  !createTotalLeakDetection(settingsFile.readStringUntil('\n')) ||
213  !setSettingsNotification(settingsFile.readStringUntil('\n')))
214  {
216  }
217  settingsFile.close(); // close the config file
218 
219  } else {
220  #ifdef DEBUG
221  Serial.print("error opening ");
222  Serial.println(SETTINGS_FILE_NAME);
223  #endif
224  applyDefaultSettings(); // apply the default settings
225  }
226 }

References applyDefaultSettings(), createHighLeakDetection(), createLowLeakDetection(), createTotalLeakDetection(), setSettingsNotification(), SETTINGS_FILE_NAME, SS_ETHERNET, and SS_SD_CARD.

Referenced by setup().

◆ loop()

void loop ( )

Definition at line 332 of file main.cpp.

332  {
333 // WARNING doesn't work correctly when the user requests to open the website
334 // because it's not included the the class LeakController that is updated
335 // independently while the website is being loaded. This is only for
336 // testing purposes anyway.
337 #ifdef GENERATE_PULSES
338  pulseGenerator.update();
339 #endif
340 
341  // update all the constrollers of the system
342  for (const auto controller : controllers)
343  controller->update();
344 }

References controllers.

◆ setSettingsNotification()

int setSettingsNotification ( String  data)

Definition at line 158 of file main.cpp.

158  {
159  #ifdef EMAIL_NOTIFICATION
160  std::vector<String> tokens = split(data);
161 
162  // make sure there's exactly mailSender::Type::COUNT + 1
163  // on the particular line in the settings file
164  if (tokens.size() != EmailSender::Type::COUNT + 1)
165  return 0;
166 
167  // apply the settings
178  #endif
179  return 1; // everything's okay
180 }

References EmailSender::ALARM, EmailSender::APPLIED_NEW_SETTING, EmailSender::BOOTING, EmailSender::BYPASS, EmailSender::CHANGED_SETTINGS, EmailSender::DAILY_OVERVIEW, EmailSender::enableNotification(), EmailSender::getInstance(), EmailSender::LEAK_DETECTED, EmailSender::RESET, EmailSender::setReceiverEmailAddress(), split(), and EmailSender::VALVE_STATE.

Referenced by loadSettings().

◆ setup()

void setup ( )

Definition at line 228 of file main.cpp.

228  {
229  // enable the serial communication
230  // for the terminal (debugging)
231  Serial.begin(SERIAL_BAUD_RATE);
232  while (!Serial)
233  ;
234 
235  DateTime::getInstance(); //setup the datetime
236 
237  #ifdef DEBUG
238  Serial.println("<[STARTING PROGRAM]>");
239  Serial.println("pins configuration...");
240  #endif
241 
242  CONFIG_PINS // configure pins
243  loadSettings(); // load setting off the SD card
244 
245  // initialize the logging part of the system
246  #ifdef DEBUG
247  Serial.println("adding instances to logging...");
253  controllers.push_back(Logger::getInstance());
254  #endif
255 
256  // initialize all controllers of the system
257  #ifdef DEBUG
258  Serial.println("adding instances of all the controllers...");
259  #endif
265 
266  // initialize the LCD display (what pages will be displayed)
267  #ifdef LCD_DISPLAY
272  //LCDController::getInstance()->addItem(FreeMemoryMeasurement::getInstance());
274  #ifdef DEBUG
276  #endif
277  #endif
278 
279  // initialize the webserver
280  #ifdef WEB_SERVER
281  #ifdef DEBUG
282  Serial.println("web server initialization...");
283  #endif
284 
285  // set HTML data sources
294 
295  controllers.push_back(WebServer::getInstance());
296 
297  // monitoring of water consumption
298  Consumption *dailyWaterConsumption = new Consumption(&pulseCounter, Consumption::DAY);
299  Consumption *monthlyWaterConsumption = new Consumption(&pulseCounter, Consumption::MONTH);
300 
303 
305  #endif
306 
307  // initialize the daily overview class
308  #ifdef LCD_DISPLAY
309  DailyOverview *dailyOverview = new DailyOverview(highLeakDetection, // high-water algo.
310  lowLeakDetection, // low-water algo.
311  totalLeakDetection, // total-water algo.
312  dailyWaterConsumption, // daily consump.
313  monthlyWaterConsumption); // monthly consump.
314  controllers.insert(controllers.begin(), dailyOverview);
315  LCDController::getInstance()->addItem(dailyOverview);
316  #endif
317 
318  #ifdef DEBUG
319  Serial.println("program is now running...");
320  #endif
321 
322  // send an e-mail that the device has started
323  #ifdef EMAIL_NOTIFICATION
326  "Home Water Leaks Detection",
327  "The device is now running\n\r"
328  "start time: " + DateTime::getInstance()->getDateTimeStr());
329  #endif
330 }

References Logger::addEntity(), WebServer::addHTMLSource(), LCDController::addItem(), LeaksController::addLeakDetection(), EmailSender::BOOTING, CONFIG_PINS, controllers, Consumption::DAY, Logger::getInstance(), LCDController::getInstance(), FreeMemoryMeasurement::getInstance(), LeaksController::getInstance(), EmailSender::getInstance(), WebServer::getInstance(), DateTime::getInstance(), ALeakDetectable::High, highLeakDetection, loadSettings(), ALeakDetectable::Low, lowLeakDetection, Consumption::MONTH, pulseCounter, EmailSender::sendEmail(), SERIAL_BAUD_RATE, LeaksController::setDailyConsumptionCounter(), LeaksController::setMonthlyConsumptionCounter(), LeaksController::setPulseCounter(), WebServer::setup(), ALeakDetectable::Total, and totalLeakDetection.

◆ split()

std::vector<String> split ( String &  data)

Definition at line 82 of file main.cpp.

82  {
83  std::vector<String> tokens;
84  String buffer = "";
85  for (int i = 0; i < (int)data.length(); i++) {
86  if (data[i] == ';') {
87  tokens.push_back(buffer);
88  buffer = "";
89  }
90  else buffer += data[i];
91  }
92  return tokens;
93 }

Referenced by createHighLeakDetection(), createLowLeakDetection(), createTotalLeakDetection(), and setSettingsNotification().

Variable Documentation

◆ controllers

std::vector<IControllable *> controllers

Definition at line 49 of file main.cpp.

Referenced by loop(), and setup().

◆ highLeakDetection

HighLeakDetection* highLeakDetection

Definition at line 51 of file main.cpp.

Referenced by applyDefaultSettings(), createHighLeakDetection(), and setup().

◆ lowLeakDetection

LowLeakDetection* lowLeakDetection

Definition at line 52 of file main.cpp.

Referenced by applyDefaultSettings(), createLowLeakDetection(), and setup().

◆ pulseCounter

◆ totalLeakDetection

TotalLeakDetection* totalLeakDetection

Definition at line 53 of file main.cpp.

Referenced by applyDefaultSettings(), createTotalLeakDetection(), and setup().

EmailSender::CHANGED_SETTINGS
@ CHANGED_SETTINGS
when the user changes settings
Definition: EmailSender.h:51
DailyOverview
Definition: DailyOverview.h:27
ALeakDetectable::Total
@ Total
total water leak detection algorithm
Definition: ALeakDetectable.h:60
lowLeakDetection
LowLeakDetection * lowLeakDetection
Definition: main.cpp:52
LeaksController::addLeakDetection
void addLeakDetection(ALeakDetectable::Type type, ALeakDetectable *leak)
Adds another water leak detection algorithm to the collection.
Definition: LeaksController.cpp:33
EmailSender::BYPASS
@ BYPASS
when a bypass changes
Definition: EmailSender.h:53
highLeakDetection
HighLeakDetection * highLeakDetection
Definition: main.cpp:51
pulseCounter
PulseCounter pulseCounter(SENSOR_PIN)
SS_SD_CARD
#define SS_SD_CARD
SD card pin (arduino documentation)
Definition: Setup.h:30
Logger::getInstance
static Logger * getInstance()
Returns the instance of the class.
Definition: Logger.cpp:5
ALeakDetectable::Low
@ Low
low water leak detection algorithm
Definition: ALeakDetectable.h:58
setSettingsNotification
int setSettingsNotification(String data)
Definition: main.cpp:158
applyDefaultSettings
void applyDefaultSettings()
Definition: main.cpp:57
split
std::vector< String > split(String &data)
Definition: main.cpp:82
HIGH_BYPASS_PIN
#define HIGH_BYPASS_PIN
high-water leak bypass button
Definition: Pins.h:25
createTotalLeakDetection
int createTotalLeakDetection(String data)
Definition: main.cpp:138
EmailSender::enableNotification
void enableNotification(Type type, bool state)
Enables/disables the particular type of e-mail notification.
Definition: EmailSender.cpp:43
SETTINGS_FILE_NAME
#define SETTINGS_FILE_NAME
the main settings file stored on the SD card
Definition: Setup.h:27
LeaksController::setMonthlyConsumptionCounter
void setMonthlyConsumptionCounter(Consumption *monthlyConsumptionCounter)
Sets an instance of Consumption (monthly water consumption)
Consumption::MONTH
@ MONTH
monthly water consumption
Definition: Consumption.h:22
SERIAL_BAUD_RATE
#define SERIAL_BAUD_RATE
baud rate for the serial communication (terminal)
Definition: Setup.h:25
TotalLeakDetection
Definition: TotalLeakDetection.h:15
EmailSender::BOOTING
@ BOOTING
when the system boots up
Definition: EmailSender.h:45
HighLeakDetection
Definition: HighLeakDetection.h:23
createLowLeakDetection
int createLowLeakDetection(String data)
Definition: main.cpp:118
ALeakDetectable::High
@ High
high water leak detection algorithm
Definition: ALeakDetectable.h:59
loadSettings
void loadSettings()
Definition: main.cpp:182
LCDController::addItem
void addItem(IDisplayable *item)
Adds another source of data for the LCD display (another class)
Definition: LCDController.cpp:16
createHighLeakDetection
int createHighLeakDetection(String data)
Definition: main.cpp:98
EmailSender::setReceiverEmailAddress
void setReceiverEmailAddress(String email)
Changes the user's e-mail address.
Definition: EmailSender.cpp:83
CONFIG_PINS
#define CONFIG_PINS
Definition: Pins.h:37
LOW_BYPASS_PIN
#define LOW_BYPASS_PIN
low-water leak bypass button
Definition: Pins.h:29
controllers
std::vector< IControllable * > controllers
Definition: main.cpp:49
DateTime::getInstance
static DateTime * getInstance()
Returns the instance of the class.
Definition: DateTime.cpp:26
EmailSender::LEAK_DETECTED
@ LEAK_DETECTED
when a leak has been detected
Definition: EmailSender.h:47
totalLeakDetection
TotalLeakDetection * totalLeakDetection
Definition: main.cpp:53
WebServer::addHTMLSource
void addHTMLSource(HTMLDataSource *source)
Adds another source of data for the HTML content.
Definition: WebServer.cpp:90
Consumption
Definition: Consumption.h:13
TOTAL_LEAK_NORMAL_DEFAULT_CONFIG
#define TOTAL_LEAK_NORMAL_DEFAULT_CONFIG
Passes the default total-water leak detection parameters to the constructor.
Definition: LimitsDefinition.h:53
LeaksController::setDailyConsumptionCounter
void setDailyConsumptionCounter(Consumption *dailyConsumptionCounter)
Sets an instance of Consumption (daily water consumption)
FreeMemoryMeasurement::getInstance
static FreeMemoryMeasurement * getInstance()
Returns the instance of the class.
Definition: FreeMemoryMeasurement.cpp:15
EmailSender::sendEmail
byte sendEmail(String subject, String data)
Sends an e-mail off to the smtp2go server.
Definition: EmailSender.cpp:93
LeaksController::setPulseCounter
void setPulseCounter(PulseCounter *pulseCounter)
Sets an instance of PulseCounter.
Definition: LeaksController.cpp:90
SS_ETHERNET
#define SS_ETHERNET
etherent pin (arduino documentation)
Definition: Setup.h:31
EmailSender::VALVE_STATE
@ VALVE_STATE
when the state of the valve has changed
Definition: EmailSender.h:48
EmailSender::RESET
@ RESET
when the device resets
Definition: EmailSender.h:49
EmailSender::APPLIED_NEW_SETTING
@ APPLIED_NEW_SETTING
when the new settings are applied
Definition: EmailSender.h:52
Logger::addEntity
void addEntity(ILoggable *log)
Adds another class to the logging system.
Definition: Logger.cpp:10
LCDController::getInstance
static LCDController * getInstance()
Returns the instance of the class.
Definition: LCDController.cpp:10
TOTAL_BYPASS_PIN
#define TOTAL_BYPASS_PIN
total-water leak bypass button
Definition: Pins.h:33
WebServer::getInstance
static WebServer * getInstance()
Returns the instance of the class.
Definition: WebServer.cpp:9
HIGH_LEAK_NORMAL_DEFAULT_CONFIG
#define HIGH_LEAK_NORMAL_DEFAULT_CONFIG
Passes the default high-water leak detection parameters to the constructor.
Definition: LimitsDefinition.h:39
LOW_LEAK_NORMAL_DEFAULT_CONFIG
#define LOW_LEAK_NORMAL_DEFAULT_CONFIG
Passes the default low-water leak detection parameters to the constructor.
Definition: LimitsDefinition.h:46
LeaksController::getInstance
static LeaksController * getInstance()
Returns the instance of the class.
Definition: LeaksController.cpp:27
EmailSender::DAILY_OVERVIEW
@ DAILY_OVERVIEW
daily overview about the system
Definition: EmailSender.h:46
EmailSender::ALARM
@ ALARM
when the state of the home alarm changes
Definition: EmailSender.h:50
Consumption::DAY
@ DAY
daily water consumption
Definition: Consumption.h:20
LowLeakDetection
Definition: LowLeakDetection.h:21
UNUSED
#define UNUSED
unused parameter
Definition: LimitsDefinition.h:19
EmailSender::getInstance
static EmailSender * getInstance()
Returns the instance of the class.
Definition: EmailSender.cpp:87
WebServer::setup
void setup(LeaksController *leaksController)
Sets the pointer to an instance of LeaksController.
Definition: WebServer.cpp:41