Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- #include<string.h>
- #include <FS.h>
- #include <DNSServer.h>
- //STRUCTURES
- struct Message
- {
- char comment[140];
- char author[20];
- };
- struct CSV
- {
- File Comments;
- CSV()
- {
- Comments = SPIFFS.open("comments.csv","a+");
- Comments.close();
- }
- void addNewComment(Message msg)
- {
- Comments = SPIFFS.open("comments.csv","a+");
- Comments.print(msg.comment);
- Comments.print(",");
- Comments.print(msg.author);
- Comments.print(",");
- Comments.close();
- }
- void deleteComment(Message msg)
- {
- Comments = SPIFFS.open("comments.csv","r+");
- File temp_comments = SPIFFS.open("temp_comments.csv","r+");
- Message temp_comment = {"",""};
- while(!Comments)
- {
- Comments.readBytes(temp_comment.comment,140);
- Comments.seek(1,SeekCur);
- Comments.readBytes(temp_comment.author,20);
- Comments.seek(1,SeekCur);
- if(strncmp(temp_comment.comment,msg.comment,140) == 0 && strncmp(temp_comment.author,msg.author,20) == 0)
- {
- continue;
- }
- temp_comments.print(temp_comment.comment);
- temp_comments.print(",");
- temp_comments.print(temp_comment.author);
- }
- Comments.close();
- temp_comments.close();
- SPIFFS.remove("comments.csv");
- SPIFFS.rename("temp_comments.csv","comments.csv");
- }
- const char* allCommentsJSON()
- {
- Comments = SPIFFS.open("comments.csv","r+");
- char* json = "{";
- int i = 0;
- Message temp_comment;
- while(!Comments)
- {
- Comments.readBytes(temp_comment.comment,140);
- Comments.seek(1,SeekCur);
- Comments.readBytes(temp_comment.author,20);
- Comments.seek(1,SeekCur);
- char* commentNo = "";
- itoa(i,commentNo,10);
- strcpy(json,commentNo);
- strcpy(json," : ");
- strcpy(json," { ");
- strcpy(json," comment : ");
- strcpy(json,temp_comment.comment);
- strcpy(json,",");
- strcpy(json," author : ");
- strcpy(json,temp_comment.author);
- strcpy(json," } ,");
- i++;
- }
- return (const char*)json;
- }
- }Comments;
- //WIFI CONFIG
- const char* ssid = "Charge-Fi";
- const char* password = "19951999";
- //UPDATE CONFIG
- const char* update_path = "/firmware";
- const char* update_username = "charge";
- const char* update_password = "charge";
- //AP CONFIG
- const char* AP_ssid = "CONNECT AND GOTO comment.com";
- const char* AP_password = "";
- IPAddress apIP(192, 168, 4, 1);
- IPAddress netMask(255, 255, 255, 0);
- //DNS CONFIG
- const char* host = "comment.com";
- //Init Server
- ESP8266WebServer server(80);
- ESP8266HTTPUpdateServer httpUpdater;
- //SERVER METHODS
- void handleAddNewComment()
- {
- if(server.hasArg("comment") && server.hasArg("author"))
- {
- Message new_comment;
- strcpy(new_comment.comment,server.arg("comment").c_str());
- strcpy(new_comment.author,server.arg("author").c_str());
- Comments.addNewComment(new_comment);
- server.send(200,"text/plain","DONE");
- }
- server.send(200,"text/plain","ARGUMENTS NOT RECIEVED");
- }
- void handleDeleteComment()
- {
- if(server.hasArg("comment") && server.hasArg("author"))
- {
- Message delete_comment;
- strcpy(delete_comment.comment,server.arg("comment").c_str());
- strcpy(delete_comment.author,server.arg("author").c_str());
- Comments.deleteComment(delete_comment);
- server.send(200,"text/plain","DONE");
- }
- server.send(200,"text/plain","ARGUMENTS NOT RECIEVED");
- }
- void handleAllComments()
- {
- String JSONComments = Comments.allCommentsJSON();
- server.send(200,"text/json",JSONComments);
- }
- void handleCSVComments()
- {
- File Comments = SPIFFS.open("comments.csv","r+");
- server.streamFile(Comments,"text/plain");
- }
- 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(".css")) return "text/css";
- else if(filename.endsWith(".js")) return "application/javascript";
- else if(filename.endsWith(".png")) return "image/png";
- else if(filename.endsWith(".gif")) return "image/gif";
- else if(filename.endsWith(".jpg")) return "image/jpeg";
- else if(filename.endsWith(".ico")) return "image/x-icon";
- else if(filename.endsWith(".xml")) return "text/xml";
- else if(filename.endsWith(".pdf")) return "application/x-pdf";
- else if(filename.endsWith(".zip")) return "application/x-zip";
- else if(filename.endsWith(".gz")) return "application/x-gzip";
- return "text/plain";
- }
- 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 handleIndex()
- {
- if(!handleFileRead("/index.html")) server.send(404, "text/plain", "Cannot display homepage! contact : ahujaarush@gmail.com");
- }
- void handleFileAccess()
- {
- if(server.hasArg("file"))
- {
- String text = "/";
- text += server.arg("file");
- if(!handleFileRead(text)) server.send(404, "text/plain", "File Not Found! contact : ahujaarush@gmail.com");
- }
- else server.send(404, "text/plain", "No argument provided contact : ahujaarush@gmail.com");
- }
- void handleNotFound()
- {
- server.send(404, "text/plain", "Cannot display homepage! contact : ahujaarush@gmail.com ");
- }
- void setup() {
- // put your setup code here, to run once:
- WiFi.begin(ssid, password);
- SPIFFS.begin();
- WiFi.softAPConfig(apIP, apIP, netMask);
- WiFi.softAP(AP_ssid, AP_password);
- MDNS.begin(host);
- MDNS.addService("http","tcp",80);
- pinMode(0,INPUT);
- digitalWrite(0,HIGH);
- server.on("/",HTTP_GET,handleIndex);
- server.on("/new",HTTP_POST,handleAddNewComment);
- server.on("/delete",HTTP_POST,handleDeleteComment);
- server.on("/allComments",HTTP_GET,handleAllComments);
- server.on("/access",HTTP_GET,handleFileAccess);
- server.onNotFound(handleNotFound);
- server.begin();
- httpUpdater.setup(&server, update_path, update_username, update_password);
- }
- void loop() {
- server.handleClient();
- }
Add Comment
Please, Sign In to add comment