Smart Poi 3.0.0
WiFi Connected LED POV Poi
Loading...
Searching...
No Matches
webServerSetupLogic.ino File Reference

Functions

bool checkFileSpace (size_t fileSize)
 
String formatBytes (size_t bytes)
 
String getContentType (String filename)
 
void handleOptions ()
 Handles CORS preflight (OPTIONS) requests.
 
void handleFileRead ()
 Handles file read requests from the server.
 
void handleFileUpload ()
 Handles file uploads to LittleFS File System.
 
void handleFileDelete ()
 Handles file deletion requests to the server.
 
void handleFileCreate ()
 Handles file creation requests to the server.
 
void handleFileList ()
 
void webServerSetupLogic (String router, String pass)
 Sets up the web server with routes for handling file operations, settings, and LED control.
 

Function Documentation

◆ checkFileSpace()

bool checkFileSpace ( size_t fileSize)
19{
20 // Get total available space on LittleFS
21 FSInfo fs_info;
22 LittleFS.info(fs_info);
23 size_t totalSpace = fs_info.totalBytes;
24 size_t availableSpace = fs_info.usedBytes;
25
26 // Calculate maximum allowed file size
27 size_t maxAllowedSize = totalSpace - maxPX - 1024; // reserve 24,000 bytes and some extra buffer
28
29 // Check if file size exceeds the max allowed size
30 if (fileSize > maxAllowedSize)
31 {
32 Serial.println("Error: File size exceeds max allowed size");
33 return false;
34 }
35 return true;
36}
const int maxPX
Definition main.ino:67

References maxPX.

Referenced by handleFileUpload().

Here is the caller graph for this function:

◆ formatBytes()

String formatBytes ( size_t bytes)
55{
56 if (bytes < 1024)
57 {
58 return String(bytes) + "B";
59 }
60 else if (bytes < (1024 * 1024))
61 {
62 return String(bytes / 1024.0) + "KB";
63 }
64 else if (bytes < (1024 * 1024 * 1024))
65 {
66 return String(bytes / 1024.0 / 1024.0) + "MB";
67 }
68 else
69 {
70 return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
71 }
72}

◆ getContentType()

String getContentType ( String filename)
92{
93 if (server.hasArg("download"))
94 return "application/octet-stream";
95 else if (filename.endsWith(".htm"))
96 return "text/html";
97 else if (filename.endsWith(".html"))
98 return "text/html";
99 else if (filename.endsWith(".css"))
100 return "text/css";
101 else if (filename.endsWith(".js"))
102 return "application/javascript";
103 else if (filename.endsWith(".png"))
104 return "image/png";
105 else if (filename.endsWith(".gif"))
106 return "image/gif";
107 else if (filename.endsWith(".jpg"))
108 return "image/jpeg";
109 else if (filename.endsWith(".ico"))
110 return "image/x-icon";
111 else if (filename.endsWith(".xml"))
112 return "text/xml";
113 else if (filename.endsWith(".pdf"))
114 return "application/x-pdf";
115 else if (filename.endsWith(".zip"))
116 return "application/x-zip";
117 else if (filename.endsWith(".gz"))
118 return "application/x-gzip";
119 else if (filename.endsWith(".bin"))
120 return "application/octet-stream";
121 return "text/plain";
122}
ESP8266WebServer server(80)

References server().

Referenced by handleFileRead().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleFileCreate()

void handleFileCreate ( )

Handles file creation requests to the server.

Manages the file creation process, including:

  • Checking for valid request arguments (at least one argument required)
  • Verifying the file path is not the root directory ("/") to prevent accidental creation
  • Checking if the file already exists using LittleFS.exists()
  • Creating the file using LittleFS.open() in write mode ("w")
  • Closing the file after creation
  • Returning a 200 response on successful creation
  • Returning error responses for:
    • Invalid requests (500, "BAD ARGS")
    • Bad paths (500, "BAD PATH")
    • File existence (500, "FILE EXISTS")
    • Creation failure (500, "CREATE FAILED")
Note
This function is not used at all in SmartPoi file uploading. todo: delete?
This function is called by the server to handle file creation requests.
Supports CORS requests and sends the necessary headers to allow cross-origin requests.
398{
399 server.sendHeader("Access-Control-Allow-Origin", "*");
400 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
401 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
402 server.sendHeader("Access-Control-Allow-Credentials", "true");
403 if (server.args() == 0)
404 return server.send(500, "text/plain", "BAD ARGS");
405 String path = server.arg(0);
406 // Serial.println("handleFileCreate: " + path);
407 if (path == "/")
408 return server.send(500, "text/plain", "BAD PATH");
409 if (LittleFS.exists(path))
410 return server.send(500, "text/plain", "FILE EXISTS");
411 File file = LittleFS.open(path, "w");
412 if (file)
413 file.close();
414 else
415 return server.send(500, "text/plain", "CREATE FAILED");
416 server.send(200, "text/plain", "");
417 path = String();
418}

References server().

Referenced by webServerSetupLogic().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleFileDelete()

void handleFileDelete ( )

Handles file deletion requests to the server.

Manages the file deletion process, including:

  • Checking for valid request arguments (at least one argument required)
  • Verifying the file path is not the root directory ("/") to prevent accidental deletion
  • Checking if the file exists before deletion using LittleFS
  • Deleting the file using LittleFS.remove()
  • Returning a 200 response on successful deletion
  • Returning error responses for:
    • Invalid requests (500, "BAD ARGS")
    • Bad paths (500, "BAD PATH")
    • File not found (404, "FileNotFound")
Note
This function is called by the server to handle file deletion requests.
Supports CORS requests and sends the necessary headers to allow cross-origin requests.
359{
360 server.sendHeader("Access-Control-Allow-Origin", "*");
361 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
362 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
363 server.sendHeader("Access-Control-Allow-Credentials", "true");
364 if (server.args() == 0)
365 return server.send(500, "text/plain", "BAD ARGS");
366 String path = server.arg(0);
367 // Serial.println("handleFileDelete: " + path);
368 if (path == "/")
369 return server.send(500, "text/plain", "BAD PATH");
370 if (!LittleFS.exists(path))
371 return server.send(404, "text/plain", "FileNotFound");
372 LittleFS.remove(path);
373 server.send(200, "text/plain", "");
374 path = String();
375}

References server().

Referenced by webServerSetupLogic().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleFileList()

void handleFileList ( )
435{
436 server.sendHeader("Access-Control-Allow-Origin", "*");
437 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
438 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
439 server.sendHeader("Access-Control-Allow-Credentials", "true");
440 if (!server.hasArg("dir"))
441 {
442 server.send(500, "text/plain", "BAD ARGS");
443 return;
444 }
445
446 String path = server.arg("dir");
447 // Serial.println("handleFileList: " + path);
448 Dir dir = LittleFS.openDir(path);
449 path = String();
450
451 String output = "[";
452 while (dir.next())
453 {
454 File entry = dir.openFile("r");
455 if (output != "[")
456 output += ',';
457 bool isDir = false;
458 output += "{\"type\":\"";
459 output += (isDir) ? "dir" : "file";
460 output += "\",\"name\":\"";
461 output += String(entry.name());
462 output += "\"}";
463 entry.close();
464 }
465
466 output += "]";
467
468 server.send(200, "text/json", output);
469}

References server().

Referenced by webServerSetupLogic().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleFileRead()

void handleFileRead ( )

Handles file read requests from the server.

Sends the requested file to the client with the appropriate headers. Supports CORS requests and sends the necessary headers to allow cross-origin requests.

Note
This function is used to handle file read requests from the server.
Sends the following headers:
  • Access-Control-Allow-Origin: * (allows requests from all origins)
  • Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, FETCH (allows multiple methods)
  • Access-Control-Allow-Headers: Content-Type (allows Content-Type header in requests)
  • Access-Control-Allow-Credentials: true (allows credentials in requests)
Returns a 500 error if no file argument is provided.
Returns a 404 error if the file is not found.
Uses LittleFS to read the file from storage.
163{
164 Serial.println("handleFileRead");
165 server.sendHeader("Access-Control-Allow-Origin", "*");
166 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
167 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
168 server.sendHeader("Access-Control-Allow-Credentials", "true");
169
170 if (!server.hasArg("file"))
171 {
172 Serial.println("no args detected");
173 server.send(500, "text/plain", "BAD ARGS");
174 return;
175 }
176 Serial.print("handleFileRead: ");
177 String path = server.arg("file");
178 Serial.println("handleFileRead: " + path);
179 // if (path.endsWith("/"))
180 // path += "index.htm";
181 String contentType = getContentType(path);
182 Serial.println("contentType: " + contentType);
183 // String pathWithGz = path + ".gz";
184 if (LittleFS.exists(path))
185 {
186 File file = LittleFS.open(path, "r");
187 size_t sent = server.streamFile(file, contentType);
188 file.close();
189 Serial.println("Found File: " + path);
190 return;
191 }
192 Serial.println("Couldn't find file " + path);
193 return;
194}
String getContentType(String filename)
Definition webServerSetupLogic.ino:91

References getContentType(), and server().

Referenced by webServerSetupLogic().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleFileUpload()

void handleFileUpload ( )

Handles file uploads to LittleFS File System.

Manages the file upload process, including:

  • Checking for valid filenames (single character present in images string)
  • Tracking file size and checking against maximum allowed size (checkFileSpace())
  • Writing data to the file (if within size limits)
  • Handling upload completion, abortion, and errors
Note
This function is called by the ESP8266 server to handle file uploads.
Supports CORS requests and sends the necessary headers to allow cross-origin requests.
Uses LittleFS to read and write files.
220{
221 if (server.uri() != "/edit")
222 return;
223
224 HTTPUpload &upload = server.upload();
225
226 // Initialize file size tracking variable
227 static size_t fileSize = 0;
228
229 if (upload.status == UPLOAD_FILE_START)
230 {
231 server.sendHeader("Access-Control-Allow-Origin", "*");
232 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
233 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
234 server.sendHeader("Access-Control-Allow-Credentials", "true");
235
236 Serial.println("uploadCounter is: ");
237 Serial.println(uploadCounter);
239
240 String filename = upload.filename;
241 if (!filename.startsWith("/"))
242 {
243 filename = "/" + filename;
244 }
245 Serial.print("handleFileUpload Name: ");
246 Serial.println(filename);
247
248 // Check if filename is a single character present in images string
249 if (filename.length() != 2 || images.indexOf(filename[1]) == -1)
250 {
251 Serial.println("Error: Invalid filename");
252 server.send(400, "text/plain", "Invalid filename");
253 return;
254 }
255
256 // Reset file size tracking
257 fileSize = 0;
258
259 // Open file for writing
260 fsUploadFile = LittleFS.open(filename, "w");
261 filename = String();
262 }
263 else if (upload.status == UPLOAD_FILE_WRITE)
264 {
265 // Track total size of the file being written
266 fileSize += upload.currentSize;
267 // Check if file size exceeds the max allowed size
268 if (!checkFileSpace(fileSize))
269 {
270 // Close the file and delete it
271 if (fsUploadFile)
272 {
273 fsUploadFile.close();
274 LittleFS.remove(upload.filename); // Remove the partially written file
275 }
276
277 // Send error response and return
278 server.send(500, "text/plain", "File size exceeds limit");
279 return;
280 }
281
282 // Check if file exceeds the maxPX size
283 if (fileSize > maxPX)
284 {
285 Serial.println("Error: File size exceeds maxPX limit");
286
287 // Close the file and delete it
288 if (fsUploadFile)
289 {
290 fsUploadFile.close();
291 LittleFS.remove(upload.filename); // Remove the partially written file
292 }
293
294 // Send error response and return
295 server.send(500, "text/plain", "File size exceeds limit");
296 return;
297 }
298
299 // Proceed with writing data if within the size limit
300 if (fsUploadFile)
301 {
302 fsUploadFile.write(upload.buf, upload.currentSize);
303 }
304 }
305 else if (upload.status == UPLOAD_FILE_END)
306 {
307 Serial.println("UPLOAD FINISHED");
308
309 if (fsUploadFile)
310 {
311 fsUploadFile.close();
312 }
313
314 // Reset upload counter and file size tracker
315 uploadCounter = 1;
316 fileSize = 0;
317
318 server.send(200, "text/plain", "Upload successful");
319 }
320 else if (upload.status == UPLOAD_FILE_ABORTED)
321 {
322 // Handle aborted uploads by closing and removing the partially written file
323 Serial.println("UPLOAD ABORTED");
324
325 if (fsUploadFile)
326 {
327 fsUploadFile.close();
328 LittleFS.remove(upload.filename); // Remove the incomplete file
329 Serial.println("Aborted file removed");
330 }
331
332 // Send an error response for aborted upload
333 server.send(500, "text/plain", "Upload aborted");
334
335 // Reset upload counter and file size tracker
336 uploadCounter = 1;
337 fileSize = 0;
338 }
339}
String images
Definition main.ino:177
int uploadCounter
Definition main.ino:180
File fsUploadFile
Definition main.ino:23
bool checkFileSpace(size_t fileSize)
Definition webServerSetupLogic.ino:18

References checkFileSpace(), fsUploadFile, images, maxPX, server(), and uploadCounter.

Referenced by webServerSetupLogic().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleOptions()

void handleOptions ( )

Handles CORS preflight (OPTIONS) requests.

Sends the necessary headers to respond to CORS preflight requests. This allows cross-origin requests from web applications.

Note
This function is used to handle CORS preflight requests.
Sends the following headers:
  • Access-Control-Allow-Origin: * (allows requests from all origins)
  • Access-Control-Allow-Methods: GET, POST, OPTIONS (allows GET, POST, and OPTIONS methods)
  • Access-Control-Allow-Headers: Content-Type (allows Content-Type header in requests)
Returns a 204 No Content response to indicate successful preflight.
138{
139 // This is needed to respond to CORS preflight (OPTIONS) requests
140 server.sendHeader("Access-Control-Allow-Origin", "*");
141 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
142 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
143 server.send(204); // No content response
144}

References server().

Referenced by webServerSetupLogic().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ webServerSetupLogic()

void webServerSetupLogic ( String router,
String pass )

Sets up the web server with routes for handling file operations, settings, and LED control.

Initializes the web server with routes for:

  • Handling file operations:
    • Listing files (GET /list)
    • Reading files (GET /edit)
    • Creating files (PUT /edit)
    • Deleting files (DELETE /edit)
  • Managing settings:
    • Router setting (GET /router)
    • Pattern setting (GET /pattern)
    • Interval setting (GET /intervalChange)
    • Brightness setting (GET /brightness)
  • Controlling LED behavior
Parameters
routerThe router setting to use
passThe password to use
Note
This function sets up the web server and its routes, but does not start the server.
492{
493 int checkChannel;
494 checkChannel = int(EEPROM.read(13));
495 int newChannel = checkChannel;
496
497 File html = LittleFS.open("/site.htm", "r");
498 responseHTML = "";
499
500 if (!html)
501 {
502 Serial.println("Failed to open file for reading");
503 }
504 else
505 {
506 size_t fileSize = html.size();
507 responseHTML.reserve(fileSize);
508
509 // Read the entire file into responseHTML
510 while (html.available())
511 {
512 responseHTML += (char)html.read(); // Read one character at a time and append it to the string
513 }
514
515 html.close();
516 Serial.println("Finished building html");
517 }
518
519 // SERVER INIT
520 server.on("/", HTTP_OPTIONS, handleOptions); // Handle CORS preflight requests
521 // list directory
522 server.on("/list", HTTP_GET, handleFileList);
523
524 // load editor
525 server.on("/edit", HTTP_GET, handleFileRead);
526
527 // create file
528 server.on("/edit", HTTP_PUT, handleFileCreate);
529
530 // delete file
531 server.on("/edit", HTTP_DELETE, handleFileDelete);
532
533 // Handle file uploads
534 server.on(
535 "/edit", HTTP_POST, []() {
536
537 },
539
540 // called when the url is not defined here - captive portal
541 server.onNotFound([]()
542 {
543 server.sendHeader("Access-Control-Allow-Origin", "*");
544 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
545 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
546 server.sendHeader("Access-Control-Allow-Credentials", "true");
547 server.send(200, "text/html", responseHTML); // sends webpage for eeprom settings if url not defined. Captive portal.
548 });
549
550 // settings - returns in format SSID, PASS, Channel, A, B, C, D, Pattern - ABCD is IP address numbers
551 server.on("/returnsettings", []()
552 {
553 server.sendHeader("Access-Control-Allow-Origin", "*");
554 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
555 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
556 server.sendHeader("Access-Control-Allow-Credentials", "true");
557 statusCode = 200;
558
559 settings = LittleFS.open("/settings.txt", "r");
560 String settingsSSID = settings.readStringUntil('\n');
561 String settingsPASS = settings.readStringUntil('\n');
562
563 settings.close();
564 // delay(100);
565 int newChannel = int(EEPROM.read(13));
566 content = settingsSSID + "," + settingsPASS + "," + newChannel + "," + addrNumA + "," + addrNumB + "," + addrNumC + "," + addrNumD + "," + patternChooser;
567 server.send(statusCode, "text/html", content); });
568
569 // to activate in browser: http://192.168.1.78/router?router=1
570 // don't forget main: http://192.168.1.1/router?router=1
571 // nothing happens, but router is now switched on.
572
573 // to deactivate in browser: http://192.168.8.78/router?router=0 *use actual ip address
574 // don't forget main: http://192.168.8.79/router?router=0 *use actual ip address
575
576 // Router settings changes
577 server.on("/router", []()
578 {
579 server.sendHeader("Access-Control-Allow-Origin", "*");
580 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
581 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
582 server.sendHeader("Access-Control-Allow-Credentials", "true");
583 String onRouter = server.arg("router"); // todo: need to handle errors what if it's too big
584 if (onRouter.length() > 0)
585 {
586 EEPROM.write(100, 0); // clearing... Is this necessary? make it some sort of default then in case of errors..?
587 EEPROM.commit();
588 int newRouter;
589 newRouter = onRouter.toInt();
590 if (newRouter == 0 || newRouter > 1)
591 { // set to not work here! Deliberate?
592 newRouter = 0;
593 routerOption = false;
594 }
595 else
596 { // 1 for router
597 routerOption = true;
598 }
599
600 EEPROM.write(100, newRouter);
601 content = "{\"Success\":\" your pattern is set \"}";
602 statusCode = 200;
603
604 // Send the response based on the logic above
605 server.send(statusCode, "application/json", content);
606 }
607 else
608 {
609 content = "{\"Error\":\"404 not found\"}";
610 statusCode = 404;
611
612
613 // Send the error response
614 server.send(statusCode, "application/json", content);
615 }
616 EEPROM.commit(); // save for next time?
617 // Serial.println("10, patternChooser saved");
618 // black, this could take a while, so save power? Also an indicator...
619 FastLED.showColor(CRGB::Black); });
620
621 // Pattern settings changes
622 server.on("/pattern", []()
623 {
624 Serial.println("pattern change requested");
625 server.sendHeader("Access-Control-Allow-Origin", "*");
626 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
627 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
628 server.sendHeader("Access-Control-Allow-Credentials", "true");
629 String onAddress = server.arg("patternChooserChange"); // need to handle errors what if it's too big
630 if (onAddress.length() > 0)
631 {
632 EEPROM.write(10, 1); // clearing... Is this necessary? make it some sort of default then in case of errors..?
633 EEPROM.commit();
634 // change address:
635 int newPatt; // temp variable - not needed, use same one as previously:
636 newPatt = onAddress.toInt();
637 // if newPatt < max patt and also > 0 { //add this check here or null()
638 patternChooser = newPatt; // change on poi as well as saving
639 EEPROM.write(10, newPatt);
640 if (newPatt < 6 && newPatt > 0)
641 {
643 EEPROM.write(11, newPatt);
644 }
645 else
646 {
647 // do something here??
648 // pattern++; //for loadPatternChooser change pattern why not! (may remove this I don't know)
649 }
650 EEPROM.commit(); // save for next time?
651 // black, this could take a while, so save power? Also an indicator...
652 // FastLED.showColor(CRGB::Black);
653 // loadPatternChooser();
654 content = "{\"Success\":\" your pattern is set \"}";
655 statusCode = 200;
656
657 // Send the response
658 server.send(statusCode, "application/json", content);
659 }
660 else
661 {
662
663 content = "{\"Error\":\"404 not found\"}";
664 statusCode = 404;
665
666 // Send the error response
667 server.send(statusCode, "application/json", content);
668 } // nothing
669 });
670
671 // Interval settings changes
672 server.on("/intervalChange", []()
673 {
674 server.sendHeader("Access-Control-Allow-Origin", "*");
675 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
676 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
677 server.sendHeader("Access-Control-Allow-Credentials", "true");
678 String newInterval = server.arg("interval");
679 if (newInterval.length() > 0)
680 {
681 // Todo: save and change this in EEPROM?
682 // EEPROM.write(10, 1); //clearing... Is this necessary? make it some sort of default then in case of errors..?
683 // EEPROM.commit();
684 // change address:
685 int tmp = newInterval.toInt();
686 if (tmp < 1)
687 {
688 interval = 500; // change every half second - shortest interval available
689 }
690 else if (tmp > 1800)
691 { // 30 minutes maximum
692 interval = 1800 * 1000;
693 }
694 else
695 {
696 interval = tmp * 1000; // how many seconds
697 }
698 content = "{\"Success\":\" your interval is set \"}";
699 statusCode = 200;
700
701 // Send the response
702 server.send(statusCode, "application/json", content);
703 }
704 else
705 {
706 content = "{\"Error\":\"404 not found\"}";
707 statusCode = 404;
708 // Send the error response
709 server.send(statusCode, "application/json", content);
710 } // nothing
711 });
712
713 // Brightness Settings changes
714 server.on("/brightness", []()
715 {
716 server.sendHeader("Access-Control-Allow-Origin", "*");
717 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
718 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
719 server.sendHeader("Access-Control-Allow-Credentials", "true");
720 String onNewBRT = server.arg("brt");
721 if (onNewBRT.length() > 0)
722 {
723 newBrightness = onNewBRT.toInt();
724 if (newBrightness > 254)
725 {
726 newBrightness = 255;
727 }
728 if (newBrightness < 20)
729 {
730 newBrightness = 20;
731 }
732
733 FastLED.setBrightness(newBrightness); // should I be removing this becos https://github.com/FastLED/FastLED/wiki/FastLED-Temporal-Dithering
734 FastLED.showColor(CRGB::Black);
735
736 EEPROM.write(15, newBrightness);
737 EEPROM.commit(); // save for next time
738
739 content = "{\"Success\":\" your brightness is set \"}";
740 statusCode = 200;
741
742 // Send the response
743 server.send(statusCode, "application/json", content);
744 }
745 else
746 {
747
748 content = "{\"Error\":\"404 not found\"}";
749 statusCode = 404;
750
751 // Send the error response
752 server.send(statusCode, "application/json", content);
753 } // nothing
754 });
755 // Update settings in format SSID, PASS, Channel, A, B, C, D, Pattern - ABCD is IP address numbers
756 server.on("/setting", []()
757 {
758 server.sendHeader("Access-Control-Allow-Origin", "*");
759 server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, FETCH");
760 server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
761 server.sendHeader("Access-Control-Allow-Credentials", "true");
762
763 settings = LittleFS.open("/settings.txt", "w");
764 if (!settings)
765 {
766 // Serial.println("settings file open failed");
767 // todo: return something useful here
768 }
769
770 settings.print(server.arg("ssid"));
771 // todo: in order to change channel below we need to overwrite the router name and password? FIX!
772 settings.print("\n");
773 // note: never use settings.println, use print("\n") instead
774
775 // Serial.print("saving ssid: ");
776 // Serial.println(server.arg("ssid"));
777
778 settings.print(server.arg("pwd"));
779 settings.print("\n");
780
781 // Serial.print("saving pwd: ");
782 // Serial.println(server.arg("pwd"));
783
784 settings.close();
785 /////////////////////////////////////////////end spiffs settings write//////////////////////////////////////////////////
786
787 // change channel setting in EEPROM://///////////////////////////////////////////////////////////////////////////////
788 String onChannel = server.arg("channel"); // need to handle errors what if it's too big
789 if (onChannel.length() > 0)
790 {
791
792 EEPROM.write(13, 1); // clearing
793
794 EEPROM.commit();
795 int newChannel2; // temp variable
796 newChannel2 = onChannel.toInt();
797 EEPROM.write(13, newChannel2);
798
799 EEPROM.commit();
800 }
801 else
802 {
803 content = "{\"Error\":\"404 not found\"}";
804 statusCode = 404;
805 ////Serial.println("Sending 404");
806 ////Serial.println("not found");
807 }
808 // change address setting in EEPROM://///////////////////////////////////////////////////////////////////////////////
809 String onAddress = server.arg("address"); // need to handle errors what if it's too big
810 if (onAddress.length() > 0)
811 {
812
813 EEPROM.write(14, 1); // clearing
814
815 EEPROM.commit();
816
817 // change address:
818 int newAddr2; // temp variable
819 newAddr2 = onAddress.toInt();
820 EEPROM.write(14, newAddr2);
821
822 EEPROM.commit();
823
824 ////Serial.print("onChannel is now: ");
825 ////Serial.println(newChannel2);
826
827 content = "{\"Success\":\" now switch both poi off and on again\"}";
828 statusCode = 200;
829 // add in patternChooser variable here, choose offline patterns in app!
830 // ESP.restart(); //not using this right now but see https://github.com/esp8266/Arduino/issues/1722#issuecomment-192829825
831 }
832 else
833 {
834 content = "{\"Error\":\"404 not found\"}";
835 statusCode = 404;
836 ////Serial.println("Sending 404");
837 ////Serial.println("not found");
838 }
839 onAddress = server.arg("addressA"); // need to handle errors what if it's too big
840 if (onAddress.length() > 0)
841 {
842 EEPROM.write(16, 1); // clearing... Is this necessary? make it some sort of default then in case of errors..?
843 EEPROM.commit();
844 // change address:
845 int newAddrA; // temp variable - not needed, use same one as previously:
846 newAddrA = onAddress.toInt();
847 EEPROM.write(16, newAddrA);
848 EEPROM.commit();
849 // Serial.println("16");
850 }
851 else
852 {
853 } // nothing
854
855 onAddress = server.arg("addressB"); // need to handle errors what if it's too big
856 if (onAddress.length() > 0)
857 {
858 EEPROM.write(17, 1); // clearing... Is this necessary? make it some sort of default then in case of errors..?
859 EEPROM.commit();
860 // change address:
861 int newAddrB; // temp variable - not needed, use same one as previously:
862 newAddrB = onAddress.toInt();
863 EEPROM.write(17, newAddrB);
864 EEPROM.commit();
865 // Serial.println("17");
866 }
867 else
868 {
869 } // nothing
870
871 onAddress = server.arg("addressC"); // need to handle errors what if it's too big
872 if (onAddress.length() > 0)
873 {
874 EEPROM.write(18, 1); // clearing... Is this necessary? make it some sort of default then in case of errors..?
875 EEPROM.commit();
876 // change address:
877 int newAddrC; // temp variable - not needed, use same one as previously:
878 newAddrC = onAddress.toInt();
879 EEPROM.write(18, newAddrC);
880 EEPROM.commit();
881 // Serial.println("18");
882 }
883 else
884 {
885 } // nothing
886
887 onAddress = server.arg("patternChooserChange"); // need to handle errors what if it's too big
888 if (onAddress.length() > 0)
889 {
890 EEPROM.write(10, 1); // clearing... Is this necessary? make it some sort of default then in case of errors..?
891 EEPROM.commit();
892 // change address:
893 int newPatt; // temp variable - not needed, use same one as previously:
894 newPatt = onAddress.toInt();
895 // if newPatt < max patt and also > 0 { //add this check here or null()
896 patternChooser = newPatt; // change on poi as well as saving
897 EEPROM.write(10, newPatt);
898 if (newPatt < 6 && newPatt > 0)
899 {
901 EEPROM.write(11, newPatt);
902 }
903 else
904 {
905 // do something here??
906 // pattern++; //for loadPatternChooser change pattern why not! (may remove this I don't know)
907 }
908 EEPROM.commit(); // save for next time?
909 // Serial.println("10, patternChooser saved");
910 // black, this could take a while, so save power? Also an indicator...
911 // FastLED.showColor(CRGB::Black);
912 // loadPatternChooser();
913 }
914 else
915 {
916 } // nothing
917
918 // Send the status code response
919 server.send(statusCode, "application/json", content);
920 // delay(50);
921 // ESP.restart(); //not using this right now but see https://github.com/esp8266/Arduino/issues/1722#issuecomment-192829825
922 });
923
924 server.begin();
925}
int patternChooser
Definition main.ino:156
uint8_t addrNumB
Definition main.ino:105
int newBrightness
Definition main.ino:39
String content
Definition main.ino:118
File settings
Definition main.ino:47
long interval
Definition main.ino:127
String responseHTML
Definition main.ino:116
int statusCode
Definition main.ino:119
uint8_t addrNumA
Definition main.ino:104
uint8_t addrNumC
Definition main.ino:106
boolean routerOption
Definition main.ino:185
int pattern
Definition main.ino:157
uint8_t addrNumD
Definition main.ino:107
void handleFileDelete()
Handles file deletion requests to the server.
Definition webServerSetupLogic.ino:358
void handleFileRead()
Handles file read requests from the server.
Definition webServerSetupLogic.ino:162
void handleFileUpload()
Handles file uploads to LittleFS File System.
Definition webServerSetupLogic.ino:219
void handleFileList()
Definition webServerSetupLogic.ino:434
void handleOptions()
Handles CORS preflight (OPTIONS) requests.
Definition webServerSetupLogic.ino:137
void handleFileCreate()
Handles file creation requests to the server.
Definition webServerSetupLogic.ino:397

References addrNumA, addrNumB, addrNumC, addrNumD, content, handleFileCreate(), handleFileDelete(), handleFileList(), handleFileRead(), handleFileUpload(), handleOptions(), interval, newBrightness, pattern, patternChooser, responseHTML, routerOption, server(), settings, and statusCode.

Referenced by littleFSLoadSettings().

Here is the call graph for this function:
Here is the caller graph for this function: