-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjectorControl.ino
More file actions
302 lines (253 loc) · 9.94 KB
/
ProjectorControl.ino
File metadata and controls
302 lines (253 loc) · 9.94 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include <ArduinoJson.h>
// WebServer object on port 80; we'll create it later once the WiFi Manager has found a network
ESP8266WebServer *myServer;
bool handleForm(); // web server handler for parameters from form
bool handleRoot(); // web server handler for root document
String getContentType(String filename); // convert the file extension to the MIME type
bool handleFileRead(String path); // send the right file to the client (if it exists)
// This structure holds the config parms
struct Config {
unsigned int projpin; // which GPIO triggers projector
unsigned int projpulse; // millseconds to pulse the projector relay
unsigned int campin; // which GPIO triggers camera
unsigned int campulse; // how long to pulse the camera trigger
unsigned int slides; // number of slides to scan
unsigned int slideinterval; // pause after taking pic before advancing to next slide
unsigned int settle; // milliseconds to wait after advancing before taking pic
};
const char *cfgfile = "/config.json";
Config cfg; // <- global configuration object
// pre-declare config functions
void loadConfiguration(const char *filename, Config &config);
void saveConfiguration(const char *filename, const Config &config);
void printFile(const char *filename);
// pre-declare the function that controls the sequence of camera / projector operations
bool runSlideScanner();
// processing web page
const String holding1 = "<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"";
const String holding2 = ";URL=/\"></head><body><p>Slide scanning in progress. Estimated time to complete: ";
const String holding3 = " seconds</p></body></html>";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset settings - for testing
//wifiManager.resetSettings();
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
wifiManager.setTimeout(180);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if(!wifiManager.autoConnect("ESP8266","password")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("Connected to WiFi:)");
// Initialize SPIFFS
if(!SPIFFS.begin()) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Should load default config if run for the first time
Serial.println(F("Loading configuration..."));
loadConfiguration(cfgfile, cfg);
// Create configuration file
Serial.println(F("Saving configuration..."));
saveConfiguration(cfgfile, cfg);
// Start a webserver on port 80
myServer = new ESP8266WebServer(80);
myServer->on("/action", HTTP_POST, handleForm);
myServer->onNotFound([]() { // If the client requests any URI
if (!handleFileRead(myServer->uri())) // send it if it exists
myServer->send(404, "text/plain", "404: Not Found"); // otherwise, respond with a 404 (Not Found) error
});
myServer->begin(); // Actually start the server
Serial.println("HTTP server started");
}
void loop(void) {
myServer->handleClient();
}
String getContentType(String filename) { // convert the file extension to the MIME type
if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".ico")) return "image/x-icon";
return "text/plain";
}
bool handleRoot() {
return handleFileRead("index.html");
}
bool handleFileRead(String path) { // send the right file to the client (if it exists)
Serial.println("handleFileRead: " + path);
if (path.endsWith("/")) path += "index.html"; // If a folder is requested, send the index file
String contentType = getContentType(path); // Get the MIME type
if (SPIFFS.exists(path)) { // If the file exists
File file = SPIFFS.open(path, "r"); // Open it
size_t sent = myServer->streamFile(file, contentType); // And send it to the client
file.close(); // Then close the file again
return true;
}
Serial.println("\tFile Not Found");
return false; // If the file doesn't exist, return false
}
bool handleForm() {
Serial.println("handleForm: ");
if (!myServer->hasArg("button")) {
myServer->send(400, "text/plain", "400: Invalid Request");
return false;
} else {
// we know we've got a button click; not sure if it's 'Save' or 'Start'
// but either way, let's unpack the parameters
cfg.projpin = myServer->arg("projpin").toInt();
cfg.projpulse = myServer->arg("projpulse").toInt();
cfg.slides = myServer->arg("slides").toInt();
cfg.slideinterval = myServer->arg("slideinterval").toInt();
cfg.campin = myServer->arg("campin").toInt();
cfg.campulse = myServer->arg("campulse").toInt();
cfg.settle = myServer->arg("settle").toInt();
if (myServer->arg("button").startsWith("Save")) {
// update saved configuration parameteres
saveConfiguration(cfgfile, cfg);
return handleFileRead("/saved.html");
} else if (myServer->arg("button").startsWith("Start")) {
return runSlideScanner(
cfg.projpin,
cfg.projpulse,
cfg.slides,
cfg.slideinterval,
cfg.campin,
cfg.campulse,
cfg.settle
);
}
}
}
// Loads the configuration from a file
void loadConfiguration(const char *filename, Config &config) {
// Open file for reading
File file = SPIFFS.open(filename, "r");
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<256> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
if (error)
Serial.println(F("Failed to read file, using default configuration"));
// Copy values from the JsonDocument to the Config
config.projpin = doc["projpin"]; // 2
config.projpulse = doc["projpulse"]; // 350
config.slides = doc["slides"]; // 80
config.slideinterval = doc["slideinterval"]; // 3000
config.campin = doc["campin"]; // 4
config.campulse = doc["campulse"]; // 350
config.settle = doc["settle"]; // 500
// Close the file (Curiously, File's destructor doesn't close the file)
file.close();
}
// Saves the configuration to a file
void saveConfiguration(const char *filename, const Config &config) {
// Delete existing file, otherwise the configuration is appended to the file
SPIFFS.remove(filename);
// Open file for writing
File file = SPIFFS.open(filename, "w");
if (!file) {
Serial.println(F("Failed to create file"));
return;
}
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<256> doc;
// Set the values in the document
doc["projpin"] = config.projpin;
doc["projpulse"] = config.projpulse;
doc["slides"] = config.slides;
doc["slideinterval"] = config.slideinterval;
doc["campin"] = config.campin;
doc["campulse"] = config.campulse;
doc["settle"] = config.settle;
// Serialize JSON to file
if (serializeJson(doc, file) == 0) {
Serial.println(F("Failed to write to file"));
}
// Close the file
file.close();
}
// Prints the content of a file to the Serial
void printFile(const char *filename) {
// Open file for reading
File file = SPIFFS.open(filename, "r");
if (!file) {
Serial.println(F("Failed to read file"));
return;
}
// Extract each characters by one by one
while (file.available()) {
Serial.print((char)file.read());
}
Serial.println();
// Close the file
file.close();
}
bool runSlideScanner(
unsigned int projpin,
unsigned int projpulse,
unsigned int slides,
unsigned int slideinterval,
unsigned int campin,
unsigned int campulse,
unsigned int settle
) {
Serial.println("Running processing batch with the following parameters:");
Serial.println(projpin);
Serial.println(projpulse);
Serial.println(slides);
Serial.println(slideinterval);
Serial.println(campin);
Serial.println(campulse);
Serial.println(settle);
// Initialise projector relay
pinMode(cfg.projpin, OUTPUT);
digitalWrite(cfg.projpin, HIGH);
// Initialise camera relay
pinMode(cfg.campin, OUTPUT);
digitalWrite(cfg.campin, HIGH);
Serial.println("Relay switches should now be open");
// put up a holding web page, with an indication of time to complete (ttc)
unsigned int ttc =
(slides * (projpulse + settle + campulse + slideinterval) / 1000) + 1;
myServer->send(200, "text/html", holding1 + ttc + holding2 + ttc + holding3);
for (int slide=0; slide < slides; slide++) {
// Advance slide
digitalWrite(projpin, LOW); // closed
delay(projpulse);
digitalWrite(projpin, HIGH); // open again
// Pause to settle camera
delay(settle);
// Trigger camera
digitalWrite(campin, LOW); // closed
delay(campulse);
digitalWrite(campin, HIGH); // open again
// Wait before advancing to next slide
delay(slideinterval);
}
return true;
}