-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266-gpio-control.ino
218 lines (185 loc) · 5.4 KB
/
esp8266-gpio-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
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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <ArduinoJson.h>
struct PinMapping {
const String name;
int value;
};
PinMapping pinMappings[] = {
{ "D0", 16 },
{ "D1", 5 },
{ "D2", 4 },
{ "D3", 0 },
{ "D4", 2 },
{ "D5", 14 },
{ "D6", 12 },
{ "D7", 13 },
{ "D8", 15 },
{ "D9", 3 },
{ "D10", 1 }
};
const char* apSSID = "NodeMCU";
ESP8266WebServer server(80);
char ssid[32];
char password[64];
IPAddress ip(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 4, 1);
void setup() {
EEPROM.begin(512);
Serial.begin(115200);
// Remove this for using Serial
for (int i = 0; i < sizeof(pinMappings) / sizeof(pinMappings[0]); i++) {
int pin = pinMappings[i].value;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
if (loadCredentialsFromEEPROM()) {
connectToWiFi();
} else {
createAP();
}
server.on("/", HTTP_GET, handleRoot);
server.on("/save", HTTP_POST, handleSave);
server.on("/control", HTTP_GET, handleControl);
server.on("/delete", HTTP_GET, handleDelete);
server.begin();
}
void loop() {
server.handleClient();
}
void createAP() {
Serial.println("Setting AP...");
WiFi.softAP(apSSID);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
}
void handleRoot() {
String html = "<html><body>";
html += "<h1>WiFi config</h1>";
html += "<form method='post' action='/save'>";
html += "SSID: <input type='text' name='ssid'><br>";
html += "Password: <input type='password' name='password'><br>";
html += "IP: <input type='text' name='ip'><br>";
html += "Subnet mask: <input type='text' name='subnet'><br>";
html += "Gateway: <input type='text' name='gateway'><br>";
html += "<input type='submit' value='Save'>";
html += "</form>";
html += "<form method='get' action='/delete'>";
html += "<input type='submit' value='Delete WiFi Credentials (AP mode)'>";
html += "</form>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleSave() {
String newSSID = server.arg("ssid");
String newPassword = server.arg("password");
String newIP = server.arg("ip");
String newSubnet = server.arg("subnet");
String newGateway = server.arg("gateway");
newSSID.toCharArray(ssid, 32);
newPassword.toCharArray(password, 64);
ip.fromString(newIP);
subnet.fromString(newSubnet);
gateway.fromString(newGateway);
saveCredentialsToEEPROM();
connectToWiFi();
server.send(200, "text/plain", "Configuration saved. Restarting NodeMCU...");
delay(1000);
ESP.restart();
}
void handleDelete() {
clearCredentialsFromEEPROM();
server.send(200, "text/plain", "WiFi credentials deleted. Restarting NodeMCU...");
delay(1000);
ESP.restart();
}
bool loadCredentialsFromEEPROM() {
EEPROM.get(0, ssid);
EEPROM.get(32, password);
EEPROM.get(64, ip);
EEPROM.get(128, subnet);
EEPROM.get(160, gateway);
if (strlen(ssid) > 0 && strlen(password) > 0 && ip != IPAddress(0,0,0,0) && subnet != IPAddress(0,0,0,0) && gateway != IPAddress(0,0,0,0)) {
return true;
}
return false;
}
void saveCredentialsToEEPROM() {
EEPROM.put(0, ssid);
EEPROM.put(32, password);
EEPROM.put(64, ip);
EEPROM.put(128, subnet);
EEPROM.put(160, gateway);
EEPROM.commit();
}
void clearCredentialsFromEEPROM() {
for (int i = 0; i < 512; i++) {
EEPROM.write(i, 0);
}
EEPROM.commit();
}
void connectToWiFi() {
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
Serial.print("Connecting to WiFi network: ");
Serial.println(ssid);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED) {
if (millis() - startAttemptTime >= 10000) {
Serial.println("\nFailed to connect to WiFi. Entering AP mode...");
createAP();
return;
}
delay(1000);
Serial.print(".");
}
Serial.println("\nSuccessful connection");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
}
// ---- GPIO handle ----
void handleControl() {
if (server.args() > 0) {
String pinStr = server.arg("pin");
String action = server.arg("action");
const int pin = getPinValue(pinStr);
Serial.print("Pin value: ");
Serial.println(pin);
if (pin != 255) {
if (action == "on") {
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
String json_response = "{\"status\":\"Pin " + pinStr + " on\"}";
server.send(200, "application/json", json_response);
} else if (action == "off") {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
String json_response = "{\"status\":\"Pin " + pinStr + " off\"}";
server.send(200, "application/json", json_response);
} else {
String json_response = "{\"error\":\"Invalid action\"}";
server.send(400, "application/json", json_response);
}
} else {
String json_response = "{\"error\":\"Invalid port number\"}";
server.send(400, "application/json", json_response);
}
} else {
String json_response = "{\"error\":\"Insufficient parameters\"}";
server.send(400, "application/json", json_response);
}
}
// Función para obtener el valor del pin a partir del nombre
int getPinValue(const String pinName) {
for (int i = 0; i < sizeof(pinMappings) / sizeof(pinMappings[0]); i++) {
if (pinName == pinMappings[i].name) {
return pinMappings[i].value;
}
}
return 255;
}