-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
239 lines (213 loc) · 6.57 KB
/
main.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <Arduino.h>
#include <TFT_eSPI.h>
#include "Adafruit_GFX.h"
#include <WiFi.h>
#include <Wire.h>
#include <HTTPClient.h>
#include <base64.h>
#include <CST816D.h>
#define I2C_SDA 4
#define I2C_SCL 5
#define TP_INT 0
#define TP_RST 1
#define off_pin 35
TFT_eSPI tft = TFT_eSPI();
CST816D touch(I2C_SDA, I2C_SCL, TP_RST, TP_INT);
unsigned long activityTime = 0;
String lastTemperature = "";
String auth = ":999999";
String encodedAuth = base64::encode(auth);
String plus = "+";
String minus = "-";
bool initialFetchDone = false;
void drawBorder() {
int outerRadius = 120;
int borderThickness = 10;
int innerRadius = outerRadius - borderThickness;
int startR = 0x10;
int startG = 0xFF;
int startB = 0xE0;
for (int radius = outerRadius; radius > innerRadius; radius--) {
float fadeFactor = float(outerRadius - radius) / borderThickness;
uint16_t fadedColor = tft.color565(
int(startR * (1 - fadeFactor)),
int(startG * (1 - fadeFactor)),
int(startB * (1 - fadeFactor))
);
tft.drawCircle(120, 120, radius, fadedColor);
}
}
void displayError(String message) {
tft.fillScreen(TFT_BLACK);
drawBorder();
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE);
tft.setCursor(30, 100);
tft.print(message);
Serial.println("Error: " + message);
}
void handleHTTPError(int httpCode) {
Serial.println("Error on HTTP request");
tft.fillScreen(TFT_BLACK);
drawBorder();
tft.setTextColor(TFT_WHITE);
tft.setTextSize(3);
tft.setCursor(50, 90);
tft.print(httpCode);
}
void updateDisplay(String temperature) {
tft.fillScreen(TFT_BLACK);
drawBorder();
tft.setTextSize(4);
tft.setTextColor(TFT_WHITE);
tft.setCursor(110, 20);
tft.print(plus);
tft.setCursor(110, 200);
tft.print(minus);
tft.setTextSize(3);
tft.setCursor(41, 90);
tft.print("Temp Set:\n\n " + temperature + " F");
}
void fetchAndDisplayTemperature() {
HTTPClient http;
int maxRetries = 5;
int retries = 0;
while (retries < maxRetries) {
http.begin("http://192.168.0.23");
http.addHeader("Authorization", "Basic " + encodedAuth);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
String identifier = "id='tempSet'>";
int startIndex = payload.indexOf(identifier);
if (startIndex != -1) {
startIndex += identifier.length();
int endIndex = payload.indexOf("</span>", startIndex);
if (endIndex != -1) {
String temperature = payload.substring(startIndex, endIndex);
if (!initialFetchDone) {
// Run this block only on the initial fetch
updateDisplay(temperature);
lastTemperature = temperature;
initialFetchDone = true; // Set the flag to true
}
if (temperature != lastTemperature) {
updateDisplay(temperature);
lastTemperature = temperature;
}
http.end(); // Close the HTTP connection
return; // Successful response, exit the loop
} else {
displayError("End tag not found");
}
} else {
displayError("Temperature data not found");
}
} else {
displayError("HTTP GET failed, retrying...");
delay(2000); // Wait for a while before retrying
}
http.end(); // Close the HTTP connection
retries++;
}
// If it fails more than 5 times, restart the ESP
displayError("HTTP GET failed more than 5 times, restarting ESP...");
ESP.restart();
}
void checkForInactivity() {
if (millis() - activityTime > 10000) {
analogWrite(TFT_BL, 5);
}
}
void increaseTemp() {
HTTPClient http;
http.begin("http://192.168.0.23/increase");
http.addHeader("Authorization", "Basic " + encodedAuth);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
fetchAndDisplayTemperature();
} else {
handleHTTPError(httpCode);
}
http.end();
}
void decreaseTemp() {
HTTPClient http;
http.begin("http://192.168.0.23/decrease");
http.addHeader("Authorization", "Basic " + encodedAuth);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
fetchAndDisplayTemperature();
} else {
handleHTTPError(httpCode);
}
http.end();
}
void handleTouch(uint16_t touchX, uint16_t touchY) {
uint16_t touchColorInactive = tft.color565(0x10, 0xE60, 0xE0);
uint16_t touchColorActive = tft.color565(0xFF, 0xFF, 0xFF);
// Calculate the touch zones for increaseTemp and decreaseTemp
int screenHeight = tft.height(); // Get the screen height (240)
int touchZoneHeight = screenHeight / 4; // Divide the screen into 4 equal parts
int upperTouchZoneY = 0; // Upper 1/4 of the screen
int lowerTouchZoneY = 3 * touchZoneHeight; // Lower 1/4 of the screen
if (touchY >= upperTouchZoneY && touchY <= (upperTouchZoneY + touchZoneHeight)) {
// Touch zone for increaseTemp (upper 1/4 of the screen)
tft.fillRoundRect(90, upperTouchZoneY, 60, touchZoneHeight, 5, touchColorActive);
tft.setTextColor(TFT_BLACK); // Set text color
tft.setTextSize(3);
tft.setCursor(110, 20);
tft.print(plus);
increaseTemp();
} else if (touchY >= lowerTouchZoneY && touchY <= (lowerTouchZoneY + touchZoneHeight)) {
// Touch zone for decreaseTemp (lower 1/4 of the screen)
tft.fillRoundRect(90, lowerTouchZoneY, 60, touchZoneHeight, 5, touchColorActive);
tft.setTextColor(TFT_BLACK); // Set text color
tft.setTextSize(3);
tft.setCursor(110, 200);
tft.print(minus);
decreaseTemp();
} else {
// No touch on the zones, set them to inactive
tft.fillRoundRect(90, upperTouchZoneY, 60, touchZoneHeight, 5, touchColorInactive);
tft.fillRoundRect(90, lowerTouchZoneY, 60, touchZoneHeight, 5, touchColorInactive);
tft.setTextColor(TFT_BLACK);
tft.setTextSize(3);
tft.setCursor(110, 20);
tft.print(plus);
tft.setCursor(110, 200);
tft.print(minus);
}
}
void touch_read() {
bool touched;
uint8_t gesture;
uint16_t touchX, touchY;
touched = touch.getTouch(&touchX, &touchY, &gesture);
if (touched) {
fetchAndDisplayTemperature();
activityTime = millis();
analogWrite(TFT_BL, 255);
handleTouch(touchX, touchY);
}
}
void setup() {
Wire.begin(I2C_SDA, I2C_SCL);
analogWrite(TFT_BL, 255);
WiFi.begin("SSID", "Password");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
tft.init();
tft.setRotation(0);
}
void loop() {
if (!initialFetchDone) {
fetchAndDisplayTemperature();
}
checkForInactivity();
touch_read();
Serial.println("looping");
}