-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduinoHotWaterDisplay.ino
194 lines (151 loc) · 4.4 KB
/
ArduinoHotWaterDisplay.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
///////// CHANGEABLE VALUES /////////
const char serverAddress[] = "home-monitoring.scaleys.co.uk";
const int serverPort = 80;
const int httpRequestDelay = 5000;
const double minutesBetweenCalls = 1.0;
///////// CHANGEABLE VALUES ABOVE /////////
EthernetClient ethernetClient;
byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0xA1, 0xCF};
const char serviceEndpoint[] = "/hotwater";
const double millisecondsInAMinute = 60000.0;
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const int LCD_WIDTH = 16;
const String DECREASING = "DECREASING";
const String INCREASING = "INCREASING";
const String LEVEL = "LEVEL";
byte upArrow[8] = {
B00100,
B01110,
B11111,
B10101,
B00100,
B00100,
B00000,
};
byte downArrow[8] = {
B00000,
B00100,
B00100,
B10101,
B11111,
B01110,
B00100,
};
void setup() {
lcd.createChar(1, upArrow);
lcd.createChar(0, downArrow);
lcd.begin(LCD_WIDTH, 2);
lcd.setCursor(0,0);
Serial.begin(9600);
delay(1000);
connectToEthernet();
}
void connectToEthernet() {
// start the Ethernet connection:
bool connectedToNetwork = false;
while(!connectedToNetwork) {
Serial.println("Attempting to connect to network...");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to connect, trying again...");
} else {
Serial.println("Connected successfully");
connectedToNetwork = true;
}
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
//lcd.print("connecting...");
Serial.print("Connected to the network IP: ");
Serial.println(Ethernet.localIP());
//lcd.print("Network up...");
}
void loop() {
displayData();
Serial.println("fetched data");
delay(millisecondsInAMinute * minutesBetweenCalls);
}
void displayData() {
String data = receiveDataFromServer();
if(data.length() > 0) {
Serial.println("display data " + data);
lcd.clear();
lcd.home();
String tempStatus = "";
int statusIndex = 0;
if(data.indexOf(DECREASING) > 0) {
tempStatus = DECREASING;
statusIndex = data.indexOf(DECREASING);
data.replace(DECREASING, "");
} else if (data.indexOf(INCREASING) > 0){
tempStatus = INCREASING;
statusIndex = data.indexOf(INCREASING);
data.replace(INCREASING, "");
} else if (data.indexOf(LEVEL) > 0) {
tempStatus = LEVEL;
statusIndex = data.indexOf(LEVEL);
data.replace(LEVEL, "");
}
if(statusIndex == 0){
statusIndex = LCD_WIDTH;
}
String line1 = data.substring(0, statusIndex);
lcd.print(line1);
if(tempStatus == INCREASING) {
lcd.print((char) 1);
} else if (tempStatus == DECREASING) {
lcd.print((char) 0);
} else if (tempStatus == LEVEL) {
lcd.print("-");
}
if(data.length() > statusIndex) {
lcd.setCursor(0, 1);
statusIndex++;
lcd.print(data.substring(statusIndex, (statusIndex + LCD_WIDTH)));
}
}
}
String receiveDataFromServer() {
Serial.println("Request data from server");
String response = "";
if (ethernetClient.connect(serverAddress, serverPort)) {
Serial.println("connected to server");
// Make a HTTP request:
ethernetClient.print("GET ");
ethernetClient.print(serviceEndpoint);
ethernetClient.println(" HTTP/1.1");
ethernetClient.println("Host: " + String(serverAddress) + ":" + serverPort);
ethernetClient.println("Accept: text/html, text/plain");
ethernetClient.println("Pragma: no-cache");
ethernetClient.println("Cache-Control: no-cache");
ethernetClient.println("Connection: close");
ethernetClient.println();
Serial.println("Called server");
delay(httpRequestDelay);
String dataRead = "";
boolean reachedData = false;
while (ethernetClient.connected() || ethernetClient.available()) {
char c = ethernetClient.read();
//Serial.print(c);
if(reachedData) {
response += c;
//Serial.println(response);
}
dataRead += c;
if(dataRead.endsWith("\r\n\r\n")) {
Serial.println("Reached the data");
reachedData = true;
}
}
Serial.println("Finished reading data");
ethernetClient.stop();
ethernetClient.flush();
Serial.println("Closed connection");
}
Serial.println("response data to return is " + response);
return response;
}