Go to the documentation of this file.
24 if (Ethernet.hardwareStatus() == EthernetNoHardware) {
26 Serial.println(
"Ethernet shield was not found");
32 if (Ethernet.linkStatus() == LinkOFF) {
34 Serial.println(
"Ethernet cable is not connected.");
57 Serial.print(
"server is at ");
58 Serial.println(Ethernet.localIP());
64 Serial.println(
"ERROR - SD card initialization failed!");
69 Serial.println(
"SUCCESS - SD card initialized.");
77 Serial.println(
" doesn't exist.");
83 Serial.println(
" exists.");
105 String httpRequest =
"";
106 String httpRequestStart =
"";
108 EthernetClient client =
server.available();
114 bool currentLineIsBlank =
true;
115 while (client.connected()) {
116 if (client.available()) {
117 char c = client.read();
123 httpRequestStart += c;
135 httpRequestStart += c;
136 httpRequestStart.remove(0);
149 httpRequestStart =
"";
156 if (c ==
'\n' && currentLineIsBlank) {
157 client.println(
"HTTP/1.1 200 OK");
158 client.println(
"Content-Type: text/html");
159 client.println(
"Connection: close");
177 Serial.print(
"error opening ");
184 currentLineIsBlank =
true;
186 currentLineIsBlank =
false;
200 Serial.print(
"HTTP Request: ");
201 Serial.println(httpRequest);
202 Serial.print(
"Size of the HTTP request: ");
203 Serial.print(httpRequest.length());
204 Serial.println(
" B");
207 bool acceptedHttpRequest = 0;
213 if (startPos != -1) {
220 #ifdef EMAIL_NOTIFICATION
221 if (acceptedHttpRequest == 0) {
223 if (startPos != -1) {
224 acceptedHttpRequest = processHTTPRequestNotification(startPos, httpRequest, EmailSender::Type::COUNT + 1);
231 if (acceptedHttpRequest)
233 return acceptedHttpRequest;
244 Serial.println(
"saving settings...");
245 Serial.println(
"raw data:");
246 Serial.println(data);
265 Serial.println(
"settings saved successfully...");
269 #ifdef EMAIL_NOTIFICATION
270 int WebServer::processHTTPRequestNotification(
int startPos, String &httpRequest,
int numberOfValues) {
271 int numberOfSemicolons = 0;
272 int length = httpRequest.length();
273 String data[numberOfValues];
277 if (httpRequest[i] ==
';') {
281 if (data[numberOfSemicolons] ==
"" || (numberOfSemicolons != numberOfValues - 1 && data[numberOfSemicolons].length() > 1))
285 if (++numberOfSemicolons == numberOfValues) {
300 data[numberOfSemicolons] =
"";
303 data[numberOfSemicolons] += httpRequest[i];
310 int numberOfSemicolons = 0;
311 int length = httpRequest.length();
312 String data[numberOfValues];
313 time_t values[numberOfValues];
316 for (
int i = startPos + String(
HTTP_RQ_SETTINGS).length(); i < length; i++) {
319 if (!((httpRequest[i] >=
'0' && httpRequest[i] <=
'9') || httpRequest[i] ==
HTTP_RQ_SEPARATOR))
323 if (httpRequest[i] ==
';') {
324 if (data[numberOfSemicolons] ==
"")
328 if (++numberOfSemicolons == numberOfValues) {
331 for (
int j = 0; j < numberOfValues; j++) {
332 values[j] = strtoul(data[j].c_str(), NULL, 10);
351 data[numberOfSemicolons] =
"";
354 data[numberOfSemicolons] += httpRequest[i];
368 while ((start = html.indexOf(
DATA_PREF)) != -1) {
373 for (end = start; html[end] !=
DATA_POST[0]; end++)
376 sub = html.substring(start, end);
382 htmlData = source->getHTMLData(key);
385 html.replace(old, htmlData);
393 html.replace(old, htmlData);
@ CHANGED_SETTINGS
when the user changes settings
@ Total
total water leak detection algorithm
#define HTTP_RQ_SETTINGS
begining of an HTTP request for changing settings
#define INDEX_FILE_NAME
file on the SD card containing the HTML website
@ BYPASS
when a bypass changes
String getFormatOfSettingsToSave()
Returns settings.
void saveSettings()
Saves settings on the SD card.
#define SS_SD_CARD
SD card pin (arduino documentation)
@ Low
low water leak detection algorithm
#define DATA_PREF
start of a sequence to be replaces with an actual value
int getNumberOfLeakDetections() const
Return the number of water leak detection algorithm.
String buff
buffer for parsing an HTTP request
void enableNotification(Type type, bool state)
Enables/disables the particular type of e-mail notification.
#define SETTINGS_FILE_NAME
the main settings file stored on the SD card
int processHTTPRequest(String &httpRequest)
Processes an HTTP request.
#define HTTP_RQ_GET_START
the very start of t
@ BOOTING
when the system boots up
#define HTTP_RQ_SEPARATOR
separator between individual parameters in an HTTP request
@ High
high water leak detection algorithm
std::vector< HTMLDataSource * > htmlDataSources
collection of classing providing data for the HTML content
void setReceiverEmailAddress(String email)
Changes the user's e-mail address.
#define HTTP_RQ_NOTIFICATION
begining of an HTTP request for changing e-mail notifications
LeaksController * leaksController
instance of LeaksController so it can be updated with each line read off the file
EthernetServer server
instance of EthernetServer listening on port 80
int setEthernet()
Sets up the ethernet shield.
@ LEAK_DETECTED
when a leak has been detected
static const String UNDEFINED_DATA
string "UNDEFINED"
void addHTMLSource(HTMLDataSource *source)
Adds another source of data for the HTML content.
void update() override
Updates the class.
unsigned long time_t
Referring to the data type unsigned long as time_t.
void update() override
Updates the class.
bool online
flag if the server is up and running
int processHTTPRequestSettings(int startPos, String &httpRequest, int numberOfValues)
Processes an HTTP request for changing settings of the water leak detection algorithms.
#define OUT_OF_RANGE_ERR
maximum integer values allowed to be passed to Arduino
#define SS_ETHERNET
etherent pin (arduino documentation)
@ VALVE_STATE
when the state of the valve has changed
@ RESET
when the device resets
@ APPLIED_NEW_SETTING
when the new settings are applied
String getFormatOfSettingsToSave()
Returns all settings.
static WebServer * getInstance()
Returns the instance of the class.
#define LITER_TO_PULSE(l)
Converts liters to pulses.
@ DAILY_OVERVIEW
daily overview about the system
#define MAX_HTTP_RQ_LENGTH
max size of an HTTP request (160B)
static WebServer * instance
the instance of the class
@ ALARM
when the state of the home alarm changes
#define DATA_POST
end of a sequence to be replaces with an actual value
const String replaceHTMLWithData(String html) const
Inserts a piece of data into a line of the HTML content.
#define BACKUP_SETTINGS_FILE_NAME
temporary backup file for the settings
byte mac[6]
MAC address of the Ethernet shield.
void changeSettings(ALeakDetectable::Type type, unsigned long detected, unsigned long reset, bool alarm)
Changes settings of a water leak detection algorithm.
static EmailSender * getInstance()
Returns the instance of the class.
WebServer()
Constructor of the class.
void setup(LeaksController *leaksController)
Sets the pointer to an instance of LeaksController.