-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266-IoT-wifi-switch.ino
193 lines (165 loc) · 4.72 KB
/
ESP8266-IoT-wifi-switch.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
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include <EEPROM.h>
#include <ArduinoJson.h>
#include <AsyncJson.h>
const String ssid = "ESP8266-IoT";
const String password = "########";
const uint ADDR = 0, MAX_NAME_LEN = 20, MAX_DEVICES = 9;
const byte AVAILABLE_PINS[MAX_DEVICES] = { 0, 2, 4, 5, 12, 13, 14, 15, 16 };
struct Device {
byte pin;
char name[MAX_NAME_LEN];
Device &operator=(Device &device)
{
pin = device.pin;
strncpy(name, device.name, MAX_NAME_LEN);
return *this;
}
};
struct Data {
byte count;
Device devices[MAX_DEVICES];
bool addDevice(byte pin, String name) {
if (count >= MAX_DEVICES) {
count = MAX_DEVICES;
return false;
}
for(size_t i = 0; i<count; i++) if(devices[i].pin == pin) return false;
for (size_t i = 0; i < MAX_DEVICES; i++) if(pin == AVAILABLE_PINS[i]) {
devices[count].pin = pin;
snprintf(devices[count].name, MAX_NAME_LEN, "%s", name.c_str());
count++;
return true;
}
return false;
}
bool removeDevice(size_t pos) {
if (count <= 0) {
count = 0;
return false;
}
if (pos >= count) {
return false;
}
for (size_t i = pos; i < count - 1; i++) {
devices[i] = devices[i + 1];
}
count--;
return true;
}
DynamicJsonDocument toJson() {
DynamicJsonDocument doc(1024); //JSON_OBJECT_SIZE(2) + JSON_ARRAY_SIZE(count) + count * JSON_OBJECT_SIZE(2)
doc["count"] = count;
for (size_t i = 0; i < count; i++) {
JsonObject obj = doc["devices"].createNestedObject();
obj["name"] = devices[i].name;
obj["pin"] = devices[i].pin;
}
return doc;
}
size_t find(byte pin) {
for (size_t i = 0; i < count; i++)
if (devices[i].pin == pin)
return i;
return -1;
}
} data;
AsyncWebServer server(80);
void setup() {
Serial.begin(9600);
WiFi.softAP(ssid, password);
EEPROM.begin(sizeof(Data));
EEPROM.get(ADDR, data);
Serial.println(String(data.count) + " devices : ");
for (size_t i = 0; i < data.count; i++) {
Serial.println(String(data.devices[i].pin) + " : " + String(data.devices[i].name));
}
server.on("/getDevices", [](AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream("application/json");
EEPROM.get(ADDR, data);
serializeJson(data.toJson(), *response);
request->send(response);
});
server.on("/addDevice", [](AsyncWebServerRequest *request) {
if (request->hasParam("pin") && request->hasParam("name"))
{
EEPROM.get(ADDR, data);
byte pin = request->getParam("pin")->value().toInt();
size_t idx = data.find(pin);
if (idx != -1)
{
Serial.println("Pin " + String(pin) + " already registered to " + data.devices[idx].name);
request->send(400);
} else {
String name = request->getParam("name")->value();
if(data.addDevice(pin, name))
{
EEPROM.put(ADDR, data);
EEPROM.commit();
AsyncResponseStream *response = request->beginResponseStream("application/json");
serializeJson(data.toJson(), *response);
request->send(response);
} else {
Serial.println("something went wrong");
request->send(500);
}
}
}
else
{
Serial.println("name/pin not provided");
request->send(400);
}
});
server.on("/removeDevice", [](AsyncWebServerRequest *request) {
if (request->hasParam("pin"))
{
byte pin = request->getParam("pin")->value().toInt();
EEPROM.get(ADDR, data);
size_t idx = data.find(pin);
if (idx == -1)
{
Serial.println(String(pin) + " not found");
request->send(404);
}
else
{
if(data.removeDevice(idx))
{
EEPROM.put(ADDR, data);
EEPROM.commit();
AsyncResponseStream *response = request->beginResponseStream("application/json");
serializeJson(data.toJson(), *response);
request->send(response);
}
else
{
Serial.println("something went wrong");
request->send(500);
}
}
}
else
{
Serial.println("pin not provided");
request->send(400);
}
});
server.on("/getAvailablePins", [](AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream("application/json");
StaticJsonDocument<JSON_ARRAY_SIZE(MAX_DEVICES)> doc;
JsonArray array = doc.to<JsonArray>();
EEPROM.get(ADDR, data);
for(size_t i=0; i<MAX_DEVICES; i++) {
if (data.find(AVAILABLE_PINS[i]) == -1) {
array.add(AVAILABLE_PINS[i]);
}
}
serializeJson(doc, *response);
request->send(response);
});
server.begin();
}
void loop() {
}