Guest User

Untitled

a guest
Apr 25th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5. #include <ESP8266HTTPUpdateServer.h>
  6. #include<string.h>
  7. #include <FS.h>
  8. #include <DNSServer.h>
  9.  
  10. //STRUCTURES
  11.  
  12. struct Message
  13. {
  14. char comment[140];
  15. char author[20];
  16. };
  17.  
  18. struct CSV
  19. {
  20. File Comments;
  21.  
  22. CSV()
  23. {
  24. Comments = SPIFFS.open("comments.csv","a+");
  25. Comments.close();
  26. }
  27.  
  28. void addNewComment(Message msg)
  29. {
  30. Comments = SPIFFS.open("comments.csv","a+");
  31. Comments.print(msg.comment);
  32. Comments.print(",");
  33. Comments.print(msg.author);
  34. Comments.print(",");
  35. Comments.close();
  36. }
  37.  
  38. void deleteComment(Message msg)
  39. {
  40. Comments = SPIFFS.open("comments.csv","r+");
  41. File temp_comments = SPIFFS.open("temp_comments.csv","r+");
  42. Message temp_comment = {"",""};
  43. while(!Comments)
  44. {
  45. Comments.readBytes(temp_comment.comment,140);
  46. Comments.seek(1,SeekCur);
  47. Comments.readBytes(temp_comment.author,20);
  48. Comments.seek(1,SeekCur);
  49.  
  50. if(strncmp(temp_comment.comment,msg.comment,140) == 0 && strncmp(temp_comment.author,msg.author,20) == 0)
  51. {
  52. continue;
  53. }
  54. temp_comments.print(temp_comment.comment);
  55. temp_comments.print(",");
  56. temp_comments.print(temp_comment.author);
  57. }
  58.  
  59. Comments.close();
  60. temp_comments.close();
  61. SPIFFS.remove("comments.csv");
  62. SPIFFS.rename("temp_comments.csv","comments.csv");
  63.  
  64. }
  65.  
  66. const char* allCommentsJSON()
  67. {
  68.  
  69. Comments = SPIFFS.open("comments.csv","r+");
  70. char* json = "{";
  71. int i = 0;
  72. Message temp_comment;
  73. while(!Comments)
  74. {
  75. Comments.readBytes(temp_comment.comment,140);
  76. Comments.seek(1,SeekCur);
  77. Comments.readBytes(temp_comment.author,20);
  78. Comments.seek(1,SeekCur);
  79.  
  80. char* commentNo = "";
  81. itoa(i,commentNo,10);
  82. strcpy(json,commentNo);
  83. strcpy(json," : ");
  84. strcpy(json," { ");
  85. strcpy(json," comment : ");
  86. strcpy(json,temp_comment.comment);
  87. strcpy(json,",");
  88. strcpy(json," author : ");
  89. strcpy(json,temp_comment.author);
  90. strcpy(json," } ,");
  91.  
  92. i++;
  93. }
  94.  
  95. return (const char*)json;
  96.  
  97. }
  98. }Comments;
  99.  
  100. //WIFI CONFIG
  101. const char* ssid = "Charge-Fi";
  102. const char* password = "19951999";
  103.  
  104. //UPDATE CONFIG
  105. const char* update_path = "/firmware";
  106. const char* update_username = "charge";
  107. const char* update_password = "charge";
  108.  
  109. //AP CONFIG
  110. const char* AP_ssid = "CONNECT AND GOTO comment.com";
  111. const char* AP_password = "";
  112. IPAddress apIP(192, 168, 4, 1);
  113. IPAddress netMask(255, 255, 255, 0);
  114.  
  115. //DNS CONFIG
  116. const char* host = "comment.com";
  117.  
  118. //Init Server
  119.  
  120. ESP8266WebServer server(80);
  121. ESP8266HTTPUpdateServer httpUpdater;
  122.  
  123. //SERVER METHODS
  124. void handleAddNewComment()
  125. {
  126. if(server.hasArg("comment") && server.hasArg("author"))
  127. {
  128. Message new_comment;
  129.  
  130. strcpy(new_comment.comment,server.arg("comment").c_str());
  131. strcpy(new_comment.author,server.arg("author").c_str());
  132.  
  133. Comments.addNewComment(new_comment);
  134.  
  135. server.send(200,"text/plain","DONE");
  136. }
  137. server.send(200,"text/plain","ARGUMENTS NOT RECIEVED");
  138. }
  139.  
  140. void handleDeleteComment()
  141. {
  142. if(server.hasArg("comment") && server.hasArg("author"))
  143. {
  144. Message delete_comment;
  145.  
  146. strcpy(delete_comment.comment,server.arg("comment").c_str());
  147. strcpy(delete_comment.author,server.arg("author").c_str());
  148.  
  149. Comments.deleteComment(delete_comment);
  150.  
  151. server.send(200,"text/plain","DONE");
  152. }
  153. server.send(200,"text/plain","ARGUMENTS NOT RECIEVED");
  154.  
  155. }
  156.  
  157. void handleAllComments()
  158. {
  159. String JSONComments = Comments.allCommentsJSON();
  160. server.send(200,"text/json",JSONComments);
  161.  
  162. }
  163.  
  164. void handleCSVComments()
  165. {
  166. File Comments = SPIFFS.open("comments.csv","r+");
  167. server.streamFile(Comments,"text/plain");
  168. }
  169.  
  170. String getContentType(String filename){
  171. if(server.hasArg("download")) return "application/octet-stream";
  172. else if(filename.endsWith(".htm")) return "text/html";
  173. else if(filename.endsWith(".html")) return "text/html";
  174. else if(filename.endsWith(".css")) return "text/css";
  175. else if(filename.endsWith(".js")) return "application/javascript";
  176. else if(filename.endsWith(".png")) return "image/png";
  177. else if(filename.endsWith(".gif")) return "image/gif";
  178. else if(filename.endsWith(".jpg")) return "image/jpeg";
  179. else if(filename.endsWith(".ico")) return "image/x-icon";
  180. else if(filename.endsWith(".xml")) return "text/xml";
  181. else if(filename.endsWith(".pdf")) return "application/x-pdf";
  182. else if(filename.endsWith(".zip")) return "application/x-zip";
  183. else if(filename.endsWith(".gz")) return "application/x-gzip";
  184. return "text/plain";
  185. }
  186.  
  187. bool handleFileRead(String path){
  188. if(path.endsWith("/")) path += "index.htm";
  189.  
  190. String contentType = getContentType(path);
  191. String pathWithGz = path + ".gz";
  192. if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
  193. if(SPIFFS.exists(pathWithGz))
  194. path += ".gz";
  195. File file = SPIFFS.open(path, "r");
  196. size_t sent = server.streamFile(file, contentType);
  197. file.close();
  198. return true;
  199. }
  200. return false;
  201. }
  202.  
  203. void handleIndex()
  204. {
  205. if(!handleFileRead("/index.html")) server.send(404, "text/plain", "Cannot display homepage! contact : ahujaarush@gmail.com");
  206. }
  207.  
  208. void handleFileAccess()
  209. {
  210. if(server.hasArg("file"))
  211. {
  212. String text = "/";
  213. text += server.arg("file");
  214. if(!handleFileRead(text)) server.send(404, "text/plain", "File Not Found! contact : ahujaarush@gmail.com");
  215. }
  216. else server.send(404, "text/plain", "No argument provided contact : ahujaarush@gmail.com");
  217. }
  218.  
  219. void handleNotFound()
  220. {
  221. server.send(404, "text/plain", "Cannot display homepage! contact : ahujaarush@gmail.com ");
  222. }
  223.  
  224. void setup() {
  225. // put your setup code here, to run once:
  226. WiFi.begin(ssid, password);
  227. SPIFFS.begin();
  228. WiFi.softAPConfig(apIP, apIP, netMask);
  229. WiFi.softAP(AP_ssid, AP_password);
  230. MDNS.begin(host);
  231. MDNS.addService("http","tcp",80);
  232. pinMode(0,INPUT);
  233. digitalWrite(0,HIGH);
  234. server.on("/",HTTP_GET,handleIndex);
  235. server.on("/new",HTTP_POST,handleAddNewComment);
  236. server.on("/delete",HTTP_POST,handleDeleteComment);
  237. server.on("/allComments",HTTP_GET,handleAllComments);
  238. server.on("/access",HTTP_GET,handleFileAccess);
  239. server.onNotFound(handleNotFound);
  240. server.begin();
  241.  
  242. httpUpdater.setup(&server, update_path, update_username, update_password);
  243.  
  244. }
  245.  
  246. void loop() {
  247. server.handleClient();
  248. }
Add Comment
Please, Sign In to add comment