HomeWaterLeaksDetection
EmailSender.cpp
Go to the documentation of this file.
1 #include "EmailSender.h"
2 
4 
5 #ifdef WEB_SERVER
6  String HTML_Notification_BOOTING(const EmailSender& emailSender);
7  String HTML_Notification_DAILY_OVERVIEW(const EmailSender& emailSender);
8  String HTML_Notification_LEAK_DETECTED(const EmailSender& emailSender);
9  String HTML_Notification_VALVE_STATE(const EmailSender& emailSender);
10  String HTML_Notification_RESET(const EmailSender& emailSender);
11  String HTML_Notification_ALARM(const EmailSender& emailSender);
12  String HTML_Notification_CHANGED_SETTINGS(const EmailSender& emailSender);
13  String HTML_Notification_APPLIED_NEW_SETTING(const EmailSender& emailSender);
14  String HTML_ReceiverEmailAddress(const EmailSender& emailSender);
15  String HTML_Notification_BYPASS(const EmailSender& emailSender);
16 #endif
17 
19  pinMode(SS_SD_CARD, OUTPUT);
20  pinMode(SS_ETHERNET, OUTPUT);
21  digitalWrite(SS_SD_CARD, HIGH); // SD Card not active
22  digitalWrite(SS_ETHERNET, HIGH); // Ethernet not active
23 
24  Ethernet.begin(mac); // set the mac addr of the ethernet shield
25 
26  // HW not found error
27  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
28  #ifdef DEBUG
29  Serial.println("Ethernet shield was not found");
30  #endif
31  return 0;
32  }
33  // cable is not connected error
34  if (Ethernet.linkStatus() == LinkOFF) {
35  #ifdef DEBUG
36  Serial.println("Ethernet cable is not connected.");
37  #endif
38  return 0;
39  }
40  return 1;
41 }
42 
43 void EmailSender::enableNotification(Type type, bool state) {
44  enabled[type] = state;
45 }
46 
48  if (Ethernet.linkStatus() != LinkON) {
49  if (!setEthernet())
50  return;
51  }
52  // by default disable all e-mail notifications
53  enabled[BOOTING] = false;
54  enabled[DAILY_OVERVIEW] = false;
55  enabled[LEAK_DETECTED] = false;
56  enabled[VALVE_STATE] = false;
57  enabled[RESET] = false;
58  enabled[ALARM] = false;
59  enabled[CHANGED_SETTINGS] = false;
61  enabled[BYPASS] = false;
62 
63  #ifdef WEB_SERVER
64  // functions providing data to the webserver
75  #endif
76 }
77 
78 void EmailSender::sendEmail(Type type,String subject, String data) {
79  if (enabled[type])
80  sendEmail(subject, data);
81 }
82 
84  this->receiverEmailAddress = email;
85 }
86 
88  if (instance == NULL)
89  instance = new EmailSender;
90  return instance;
91 }
92 
93 byte EmailSender::sendEmail(String subject, String data) {
94  // connecting to the server
95  int success = client.connect(SMTPServerAddress, SMTPServerPort);
96  #ifdef DEBUG
97  if (success == 0)
98  Serial.println("connected to the SMTPServer");
99  else Serial.println("connection to the SMTPServer failed");
100  #endif
101  if (success != 1)
102  return 0;
103 
104  if (!eRcv())
105  return 0;
106 
107  // sending the "HELLO" message
108  #ifdef DEBUG
109  Serial.println("Sending hello");
110  #endif
111  client.println("EHLO " + String(arduinoIP));
112  if (!eRcv())
113  return 0;
114 
115  // sending a message to authenticate
116  #ifdef DEBUG
117  Serial.println("Sending auth login");
118  #endif
119  client.println("auth login");
120  if (!eRcv())
121  return 0;
122 
123  // sending the login
124  #ifdef DEBUG
125  Serial.println(("Sending User"));
126  #endif
127  client.println(auth64Login);
128  if (!eRcv())
129  return 0;
130 
131  // sending the password
132  #ifdef DEBUG
133  Serial.println(F("Sending Password"));
134  #endif
135  client.println(auth64Pass);
136  if (!eRcv())
137  return 0;
138 
139  // sending the sender's email address
140  #ifdef DEBUG
141  Serial.println("Sending From");
142  #endif
143  client.println("MAIL From: <" + String(emailAddress) + ">");
144  if (!eRcv())
145  return 0;
146 
147  // sending the receiver's email address
148  #ifdef DEBUG
149  Serial.println("Sending To");
150  #endif
151  client.println("RCPT To: <" + String(receiverEmailAddress) + ">");
152  if (!eRcv())
153  return 0;
154 
155  // starting sending data
156  #ifdef DEBUG
157  Serial.println("Sending DATA");
158  #endif
159  client.println("DATA");
160  if (!eRcv())
161  return 0;
162 
163  // sending the e-mail
164  #ifdef DEBUG
165  Serial.println("Sending email");
166  #endif
167  client.println("To: User <" + String(receiverEmailAddress) + ">");
168  client.println("From: Home Water Leaks Detection <" + String(emailAddress) + ">");
169 
170  client.println("Subject: " + subject); // subject
171  client.println(data); // body
172  client.println("."); // end of the e-mail
173  if (!eRcv())
174  return 0;
175 
176  // log out
177  #ifdef DEBUG
178  Serial.println("Sending QUIT");
179  #endif
180  client.println("QUIT");
181  if (!eRcv())
182  return 0;
183 
184  // close the connection
185  client.stop();
186  #ifdef DEBUG
187  Serial.println("disconnected");
188  #endif
189  return 1;
190 }
191 
193  byte respCode;
194  byte thisByte;
195  int loopCount = 0;
196 
197  while (!client.available()) {
198  delay(1);
199  loopCount++;
200 
201  if (loopCount > 10000) {
202  client.stop();
203  #ifdef DEBUG
204  Serial.println("\r\nTimeout");
205  #endif
206  return 0;
207  }
208  }
209  respCode = client.peek();
210  while (client.available()) {
211  thisByte = client.read();
212  #ifdef DEBUG
213  Serial.write(thisByte);
214  #endif
215  }
216  if (respCode >= '4') {
217  efail();
218  return 0;
219  }
220  return 1;
221 }
222 
224  byte thisByte = 0;
225  int loopCount = 0;
226  client.println(F("QUIT"));
227  while (!client.available()) {
228  delay(1);
229  loopCount++;
230  if (loopCount > 10000) {
231  client.stop();
232  #ifdef DEBUG
233  Serial.println("\r\nTimeout");
234  #endif
235  return;
236  }
237  }
238  while (client.available()) {
239  thisByte = client.read();
240  Serial.write(thisByte);
241  }
242  client.stop();
243  #ifdef DEBUG
244  Serial.println("disconnected");
245  #endif
246 }
247 
249  return String(enabled[BOOTING]) + ";" +
250  String(enabled[DAILY_OVERVIEW]) + ";" +
251  String(enabled[LEAK_DETECTED]) + ";" +
252  String(enabled[VALVE_STATE]) + ";" +
253  String(enabled[RESET]) + ";" +
254  String(enabled[ALARM]) + ";" +
255  String(enabled[CHANGED_SETTINGS]) + ";" +
256  String(enabled[APPLIED_NEW_SETTING]) + ";" +
257  String(enabled[BYPASS]) + ";" +
258  receiverEmailAddress + ";"; // user's e-mail address
259 }
260 
261 #ifdef WEB_SERVER
262  const String EmailSender::getHTMLData(const int id) const {
263  auto fce = htmlData.find(id);
264  if (fce == htmlData.end())
266  return fce->second(*this);
267  }
268 
269  String HTML_Notification_BOOTING(const EmailSender& emailSender) {
270  auto it = emailSender.enabled.find(EmailSender::BOOTING);
271  return it->second == true ? "checked" : "";
272  }
273 
274  String HTML_Notification_DAILY_OVERVIEW(const EmailSender& emailSender) {
275  auto it = emailSender.enabled.find(EmailSender::DAILY_OVERVIEW);
276  return it->second == true ? "checked" : "";
277  }
278 
279  String HTML_Notification_LEAK_DETECTED(const EmailSender& emailSender) {
280  auto it = emailSender.enabled.find(EmailSender::LEAK_DETECTED);
281  return it->second == true ? "checked" : "";
282  }
283 
284  String HTML_Notification_VALVE_STATE(const EmailSender& emailSender) {
285  auto it = emailSender.enabled.find(EmailSender::VALVE_STATE);
286  return it->second == true ? "checked" : "";
287  }
288 
289  String HTML_Notification_RESET(const EmailSender& emailSender) {
290  auto it = emailSender.enabled.find(EmailSender::RESET);
291  return it->second == true ? "checked" : "";
292  }
293 
294  String HTML_Notification_ALARM(const EmailSender& emailSender) {
295  auto it = emailSender.enabled.find(EmailSender::ALARM);
296  return it->second == true ? "checked" : "";
297  }
298 
299  String HTML_Notification_CHANGED_SETTINGS(const EmailSender& emailSender) {
300  auto it = emailSender.enabled.find(EmailSender::CHANGED_SETTINGS);
301  return it->second == true ? "checked" : "";
302  }
303 
304  String HTML_Notification_APPLIED_NEW_SETTING(const EmailSender& emailSender) {
305  auto it = emailSender.enabled.find(EmailSender::APPLIED_NEW_SETTING);
306  return it->second == true ? "checked" : "";
307  }
308 
309  String HTML_Notification_BYPASS(const EmailSender& emailSender) {
310  auto it = emailSender.enabled.find(EmailSender::BYPASS);
311  return it->second == true ? "checked" : "";
312  }
313 
314  String HTML_ReceiverEmailAddress(const EmailSender& emailSender) {
315  return emailSender.receiverEmailAddress;
316  }
317 #endif
EmailSender::CHANGED_SETTINGS
@ CHANGED_SETTINGS
when the user changes settings
Definition: EmailSender.h:51
EmailSender::getHTMLData
const String getHTMLData(const int id) const
Since this class is registered as a source of data for the HTML content, it needs return the appropri...
EmailSender::instance
static EmailSender * instance
the instance of EmailSender
Definition: EmailSender.h:61
arduinoIP
#define arduinoIP
Definition: EmailSender.h:20
EmailSender::HTML_Notification_APPLIED_NEW_SETTING
friend String HTML_Notification_APPLIED_NEW_SETTING(const EmailSender &emailSender)
Associated function for returing the state of the 'applied settings' notification.
EmailSender::BYPASS
@ BYPASS
when a bypass changes
Definition: EmailSender.h:53
EmailSender::HTML_Notification_BYPASS
friend String HTML_Notification_BYPASS(const EmailSender &emailSender)
Associated function for returing the state of the 'bypass state' notification.
EmailSender::getFormatOfSettingsToSave
String getFormatOfSettingsToSave()
Returns settings.
Definition: EmailSender.cpp:248
SS_SD_CARD
#define SS_SD_CARD
SD card pin (arduino documentation)
Definition: Setup.h:30
EmailSender::receiverEmailAddress
String receiverEmailAddress
the default user's email address
Definition: EmailSender.h:65
EmailSender::enabled
std::map< Type, bool > enabled
map holding info about each time of e-mail - whether it's enabled or not
Definition: EmailSender.h:60
EmailSender
Definition: EmailSender.h:39
EmailSender::Type
Type
different types of e-mail that can be sent to the user
Definition: EmailSender.h:44
EmailSender::enableNotification
void enableNotification(Type type, bool state)
Enables/disables the particular type of e-mail notification.
Definition: EmailSender.cpp:43
EmailSender::BOOTING
@ BOOTING
when the system boots up
Definition: EmailSender.h:45
emailAddress
#define emailAddress
Definition: EmailSender.h:25
EmailSender.h
auth64Pass
#define auth64Pass
Definition: EmailSender.h:27
EmailSender::HTML_Notification_VALVE_STATE
friend String HTML_Notification_VALVE_STATE(const EmailSender &emailSender)
Associated function for returing the state of the 'valve state' notification.
EmailSender::setReceiverEmailAddress
void setReceiverEmailAddress(String email)
Changes the user's e-mail address.
Definition: EmailSender.cpp:83
EmailSender::HTML_Notification_BOOTING
friend String HTML_Notification_BOOTING(const EmailSender &emailSender)
Associated function for returing the state of the 'bootup' notification.
EmailSender::LEAK_DETECTED
@ LEAK_DETECTED
when a leak has been detected
Definition: EmailSender.h:47
HTMLDataSource::UNDEFINED_DATA
static const String UNDEFINED_DATA
string "UNDEFINED"
Definition: HTMLDataSource.h:17
EmailSender::efail
void efail()
Closes the ethernet client after the connection has failed.
Definition: EmailSender.cpp:223
EmailSender::client
EthernetClient client
EthernetClient used to connect to the smt2go server.
Definition: EmailSender.h:62
auth64Login
#define auth64Login
Definition: EmailSender.h:26
EmailSender::mac
byte mac[6]
mac address of the ethernet shield
Definition: EmailSender.h:64
EmailSender::HTML_ReceiverEmailAddress
friend String HTML_ReceiverEmailAddress(const EmailSender &emailSender)
Associated function for returing the user's e-mail address.
EmailSender::sendEmail
byte sendEmail(String subject, String data)
Sends an e-mail off to the smtp2go server.
Definition: EmailSender.cpp:93
SS_ETHERNET
#define SS_ETHERNET
etherent pin (arduino documentation)
Definition: Setup.h:31
EmailSender::HTML_Notification_CHANGED_SETTINGS
friend String HTML_Notification_CHANGED_SETTINGS(const EmailSender &emailSender)
Associated function for returing the state of the 'changed settings' notification.
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
EmailSender::eRcv
byte eRcv()
Reads data using the ethernet client comming from the smtp2go server.
Definition: EmailSender.cpp:192
EmailSender::HTML_Notification_ALARM
friend String HTML_Notification_ALARM(const EmailSender &emailSender)
Associated function for returing the state of the 'home alarm state' notification.
SMTPServerAddress
#define SMTPServerAddress
Definition: EmailSender.h:22
EmailSender::HTML_Notification_DAILY_OVERVIEW
friend String HTML_Notification_DAILY_OVERVIEW(const EmailSender &emailSender)
Associated function for returing the state of the 'daily overview' notification.
EmailSender::EmailSender
EmailSender()
Constructor of the class.
Definition: EmailSender.cpp:47
EmailSender::DAILY_OVERVIEW
@ DAILY_OVERVIEW
daily overview about the system
Definition: EmailSender.h:46
EmailSender::HTML_Notification_LEAK_DETECTED
friend String HTML_Notification_LEAK_DETECTED(const EmailSender &emailSender)
Associated function for returing the state of the 'leak detected' notification.
EmailSender::setEthernet
int setEthernet()
Initializes the ethernet shield.
Definition: EmailSender.cpp:18
EmailSender::HTML_Notification_RESET
friend String HTML_Notification_RESET(const EmailSender &emailSender)
Associated function for returing the state of the 'system reset' notification.
EmailSender::ALARM
@ ALARM
when the state of the home alarm changes
Definition: EmailSender.h:50
SMTPServerPort
#define SMTPServerPort
Definition: EmailSender.h:23
EmailSender::htmlData
std::map< int, String(*)(const EmailSender &leaksController)> htmlData
A map of different values (keys) and their associated functions which returns the appropriate values.
Definition: EmailSender.h:139
EmailSender::getInstance
static EmailSender * getInstance()
Returns the instance of the class.
Definition: EmailSender.cpp:87