Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <limits.h>
- #include <time.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdint.h>
- #include <Arduino.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <FS.h>
- #include <ESP8266HTTPUpdateServer.h>
- #include "web_server.h"
- ESP8266WebServer server(80);
- ESP8266HTTPUpdateServer httpUpdater;
- const char* update_path = "/firmware";
- const char* update_username = "admin";
- const char* update_password = "admin";
- JsonCommandsBuffer JsonResults;
- bool isConnected = false;
- String getContentType(String filename){
- if(server.hasArg("download")) return "application/octet-stream";
- else if(filename.endsWith(".htm")) return "text/html";
- else if(filename.endsWith(".html")) return "text/html";
- else if(filename.endsWith(".json")) return "text/json";
- else if(filename.endsWith(".css")) return "text/css";
- else if(filename.endsWith(".js")) return "application/javascript";
- else if(filename.endsWith(".gz")) return "application/x-gzip";
- return "text/plain";
- }
- void handleRoot() {
- server.send(200, "text/plain", "hello from esp82633!");
- }
- File fsUploadFile;
- bool handleFileRead(String path){
- if(path.endsWith("/")) path += "index.htm";
- String contentType = getContentType(path);
- String pathWithGz = path + ".gz";
- if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
- if(SPIFFS.exists(pathWithGz))
- path += ".gz";
- File file = SPIFFS.open(path, "r");
- size_t sent = server.streamFile(file, contentType);
- file.close();
- return true;
- }
- return false;
- }
- void handleFileUpload(){
- if(server.uri() != "/edit") return;
- HTTPUpload& upload = server.upload();
- if(upload.status == UPLOAD_FILE_START){
- String filename = upload.filename;
- if(!filename.startsWith("/")) filename = "/"+filename;
- fsUploadFile = SPIFFS.open(filename, "w");
- filename = String();
- } else if(upload.status == UPLOAD_FILE_WRITE){
- if(fsUploadFile)
- fsUploadFile.write(upload.buf, upload.currentSize);
- } else if(upload.status == UPLOAD_FILE_END){
- if(fsUploadFile)
- fsUploadFile.close();
- }
- }
- void handleWebServerRequests()
- {
- if(isConnected)
- {
- server.handleClient();
- }
- }
- bool initWebServer()
- {
- // Wait for connection
- int ledVal = LOW;
- Serial.print("INFO:IP address: ");
- Serial.println(WiFi.localIP());
- if (MDNS.begin("device2")) {
- Serial.println("MDNS responder started");
- }
- MDNS.addService("http", "tcp", 80); // Announce esp tcp service on port 8080
- server.on("/", handleRoot);
- server.on("/reloadconfig", HTTP_GET, []() {
- if(realoadConfiguration(&Serial))
- server.send ( 200, "text/json", "{\"success\":\"true\"}" );
- else
- server.send ( 200, "text/json", "{\"success\":\"false\"}" );
- });
- server.on("/devset", HTTP_GET, []() {
- if (server.hasArg("device") == false) { //Check if body received
- server.send(200, "text/json", "{ \"success\":\"false\", \"error\" : \"Device not set\"}");
- return;
- }
- if (server.hasArg("value") == false) {
- server.send(200, "text/json", "{\"success\":\"false\", \"error\" : \"Statuc not set\"}");
- return;
- }
- Serial.print(F("devset "));
- Serial.print(server.arg("device"));
- Serial.print(F(" "));
- Serial.println(server.arg("value"));
- server.send ( 200, "text/json", "{\"success\":\"true\"}" );
- });
- server.on("/status", HTTP_GET, []() {
- String res = "[";
- for(int i = 0; i < JsonResults.size(); i++)
- {
- if(i > 0) res += ",";
- res += JsonResults[i];
- }
- res += "]";
- JsonResults.clear();
- server.send ( 200, "text/json", res );
- });
- server.on("/devsreq", HTTP_GET, []() {
- if (server.hasArg("ids") == false) { //Check if body received
- server.send(200, "text/json", "{\"success\":\"false\", \"error\" = 'Device not set'}");
- return;
- }
- Serial.print("devsget ");
- Serial.println(server.arg("ids"));
- server.send ( 200, "text/json", "{\"success\":\"true\"}" );
- });
- server.on("/inline", []() {
- Serial.println("inline request");
- server.send(200, "text/plain", "this works as well");
- });
- server.on("/edit", HTTP_GET, [](){
- if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound");
- });
- server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleFileUpload);
- server.onNotFound([](){
- if(!handleFileRead(server.uri()))
- server.send(404, "text/plain", "FileNotFound");
- });
- httpUpdater.setup(&server, update_path, update_username, update_password);
- server.begin();
- isConnected = true;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement