2021-06-23 16:44:37 +03:00
|
|
|
#include "server.hpp"
|
|
|
|
#include <FS.h>
|
|
|
|
|
|
|
|
#define DBG_OUTPUT_PORT Serial
|
|
|
|
|
|
|
|
|
|
|
|
String getContentType(String filename){
|
2021-06-27 21:46:11 +03:00
|
|
|
if(filename.indexOf(".htm") > 0) return "text/html";
|
|
|
|
else if(filename.indexOf(".html")> 0) return "text/html";
|
|
|
|
else if(filename.indexOf(".css")> 0) return "text/css";
|
|
|
|
else if(filename.indexOf(".js")> 0) return "application/javascript";
|
|
|
|
else if(filename.indexOf(".png")> 0) return "image/png";
|
|
|
|
else if(filename.indexOf(".gif")> 0) return "image/gif";
|
|
|
|
else if(filename.indexOf(".jpg")> 0) return "image/jpeg";
|
|
|
|
else if(filename.indexOf(".ico")> 0) return "image/x-icon";
|
|
|
|
else if(filename.indexOf(".xml")> 0) return "text/xml";
|
|
|
|
else if(filename.indexOf(".pdf")> 0) return "application/x-pdf";
|
2021-06-23 16:44:37 +03:00
|
|
|
return "text/plain";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool handleFileRead(String path ){
|
|
|
|
DBG_OUTPUT_PORT.println("handleFileRead: " + path);
|
2021-06-25 09:14:06 +03:00
|
|
|
if(path.endsWith("/")) path += "index.html";
|
2021-06-27 21:46:11 +03:00
|
|
|
String contentType = getContentType(path) + ";charset=utf-8";
|
|
|
|
if(LittleFS.exists(path)){
|
|
|
|
DBG_OUTPUT_PORT.println("handle: " + path);
|
2021-06-23 16:44:37 +03:00
|
|
|
File file = LittleFS.open(path, "r");
|
2021-06-27 21:46:11 +03:00
|
|
|
if(path.endsWith(".gz")){
|
|
|
|
//server.sendHeader("Content-Encoding" , "gzip");
|
|
|
|
} else {
|
|
|
|
contentType += ";charset=utf-8";
|
|
|
|
}
|
|
|
|
server.streamFile(file, contentType);
|
2021-06-23 16:44:37 +03:00
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleHours(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:46:11 +03:00
|
|
|
int LDRPin = A0;
|
|
|
|
int LDRReading = 0;
|
|
|
|
|
|
|
|
int milisInterval = 2000;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
void getData() {
|
|
|
|
//This is a JSON formatted string that will be served. You can change the values to whatever like.
|
|
|
|
// {"data":[{"dataValue":"1024"},{"dataValue":"23"}]} This is essentially what is will output you can add more if you like
|
|
|
|
Serial.println("Data start");
|
|
|
|
LDRReading = analogRead(LDRPin);
|
|
|
|
String text2 = "{\"data\":[";
|
|
|
|
text2 += "{\"dataValue\":\"";
|
|
|
|
text2 += "LDRReading";
|
|
|
|
text2 += "\"},";
|
|
|
|
text2 += "{\"dataValue\":\"";
|
|
|
|
text2 += "count";
|
|
|
|
text2 += "\"}";
|
|
|
|
text2 += "]}";
|
|
|
|
Serial.println("Data end");
|
|
|
|
server.send(200, "text/html", text2);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2021-06-23 16:44:37 +03:00
|
|
|
void server_setup(){
|
2021-06-27 21:46:11 +03:00
|
|
|
LittleFS.begin();
|
|
|
|
server.on("/data", getData);
|
|
|
|
server.on("/hour", handleHours);
|
|
|
|
|
|
|
|
server.begin();
|
|
|
|
Serial.println("HTTP server started");
|
|
|
|
|
|
|
|
server.onNotFound([](){
|
|
|
|
if(!handleFileRead(server.uri()))
|
|
|
|
server.send(404, "text/plain", "FileNotFound");
|
|
|
|
});
|
2021-06-23 16:44:37 +03:00
|
|
|
}
|
2021-06-27 21:46:11 +03:00
|
|
|
|
|
|
|
void server_loop() {
|
|
|
|
server.handleClient();
|
|
|
|
}
|