-
Notifications
You must be signed in to change notification settings - Fork 6
/
NodeMCU-Relay.ino
171 lines (147 loc) · 3.97 KB
/
NodeMCU-Relay.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ArduinoJson.h>
// D7 = Relay
/////////////////// CHANGE THESE VALUES //////////////////////
// Required:
const char* ssid = "SSID"; // Name of your network
const char* password = "PASSWORD"; // Password for your network
const String relay = "HIGH"; // Relay type (`HIGH` or `LOW`)
const char* mdns = "relay"; // mDNS name
// For Modulation:
const uint32_t modulationOn = 5000; // Time (in ms) for relay to be ON when modulating
const uint32_t modulationOff = 20000; // Time (in ms) for relay to be OFF when modulating
// For Momentary:
const int momentaryOn = 1000; // Delay time (in ms) for the ON state for MOMENTARY
const int momentaryOff = 1000; // Delay time (in ms) for the OFF state for MOMENTARY
//////////////////////////////////////////////////////////////
const int relayPin = 13;
int state = 0;
bool ignoreMe = false;
int relayOn, relayOff;
bool led_blinking, led_on;
uint32_t last_toggle;
ESP8266WebServer server(80);
void setup() {
if (relay.equals("LOW")) {
relayOn = 0;
relayOff = 1;
} else {
relayOn = 1;
relayOff = 0;
}
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, relayOff);
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.println("Connecting to \"" + String(ssid) + "\"");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(String(++i) + " ");
}
Serial.println();
Serial.println("Connected successfully");
// Print the IP address
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (!MDNS.begin(mdns)) {
Serial.println("Error setting up MDNS responder!");
}
Serial.println("mDNS address: " + String(mdns) + ".local");
server.on("/setState", []() {
String type = server.arg("type");
state = server.arg("value").toInt();
if (type.equals("modulation")) {
if (state) {
ignoreMe = false;
start_blinking();
} else {
ignoreMe = false;
stop_blinking();
}
}
if (type.equals("momentary")) {
if (state) {
stop_blinking();
digitalWrite(relayPin, relayOn);
delay(momentaryOn);
digitalWrite(relayPin, relayOff);
ignoreMe = false;
} else {
stop_blinking();
digitalWrite(relayPin, relayOn);
delay(momentaryOff);
digitalWrite(relayPin, relayOff);
ignoreMe = false;
}
}
if (type.equals("switch")) {
if (state) {
stop_blinking();
digitalWrite(relayPin, relayOn);
ignoreMe = true;
} else {
stop_blinking();
digitalWrite(relayPin, relayOff);
ignoreMe = false;
}
}
server.send(200);
});
server.on("/status", []() {
size_t capacity = JSON_OBJECT_SIZE(1);
DynamicJsonDocument doc(capacity);
doc["currentState"] = state;
String json;
serializeJson(doc, json);
server.send(200, "application/json", json);
});
// Start the server
server.begin();
}
//Start of modulation functions
void update_led() {
uint32_t now = millis();
if (!led_blinking && !ignoreMe) {
digitalWrite(relayPin, relayOff);
led_on = false;
last_toggle = now - modulationOff;
return;
}
if (led_on && now - last_toggle >= modulationOn && !ignoreMe) {
digitalWrite(relayPin, relayOff);
led_on = false;
last_toggle = now;
}
if (!led_on && now - last_toggle >= modulationOff && !ignoreMe) {
digitalWrite(relayPin, relayOn);
led_on = true;
last_toggle = now;
}
}
void start_blinking() {
digitalWrite(relayPin, relayOn);
led_blinking = true;
led_on = true;
last_toggle = millis();
}
void stop_blinking() {
digitalWrite(relayPin, relayOff);
led_blinking = false;
led_on = false;
}
//End of modulation functions
//Main loop
void loop() {
update_led();
server.handleClient();
MDNS.update();
}