-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelais_display.ino
286 lines (269 loc) · 8.62 KB
/
relais_display.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = R"=====(
<!DOCTYPE html>
<html>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1.0'/>
<meta charset='utf-8'>
<style>
body {font-size:140%; }
#main {display: table; margin: auto; padding: 0 10px 0 10px; }
h2 {text-align:center; }
#button {
background: #3D94F6;
border-radius: 10px;
color: #FFFFFF;
font-family: Open Sans;
width: 140px;
font-size: 20px;
padding: 15px;
box-shadow: 2px 2px 5px black;
text-shadow: 2px 2px 5px black;
border: solid #337FED 1px;
}
.tooltip {
position: fixed;
font-family: Open Sans;
font-size: 14px;
bottom: 0;
right: 0;
}
.tooltip .tooltiptext {
opacity: 0;
transition: opacity 1s;
visibility: hidden;
width: auto;
background-color: black;
color: #fff;
text-align: center;
border-radius: 3px;
padding: 3px 0;
position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 1s;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
a:link {
color: darkgray;
}
</style>
<script>
function RELAIStoggle() {
var button_text = document.getElementById("button").value;
ajaxLoad('TOGGLE');
}
function RELAISon() {
var button_text = document.getElementById("button").value;
ajaxLoad('ON');
}
function RELAISoff() {
var button_text = document.getElementById("button").value;
ajaxLoad('OFF');
}
var ajaxRequest = null;
if (window.XMLHttpRequest) { ajaxRequest =new XMLHttpRequest(); }
else { ajaxRequest =new ActiveXObject("Microsoft.XMLHTTP"); }
function ajaxLoad(ajaxURL) {
if(!ajaxRequest){ alert("AJAX is not supported."); return; }
ajaxRequest.open("GET",ajaxURL,true);
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState == 4 && ajaxRequest.status==200) {
var ajaxResult = ajaxRequest.responseText;
if ( ajaxResult == "ON" ) { document.getElementById("button").value = "Turn off"; }
else if ( ajaxResult == "OFF" ) { document.getElementById("button").value = "Turn on"; }
document.getElementById("reply").innerHTML = ajaxResult;
}
}
ajaxRequest.send();
}
</script>
<link rel="shortcut icon" href="https://www.hugo.ro/favicon.ico" />
<title>Drrr Power Control</title>
</head>
<body>
<div id='main' align='center'>
<h2>Drrr Power Control</h2>
<div align="center">
<p id = "reply">Status: Relais is
)=====";
String html_2 = R"=====(
</p>
<p><input type="button" id = "button" onclick="RELAIStoggle()" value="Toggle" /></p>
<p><input type="button" id = "button" onclick="RELAISon()" value="Turn on" /></p>
<p><input type="button" id = "button" onclick="RELAISoff()" value="Turn off" /></p>
</div>
</div>
<div class="tooltip"><a href="mailto:[email protected]?subject=Ndnd Power Control">©2018</a>
<span class="tooltiptext">by Hugo and others</span>
</div>
</body>
</html>
)=====";
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ESP8266mDNS.h>
#include <SSD1306Wire.h>
#include <DNSServer.h>
#include <ESP8266HTTPClient.h>
#define RELAIS_PIN D1
#define TOUCH_PIN D7
SSD1306Wire display(0x3c, D6, D5);
WiFiServer server(80);
String request = "";
String hostname = "Drrr";
int disp = 1;
long mins = (millis() / 60000);
long minsOld = mins;
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
Serial.println("Opening configuration portal");
display.init();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 10, "# Entered config mode #");
display.drawString(0, 30, "ID/pass: NDND/pass4esp");
display.drawString(0, 40, "Config IP: 10.0.1.1");
display.display();
}
void setup() {
pinMode(RELAIS_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Serial started at 115200");
Serial.println();
WiFiManager wifiManager;
wifiManager.setTimeout(300);
wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
wifiManager.setDebugOutput(false);
wifiManager.setAPCallback(configModeCallback);
if(!wifiManager.autoConnect("NDND","pass4esp")) {
Serial.println("failed to connect and hit timeout");
ESP.reset();
delay(1000);
}
Serial.print(" secs in setup() connection result is ");
int connRes = WiFi.waitForConnectResult();
Serial.println(connRes);
if (WiFi.status()!=WL_CONNECTED) {
Serial.println("failed to connect, finishing setup anyway");
} else {
if (!MDNS.begin("esp8266")) { Serial.println("Error setting up MDNS responder!"); }
else { Serial.println("mDNS responder started"); }
server.begin();
Serial.println("Server started");
}
String WiFi_Name = WiFi.SSID();
IPAddress ip = WiFi.localIP();
char buf[16];
sprintf(buf, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
Serial.print("LAN IP: ");
Serial.println(buf);
String payload = "";
HTTPClient http; // GET Inet IP
http.begin("http://ipinfo.io/ip");
http.addHeader("Content-Type", "application/json");
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK) {
Serial.print("HTTP response code ");
Serial.println(httpCode);
payload = http.getString();
Serial.println("Inet IP: " + payload);
display.init();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, hostname + " connected to");
display.drawString(0, 10, "WiFi: " + WiFi_Name);
display.drawString(0, 20, "LAN IP: " + String("") + buf);
display.drawString(0, 30, "WAN IP: " + payload);
display.display();
}
http.end();
delay(50);
}
void loop() {
mins = (millis() / 60000);
int RELAIS_Pin = digitalRead(RELAIS_PIN);
if (mins <= (minsOld + 1)) {
display.normalDisplay();
display.setBrightness(255);
if (RELAIS_Pin == 0) {
display.setColor(BLACK);
display.drawString(0, 50, "Relais is ON");
display.setColor(WHITE);
display.drawString(0, 50, "Relais is OFF");
display.display();
} else if (RELAIS_Pin == 1){
display.setColor(BLACK);
display.drawString(0, 50, "Relais is OFF");
display.setColor(WHITE);
display.drawString(0, 50, "Relais is ON");
display.display();
}
} else {
display.setContrast(10,5,0);
}
int old = 0;
while (digitalRead(TOUCH_PIN) == 1) {
if ( old == 0) {
if (RELAIS_Pin == 0) {
digitalWrite(RELAIS_PIN, HIGH);
Serial.println("Manually switched Relais ON");
}
else if (RELAIS_Pin == 1) {
digitalWrite(RELAIS_PIN, LOW);;
Serial.println("Manually switched Relais OFF");
}
}
old = 1;
minsOld = mins;
}
WiFiClient client = server.available(); // Check if a client has connected
if (!client) { return; }
request = client.readStringUntil('\r'); // Read the first line of the request
Serial.print("request: "); Serial.println(request);
if (request.indexOf("ON") > 0) {
digitalWrite(RELAIS_PIN, HIGH);
client.print(header);
client.print("Status: Relais is ON");
Serial.println("Switched Relais ON");
} else if (request.indexOf("OFF") > 0) {
digitalWrite(RELAIS_PIN, LOW);
client.print(header);
client.print("Status: Relais is OFF");
Serial.println("Switched Relais OFF");
} else if (request.indexOf("TOGGLE") > 0) {
int RELAIS_Pin = digitalRead(RELAIS_PIN);
if (RELAIS_Pin == 0) {
digitalWrite(RELAIS_PIN, HIGH);
client.print(header);
client.print("Status: Relais is ON");
Serial.println("Switched Relais ON");
} else if (RELAIS_Pin == 1) {
digitalWrite(RELAIS_PIN, LOW);
client.print(header);
client.print("Status: Relais is OFF");
Serial.println("Switched Relais OFF");
}
} else {
client.flush();
client.print(header);
client.print(html_1);
if (RELAIS_Pin == 0) {
client.print("OFF");
} else {
client.print("ON");
}
client.print(html_2);
delay(50);
}
}