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
|
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include "memoryAccess.h"
#include "style.h" // custom stylesheet, get it from stylesheet
#include "index.h" // Index page html, get it from MAIN_page
#include "settings.h" // Settings page html, get it from SETTINGS_page
// User variables
const String MemoryAccessVersion = "cp-a1";
const String deviceType = "clapSensor"; // This is what kind of device it is, crucial for the homeServer when determing which API to use
const int wifiConnectionTimeout = 5; // n*5 sec
char ssid[30];
char password[30];
char APssid[30];
String deviceName;
String deviceLocation;
String deviceId;
memoryAccess MemoryAccess;
const int lampPin = 14; // Pin the appliance is connected to
const int sensorPin = 5; // Pin the sensor is connected to
bool lampOn; // Is the appliance turned on?
bool sensorActive; // Should we care about sensor input?
bool buttonState; // Used to make button not flash indefinetly
ESP8266WebServer server ( 80 ); // webServer on port 80
void setup ( void ) {
// Activate serial
Serial.begin ( 115200 );
MemoryAccess.init();
MemoryAccess.dump();
if (MemoryAccess.readAscii("version")) {
deviceName = MemoryAccess.readAscii("deviceName");
deviceLocation = MemoryAccess.readAscii("deviceLocation");
deviceId = MemoryAccess.readAscii("deviceId");
MemoryAccess.readAscii("SSID").toCharArray(ssid, 30);
MemoryAccess.readAscii("Password").toCharArray(password, 30);
(deviceType + "-" + deviceId).toCharArray(APssid, 30);
} else {
// SET TO DEFAULT VALUES
MemoryAccess.writeAscii("version", MemoryAccessVersion);
MemoryAccess.writeAscii("deviceName", "");
MemoryAccess.writeAscii("deviceLocation", "");
MemoryAccess.writeAscii("deviceGuid", uniqId());
MemoryAccess.writeAscii("SSID", "");
MemoryAccess.writeAscii("Password", "");
MemoryAccess.commit();
Serial.println("Have set default values, making watchdog boot us!");
while (1);
}
Serial.print("ssid: ");
Serial.println(ssid);
Serial.print("Password: ");
Serial.println(password);
Serial.print("deviceName: ");
Serial.println(deviceName);
Serial.print("deviceLocation: ");
Serial.println(deviceLocation);
Serial.print("deviceId: ");
Serial.println(deviceId);
// Setup pins and set default values
pinMode ( lampPin, OUTPUT );
pinMode ( sensorPin, INPUT );
lampOn = false; // Vil at denne skal bli husket, altså være samme etter boot
sensorActive = true;
digitalWrite(lampPin, lampOn);
digitalWrite(sensorPin, sensorActive);
// Try to connect to WiFi
WiFi.begin ( ssid, password );
// Wait for connection
int connTime = 0;
while ( WiFi.status() != WL_CONNECTED ) {
if (connTime >= wifiConnectionTimeout) { break; }
delay ( 1000 );
Serial.print ( "." );
connTime++;
}
Serial.println ( "" );
if (connTime >= wifiConnectionTimeout) {
WiFi.softAP(APssid); // add password here as second parameter, currently just a open hotspot
IPAddress myIP = WiFi.softAPIP();
Serial.print("APssid: ");
Serial.println(APssid);
Serial.print("AP IP address: ");
Serial.println(myIP);
} else {
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
}
if ( MDNS.begin ( "esp8266" ) ) {
Serial.println ( "MDNS responder started" );
}
// Gui
server.on ( "/", handleRoot );
server.on ( "/settings", handleSettings );
// Json
server.on ( "/j/", handleJson );
server.on ( "/j", handleJson );
// Others
server.on ( "/style.css", handleStylesheet);
server.on ( "/inline", []() {
server.send ( 200, "text/plain", "this works as well" );
} );
server.onNotFound ( handleNotFound );
server.begin();
Serial.println ( "HTTP server started" );
}
void loop ( void ) {
server.handleClient();
if (sensorActive) {
bool currButtonState = digitalRead(sensorPin);
if (currButtonState == true && buttonState == false) {
setLamp("TOGGLE");
}
buttonState = currButtonState;
}
}
String uniqId() {
return "AAAAAAAAAA";
}
void setLamp(String action) {
if (action == "ON") {
lampOn = true;
} else if (action == "OFF") {
lampOn = false;
} else if (action == "TOGGLE") {
lampOn = !lampOn;
}
digitalWrite(lampPin, lampOn);
}
void setSensor(String action) {
if (action == "ON") {
sensorActive = true;
} else if (action == "OFF") {
sensorActive = false;
}
}
void handleRoot() {
String htmlPage = MAIN_page;
htmlPage.replace("{{NAME}}", deviceName);
htmlPage.replace("{{Location}}", deviceLocation);
server.send ( 200, "text/html", htmlPage);
}
void handleJson() {
// escape dobbel tøddler med en \ slik \" for å ha dobbel tøddler i en string
for (int i = 0; i < server.args(); i++) {
String argKey = server.argName(i);
String argVal = server.arg(i);
if (argKey == "lamp") {
if (argVal == "1" || argVal == "true") { setLamp("ON"); }
else { setLamp("OFF"); }
} else if (argKey == "sens") {
if (argVal == "1" || argVal == "true") { setSensor("ON"); }
else { setSensor("OFF"); }
}
}
String jsonAnswer = "{";
jsonAnswer += "\"deviceName\":\"";
jsonAnswer += deviceName;
jsonAnswer += "\",\"deviceId\":\"";
jsonAnswer += deviceId;
jsonAnswer += "\",\"deviceType\":\"";
jsonAnswer += deviceType;
jsonAnswer += "\",\"deviceLocation\":\"";
jsonAnswer += deviceLocation;
jsonAnswer += "\",\"lampOn\":\"";
jsonAnswer += (String)lampOn;
jsonAnswer += "\",\"sensorOn\":\"";
jsonAnswer += (String)sensorActive;
jsonAnswer += "\"}";
server.send ( 200, "text/html", jsonAnswer);
}
void handleSettings() {
String successMsg = "";
String type, _deviceName, _deviceLocation, _ssid, _password;
for (int i = 0; i < server.args(); i++) {
String argKey = server.argName(i);
String argVal = server.arg(i);
if (argKey == "txtDeviceName") {
_deviceName = argVal;
} else if (argKey == "txtDeviceLocation") {
_deviceLocation = argVal;
} else if (argKey == "txtSSID") {
_ssid = argVal;
} else if (argKey == "txtPassword") {
_password = argVal;
}
}
if (type == "settings") {
if (_deviceName != "") {
MemoryAccess.writeAscii("deviceName", _deviceName);
} else if (_deviceLocation != "") {
MemoryAccess.writeAscii("deviceLocation", _deviceLocation);
} else if (_ssid != "") {
MemoryAccess.writeAscii("SSID", _ssid);
} else if (_password != "") {
MemoryAccess.writeAscii("Password", _password);
}
MemoryAccess.commit();
successMsg = "Settings updated!";
while (1);
}
String htmlResponse = SETTINGS_page;
htmlResponse.replace("{{NAME}}", deviceName);
htmlResponse.replace("{{SUCCESSMSG}}", successMsg);
htmlResponse.replace("{{DEVICENAME}}", deviceName);
htmlResponse.replace("{{DEVICELOCATION}}", deviceLocation);
server.send( 200, "text/html", htmlResponse );
}
void handleStylesheet() {
String style = stylesheet;
server.send ( 200, "text/css", style);
}
void handleNotFound() {
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 );
}
|