-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay_control.ino
134 lines (121 loc) · 3.33 KB
/
relay_control.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "my_wifi_ssid";
const char* password = "my_wifi_password";
ESP8266WebServer server ( 80 );
const int relay = 13;
void handleRoot() {
String temp = "<html>\
<head>\
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />\
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>\
<script>\
$(document).ready(function(){\
$.get('status', function(data, status){\
if (parseInt(data) == 0) {\
$('#mainButton').html('OPEN');\
} else {\
$('#mainButton').html('CLOSE');\
};\
});\
var clicked = false;\
$('button').click(function(){\
if (!clicked){\
clicked = true;\
setTimeout(function(){ clicked = false; }, 2000);\
}else{\
return false;\
}\
$.get('status', function(data, status){\
if (parseInt(data) == 1) {\
$('#mainButton').html('OPEN');\
$.get('off', function(data1, status1){});\
} else {\
$('#mainButton').html('CLOSE');\
$.get('on', function(data2, status2){});\
};\
});\
});\
});\
</script>\
<title>NodeMCU HTTP relay</title>\
<style>\
body { background-size: cover; font-family: Arial, Helvetica, Sans-Serif; Color: #FFFFFF; }\
.button {\
background-color: #4CAF50;\
border: none;\
color: white;\
padding: 15px 32px;\
text-align: center;\
text-decoration: none;\
display: inline-block;\
font-size: 60px;\
margin: 4px 2px;\
cursor: pointer;\
}\
</style>\
</head>\
<body>\
<button id='mainButton' class='button'></button>\
</body>\
</html>";
server.send ( 200, "text/html", temp );
}
void handleNotFound() {
digitalWrite ( relay, 1 );
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) {
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.send ( 404, "text/plain", message );
digitalWrite ( relay, 0 );
}
void setup ( void ) {
pinMode ( relay, OUTPUT );
digitalWrite ( relay, 1 );
Serial.begin ( 115200 );
WiFi.begin ( ssid, password );
Serial.println ( "" );
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
if ( MDNS.begin ( "esp8266" ) ) {
Serial.println ( "MDNS responder started" );
}
server.on ( "/", handleRoot );
server.on ( "/status", []() {
String text;
text += digitalRead ( relay );
server.send ( 200, "text/plain", text );
} );
server.on ( "/on", []() {
digitalWrite ( relay, 1 );
server.send ( 200, "text/plain", "OK" );
} );
server.on ( "/off", []() {
digitalWrite ( relay, 0 );
server.send ( 200, "text/plain", "OK" );
} );
server.onNotFound ( handleNotFound );
server.begin();
Serial.println ( "HTTP server started" );
}
void loop ( void ) {
server.handleClient();
}