Components and supplies
1
ESP8266 ESP-01
1
ESP8266 ESP-01 USB Seriell Adapter Programmer
Apps and platforms
1
esp8266 File Upload Tool
1
Arduino support for ESP8266
1
Arduino IDE
Project description
Code
Guestbook
arduino
A simple web server
1#include <ESP8266WebServer.h> 2#include <FS.h> 3#include <DNSServer.h> 4 5extern "C" { 6#include<user_interface.h> 7} 8 9#define LOGBOOK "/logbook.txt" 10 11//#define DEV //devoleper mode. disable it, for faster web sites 12 13const byte DNS_PORT = 53; 14const char* user = "admin"; 15const char* pass = "esp8266"; 16const char* dns = "guestbook.com"; 17const char* ssid = "Guestbook"; 18const char* password = ""; 19uint8_t ip[] = {192,168,0,1}; 20 21int x, xx, counter; 22 23unsigned long currentMillis; 24unsigned long previousMillis = 0; 25const long interval = 600000; //delay time for delete 26 27String HTML, fileName, fileEdit, admin; 28String Year, Month, Day, rows; 29String Vname, Nname, Comment, Counter, head, nav, footer, macAddr; 30 31String formatBytes(size_t bytes) { 32 if (bytes < 1024) { 33 return String(bytes) + "B"; 34 } else if (bytes < (1024 * 1024)) { 35 return String(bytes / 1024.0) + "KB"; 36 } else if (bytes < (1024 * 1024 * 1024)) { 37 return String(bytes / 1024.0 / 1024.0) + "MB"; 38 } else { 39 return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB"; 40 } 41} 42 43bool test; 44 45File page; 46DNSServer dnsServer; 47// Create an instance of the server on Port 80 48ESP8266WebServer server(80); 49 50String getContentType(String filename); 51void notFound();// Sendet "Not Found"-Seite 52void handleUnknown(); // Liefert Web-Seiten aus dem SPIFFS 53void handleFileUpload();// upload a new file to the SPIFFS 54void adminHtml(); 55void logHtml(); 56void indexHtml(); 57void aboutHtml(); 58void getMacAddr(); 59 60void setup() 61{ //Serial.begin(9600); 62 SPIFFS.begin(); 63 //delay(100); 64 //loadConfig(); 65 IPAddress apIP(ip[0],ip[1],ip[2],ip[3]); // set IP-address 66 WiFi.mode(WIFI_AP_STA); 67 WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // subnet FF FF FF 00 68 WiFi.softAP(ssid,password, 1, 0, 1); // ssid,pw,channel,hidden,max conections 69 70 dnsServer.setTTL(300); 71 dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure); 72 dnsServer.start(DNS_PORT, dns, apIP); 73 74 server.on("/admin", HTTP_POST, // if the client posts to the upload page 75 []() { 76 server.send(200); 77 }, // Send status 200 (OK) to tell the client we are ready to receive 78 handleFileUpload // Receive and save the file 79 ); 80 server.on("/admin", []() { 81 if (!server.authenticate(user, pass)) 82 return server.requestAuthentication(); 83 adminHtml(); 84 server.send(303); 85 }); 86 server.on("/log.html", logHtml); 87 server.on("/", indexHtml); 88 server.on("/index.html", indexHtml); 89 server.on("/about.html", aboutHtml); 90 server.on("/site.html", siteHtml); 91 server.onNotFound(handleUnknown); 92 server.begin(); 93 94#if not defined (DEV) 95 loadString(); 96#endif 97} 98 99void loop() 100{ 101 // Check if a client has connected 102 server.handleClient(); 103 dnsServer.processNextRequest(); 104 currentMillis = millis(); 105 if (currentMillis - previousMillis >= interval) { 106 previousMillis = currentMillis; 107 108 SPIFFS.remove("/comments.txt"); 109 } 110} 111// Sendet "Not Found"-Seite 112void notFound() 113{ HTML = F("<html><head><title>404 Not Found</title></head><body>" 114 "<body><h1>Page not found</h1>" 115 "<br><p>404-error</p>" 116 //"<body background='/404-error.jpg'>" 117 "</body></html>"); 118 server.send(404, "text/html", HTML); 119} 120 121// Es wird versucht, die angegebene Datei aus dem SPIFFS hochzuladen 122void handleUnknown() 123{ fileName = server.uri(); 124 page = SPIFFS.open(fileName, "r"); 125 if (page) 126 { String contentTyp = getContentType(fileName); 127 size_t sent = server.streamFile(page, contentTyp); 128 page.close(); 129 } 130 else { 131 notFound(); 132 } 133} 134void logHtml() 135{ 136 #if defined (DEV) 137 loadString(); 138 #endif 139 page = SPIFFS.open (LOGBOOK, "r" ) ; 140 HTML = head; 141 HTML += nav; 142 HTML += "<br><br><br><center><h1>Guestbook Log</h1>"; 143 HTML += "<p>Here you can see all people that have found the accesspoint.</p>"; 144 HTML += "<br><br>"; 145 HTML += "<table><th>Date</th><th>Name</th><th>Comment</th>"; 146 test = true; 147 do { 148 admin = page.readStringUntil('\ 149'); 150 if (admin.length() > 0) { 151 152 Year = admin.substring(0, 4); 153 Month = admin.substring(5, 7); 154 Day = admin.substring(8, 10); 155 for (x = 12; x < admin.length(); x++) 156 { 157 if (admin.charAt(x) == '|') { 158 Vname = admin.substring(11, x); 159 break; 160 } 161 } 162 for (xx = x + 1; xx < admin.length(); xx++) { 163 if (admin.charAt(xx) == '|') { 164 Nname = admin.substring(x + 1, xx); 165 break; 166 } 167 } 168 Comment = admin.substring(xx + 1, admin.length() - 1); 169 HTML += "<tr><td>" + Month + ", " + Day + ", " + Year + " </td>"; 170 HTML += "<td>" + Vname + " " + Nname + " </td>"; 171 rows = "1"; 172 if (Comment.length() > 80) rows = "2"; 173 if (Comment.length() > 160) rows = "3"; 174 if (Comment.length() > 240) rows = "4"; 175 HTML += "<td><textarea rows='" + rows + "' cols='80'>" + Comment + "</textarea></td></tr>"; 176 } else { 177 test = false; 178 page.close(); 179 } 180 } while (test); 181 Counter = counter; 182 HTML += "</table></center><br><br>"; 183 HTML += "<br><p>" + Counter + " Vistors</p>"; 184 //HTML+="<p>"+server.client().remoteIP().toString()+"</p>"; 185 //getMacAddr(); 186 //HTML+="<p>"+macAddr+"</p>"; 187 HTML += footer; 188 server.send(200, "text/html", HTML); 189} 190void adminHtml() { 191 #if defined (DEV) 192 loadString(); 193 #endif 194 if (server.hasArg("format")) { 195 SPIFFS.format(); 196 } 197 if (server.hasArg("message")) { 198 admin = server.arg(0).c_str(); 199 admin.replace("%0D%0A++", "\ 200"); 201 page = SPIFFS.open (fileEdit, "w" ); 202 page.print(admin); 203 page.close(); 204 } 205 if (server.hasArg("delete")) { 206 fileName = server.arg(0).c_str(); 207 fileName = "/" + fileName; 208 SPIFFS.remove(fileName); 209 } 210 if (server.hasArg("reboot")) { 211 ESP.restart(); 212 } 213 if (server.hasArg("show")) { 214 fileEdit = server.arg(0).c_str(); 215 fileEdit = "/" + fileEdit; 216 page = SPIFFS.open (fileEdit, "r" ); 217 HTML = head; 218 HTML += nav; 219 HTML += "<center><h1>Edit " + fileEdit.substring(1) + "</h1>"; 220 HTML += "<pre><form action='/admin'><br><textarea rows='20' cols='80' name='message'>"; 221 admin = page.readString(); 222 admin.replace("<", "<"); 223 admin.replace(">", ">"); 224 HTML += admin + "</textarea></pre>"; 225 page.close(); 226 HTML += "<br><input type='submit' value='save'></form><br><form action='/admin'><br><input type='submit' value='back'></form><br></center>"; 227 server.send(200, "text/html", HTML); 228 } 229 HTML = head; 230 HTML += nav; 231 HTML += "<center><br><br><br><form method='post' enctype='multipart/form-data'><input type='file' name='name' required><input class='button' type='submit' value='Upload'></form><br>"; 232 HTML += "<table>"; 233 Dir dir = SPIFFS.openDir("/"); 234 while (dir.next()) { 235 HTML += "<tr><td>" + dir.fileName().substring(1) + "</td> "; 236 //HTML+="<td><form action='/admin'><input type='submit' name='show' value='"+dir.fileName().substring(1)+"'></form>"; 237 HTML += "<td><button><a href=admin?show=" + dir.fileName().substring(1) + ">show/edit</a></button></td>"; 238 HTML += "<td><button><a href='" + dir.fileName() + "' download='" + dir.fileName().substring(1) + "'>download</a></button></td> "; 239 HTML += "<td><button><a href=admin?delete=" + dir.fileName().substring(1) + ">delete</a></button></td>"; 240 HTML += "<td style='text-align:right'>" + formatBytes(dir.fileSize()) + "</td></tr> "; 241 } 242 HTML += "</table><br>"; 243 FSInfo fsInfo; 244 SPIFFS.info(fsInfo); 245 HTML += "FS Bytes: "; 246 HTML += fsInfo.usedBytes; 247 HTML += " / "; 248 HTML += fsInfo.totalBytes; 249 HTML += "<br><br><button><a href=admin?reboot=''>restart</a></button><br>"; 250 HTML += "<br><button><a href=admin?format=''>format</a></button>"; 251 HTML += "</center>" + footer; 252 server.send(200, "text/html", HTML); 253} 254void handleFileUpload() { // upload a new file to the SPIFFS 255 HTTPUpload& upload = server.upload(); 256 if (upload.status == UPLOAD_FILE_START) { 257 String filename = upload.filename; 258 if (!filename.startsWith("/")) filename = "/" + filename; 259 page = SPIFFS.open(filename, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist) 260 filename = String(); 261 } else if (upload.status == UPLOAD_FILE_WRITE) { 262 if (page) 263 page.write(upload.buf, upload.currentSize); // Write the received bytes to the file 264 } else if (upload.status == UPLOAD_FILE_END) { 265 if (page) { // If the file was successfully created 266 page.close(); // Close the file again 267 server.sendHeader("Location", "/admin"); // Redirect the client to the success page 268 server.send(303); 269 } else { 270 server.send(500, "text/plain", "500: couldn't create file"); 271 } 272 } 273} 274void indexHtml() { 275#if defined (DEV) 276 loadString(); 277 #endif 278 getMacAddr(); 279 page = SPIFFS.open("/visitors.txt", "r"); 280 test = true; 281 do { 282 admin = page.readStringUntil('|'); 283 if (admin.length() > 0) { 284 if (admin == macAddr) { 285 page.close(); 286 test = false; 287 } 288 } else { 289 test = false; 290 page = SPIFFS.open("/visitors.txt", "a+"); 291 page.print(macAddr + "|"); 292 page.close(); 293 counter++; 294 } 295 } while (test); 296 page = SPIFFS.open("/count.txt", "w"); 297 page.print(counter); 298 HTML = head; 299 HTML += nav; 300 page = SPIFFS.open("/index", "r"); 301 HTML += page.readString(); 302 page.close(); 303 page = SPIFFS.open ("/count.txt", "r" ); 304 Counter = page.readString(); 305 page.close(); 306 HTML += "<br><p>" + Counter + " Vistors</p>"; 307 HTML += footer; 308 server.send(200, "text/html", HTML); 309} 310void aboutHtml() { 311 #if defined (DEV) 312 loadString(); 313 #endif 314 HTML = head; 315 HTML += nav; 316 page = SPIFFS.open("/about", "r"); 317 HTML += page.readString(); 318 page.close(); 319 Counter = counter; 320 HTML += "<br><p>" + Counter + " Vistors</p>"; 321 HTML += footer; 322 server.send(200, "text/html", HTML); 323} 324void siteHtml() { 325 #if defined (DEV) 326 loadString(); 327 #endif 328 getMacAddr(); 329 page = SPIFFS.open("/comments.txt", "r"); 330 test = true; 331 do { 332 admin = page.readStringUntil('|'); 333 //Serial.println(admin); 334 if (admin.length() > 0) { 335 if (admin == macAddr) { 336 page.close(); 337 errorRegister(); 338 return; 339 } 340 } else { 341 test = false; 342 page = SPIFFS.open("/comments.txt", "a+"); 343 page.print(macAddr + "|"); 344 page.close(); 345 previousMillis = millis(); 346 } 347 } while (test); 348 page = SPIFFS.open (LOGBOOK, "a+" ); 349 page.print(server.arg(0).c_str()); 350 page.print("|"); 351 String Comm = (server.arg(1).c_str()); 352 Comm.replace("\ 353", " "); 354 Comm.replace("\ ", " "); 355 page.print(Comm); 356 page.print("|"); 357 Comm = (server.arg(2).c_str()); 358 Comm.replace("\ 359", " "); 360 Comm.replace("\ ", " "); 361 page.print(Comm); 362 page.print("|"); 363 Comm = server.arg(3).c_str(); 364 Comm.replace("\ 365", " "); 366 Comm.replace("\ ", " "); 367 page.println(Comm); 368 page.close(); 369 HTML = head; 370 HTML += nav; 371 page = SPIFFS.open("/site", "r"); 372 HTML += page.readString(); 373 page.close(); 374 Counter = counter; 375 HTML += "<br><p>" + Counter + " Vistors</p>"; 376 HTML += footer; 377 server.send(200, "text/html", HTML); 378} 379void errorRegister() { 380 #if defined (DEV) 381 loadString(); 382 #endif 383 HTML = head; 384 HTML += nav; 385 page = SPIFFS.open("/error", "r"); 386 HTML += page.readString(); 387 page.close(); 388 Counter = counter; 389 HTML += "<br><p>" + Counter + " Vistors</p>"; 390 HTML += footer; 391 server.send(200, "text/html", HTML); 392} 393 394void getMacAddr() { 395 396 struct station_info *stat_info; 397 398 stat_info = wifi_softap_get_station_info(); 399 400 macAddr = String(stat_info->bssid[0], HEX); 401 macAddr += String(stat_info->bssid[1], HEX); 402 macAddr += String(stat_info->bssid[2], HEX); 403 macAddr += String(stat_info->bssid[3], HEX); 404 macAddr += String(stat_info->bssid[4], HEX); 405 macAddr += String(stat_info->bssid[5], HEX); 406} 407void loadString() { 408 page = SPIFFS.open("/count.txt", "r"); 409 Counter = page.readString(); 410 page.close(); 411 counter = Counter.toInt(); 412 page = SPIFFS.open("/head", "r"); 413 head = page.readString(); 414 page.close(); 415 page = SPIFFS.open("/nav", "r"); 416 nav = page.readString(); 417 page.close(); 418 page = SPIFFS.open("/footer", "r"); 419 footer = page.readString(); 420 page.close(); 421} 422String getContentType(String filename) { // convert the file extension to the MIME type 423 if (filename.endsWith(".html")) return "text/html"; 424 else if (filename.endsWith(".css")) return "text/css"; 425 else if (filename.endsWith(".js")) return "application/javascript"; 426 else if (filename.endsWith(".ico")) return "image/x-icon"; 427 return "text/plain"; 428} 429
Guestbook
arduino
A simple web server
1#include <ESP8266WebServer.h> 2#include <FS.h> 3#include <DNSServer.h> 4 5extern 6 "C" { 7#include<user_interface.h> 8} 9 10#define LOGBOOK "/logbook.txt" 11 12//#define 13 DEV //devoleper mode. disable it, for faster web sites 14 15const byte DNS_PORT 16 = 53; 17const char* user = "admin"; 18const char* pass = "esp8266"; 19const 20 char* dns = "guestbook.com"; 21const char* ssid = "Guestbook"; 22const char* 23 password = ""; 24uint8_t ip[] = {192,168,0,1}; 25 26int x, xx, counter; 27 28unsigned 29 long currentMillis; 30unsigned long previousMillis = 0; 31const long interval 32 = 600000; //delay time for delete 33 34String HTML, fileName, fileEdit, admin; 35String 36 Year, Month, Day, rows; 37String Vname, Nname, Comment, Counter, head, nav, footer, 38 macAddr; 39 40String formatBytes(size_t bytes) { 41 if (bytes < 1024) { 42 43 return String(bytes) + "B"; 44 } else if (bytes < (1024 * 1024)) { 45 return 46 String(bytes / 1024.0) + "KB"; 47 } else if (bytes < (1024 * 1024 * 1024)) { 48 49 return String(bytes / 1024.0 / 1024.0) + "MB"; 50 } else { 51 return 52 String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB"; 53 } 54} 55 56bool test; 57 58File 59 page; 60DNSServer dnsServer; 61// Create an instance of the server on Port 80 62ESP8266WebServer 63 server(80); 64 65String getContentType(String filename); 66void notFound();// 67 Sendet "Not Found"-Seite 68void handleUnknown(); // Liefert Web-Seiten aus 69 dem SPIFFS 70void handleFileUpload();// upload a new file to the SPIFFS 71void 72 adminHtml(); 73void logHtml(); 74void indexHtml(); 75void aboutHtml(); 76void 77 getMacAddr(); 78 79void setup() 80{ //Serial.begin(9600); 81 SPIFFS.begin(); 82 83 //delay(100); 84 //loadConfig(); 85 IPAddress apIP(ip[0],ip[1],ip[2],ip[3]); 86 // set IP-address 87 WiFi.mode(WIFI_AP_STA); 88 WiFi.softAPConfig(apIP, apIP, 89 IPAddress(255, 255, 255, 0)); // subnet FF FF FF 00 90 WiFi.softAP(ssid,password, 91 1, 0, 1); // ssid,pw,channel,hidden,max conections 92 93 94 dnsServer.setTTL(300); 95 dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure); 96 97 dnsServer.start(DNS_PORT, dns, apIP); 98 99 server.on("/admin", HTTP_POST, 100 // if the client posts to the upload page 101 []() 102 { 103 server.send(200); 104 }, // Send status 105 200 (OK) to tell the client we are ready to receive 106 handleFileUpload // 107 Receive and save the file 108 ); 109 server.on("/admin", []() { 110 111 if (!server.authenticate(user, pass)) 112 return server.requestAuthentication(); 113 114 adminHtml(); 115 server.send(303); 116 }); 117 server.on("/log.html", 118 logHtml); 119 server.on("/", indexHtml); 120 server.on("/index.html", indexHtml); 121 122 server.on("/about.html", aboutHtml); 123 server.on("/site.html", siteHtml); 124 125 server.onNotFound(handleUnknown); 126 server.begin(); 127 128#if not defined 129 (DEV) 130 loadString(); 131#endif 132} 133 134void loop() 135{ 136 // Check if 137 a client has connected 138 server.handleClient(); 139 dnsServer.processNextRequest(); 140 141 currentMillis = millis(); 142 if (currentMillis - previousMillis >= interval) 143 { 144 previousMillis = currentMillis; 145 146 SPIFFS.remove("/comments.txt"); 147 148 } 149} 150// Sendet "Not Found"-Seite 151void notFound() 152{ HTML = F("<html><head><title>404 153 Not Found</title></head><body>" 154 "<body><h1>Page not found</h1>" 155 156 "<br><p>404-error</p>" 157 //"<body background='/404-error.jpg'>" 158 159 "</body></html>"); 160 server.send(404, "text/html", HTML); 161} 162 163// 164 Es wird versucht, die angegebene Datei aus dem SPIFFS hochzuladen 165void handleUnknown() 166{ 167 fileName = server.uri(); 168 page = SPIFFS.open(fileName, "r"); 169 if (page) 170 171 { String contentTyp = getContentType(fileName); 172 size_t sent = server.streamFile(page, 173 contentTyp); 174 page.close(); 175 } 176 else { 177 notFound(); 178 } 179} 180void 181 logHtml() 182{ 183 #if defined (DEV) 184 loadString(); 185 #endif 186 page = 187 SPIFFS.open (LOGBOOK, "r" ) ; 188 HTML = head; 189 HTML += nav; 190 HTML += 191 "<br><br><br><center><h1>Guestbook Log</h1>"; 192 HTML += "<p>Here you can see 193 all people that have found the accesspoint.</p>"; 194 HTML += "<br><br>"; 195 196 HTML += "<table><th>Date</th><th>Name</th><th>Comment</th>"; 197 test = true; 198 199 do { 200 admin = page.readStringUntil('\ 201'); 202 if (admin.length() > 0) 203 { 204 205 Year = admin.substring(0, 4); 206 Month = admin.substring(5, 207 7); 208 Day = admin.substring(8, 10); 209 for (x = 12; x < admin.length(); 210 x++) 211 { 212 if (admin.charAt(x) == '|') { 213 Vname = admin.substring(11, 214 x); 215 break; 216 } 217 } 218 for (xx = x + 1; xx < admin.length(); 219 xx++) { 220 if (admin.charAt(xx) == '|') { 221 Nname = admin.substring(x 222 + 1, xx); 223 break; 224 } 225 } 226 Comment = admin.substring(xx 227 + 1, admin.length() - 1); 228 HTML += "<tr><td>" + Month + ", " + Day + 229 ", " + Year + " </td>"; 230 HTML += "<td>" + Vname + 231 " " + Nname + " </td>"; 232 rows = "1"; 233 234 if (Comment.length() > 80) rows = "2"; 235 if (Comment.length() > 160) 236 rows = "3"; 237 if (Comment.length() > 240) rows = "4"; 238 HTML += 239 "<td><textarea rows='" + rows + "' cols='80'>" + Comment + "</textarea></td></tr>"; 240 241 } else { 242 test = false; 243 page.close(); 244 } 245 } while 246 (test); 247 Counter = counter; 248 HTML += "</table></center><br><br>"; 249 250 HTML += "<br><p>" + Counter + " Vistors</p>"; 251 //HTML+="<p>"+server.client().remoteIP().toString()+"</p>"; 252 253 //getMacAddr(); 254 //HTML+="<p>"+macAddr+"</p>"; 255 HTML += footer; 256 257 server.send(200, "text/html", HTML); 258} 259void adminHtml() { 260 #if defined 261 (DEV) 262 loadString(); 263 #endif 264 if (server.hasArg("format")) { 265 SPIFFS.format(); 266 267 } 268 if (server.hasArg("message")) { 269 admin = server.arg(0).c_str(); 270 271 admin.replace("%0D%0A++", "\ 272"); 273 page = SPIFFS.open (fileEdit, "w" 274 ); 275 page.print(admin); 276 page.close(); 277 } 278 if (server.hasArg("delete")) 279 { 280 fileName = server.arg(0).c_str(); 281 fileName = "/" + fileName; 282 283 SPIFFS.remove(fileName); 284 } 285 if (server.hasArg("reboot")) { 286 ESP.restart(); 287 288 } 289 if (server.hasArg("show")) { 290 fileEdit = server.arg(0).c_str(); 291 292 fileEdit = "/" + fileEdit; 293 page = SPIFFS.open (fileEdit, "r" ); 294 295 HTML = head; 296 HTML += nav; 297 HTML += "<center><h1>Edit " + 298 fileEdit.substring(1) + "</h1>"; 299 HTML += "<pre><form action='/admin'><br><textarea 300 rows='20' cols='80' name='message'>"; 301 admin = page.readString(); 302 admin.replace("<", 303 "<"); 304 admin.replace(">", ">"); 305 HTML += admin + "</textarea></pre>"; 306 307 page.close(); 308 HTML += "<br><input type='submit' value='save'></form><br><form 309 action='/admin'><br><input type='submit' value='back'></form><br></center>"; 310 311 server.send(200, "text/html", HTML); 312 } 313 HTML = head; 314 HTML += 315 nav; 316 HTML += "<center><br><br><br><form method='post' enctype='multipart/form-data'><input 317 type='file' name='name' required><input class='button' type='submit' value='Upload'></form><br>"; 318 319 HTML += "<table>"; 320 Dir dir = SPIFFS.openDir("/"); 321 while (dir.next()) 322 { 323 HTML += "<tr><td>" + dir.fileName().substring(1) + "</td> "; 324 325 //HTML+="<td><form action='/admin'><input type='submit' name='show' value='"+dir.fileName().substring(1)+"'></form>"; 326 327 HTML += "<td><button><a href=admin?show=" + dir.fileName().substring(1) + 328 ">show/edit</a></button></td>"; 329 HTML += "<td><button><a href='" + dir.fileName() 330 + "' download='" + dir.fileName().substring(1) + "'>download</a></button></td> "; 331 332 HTML += "<td><button><a href=admin?delete=" + dir.fileName().substring(1) 333 + ">delete</a></button></td>"; 334 HTML += "<td style='text-align:right'>" 335 + formatBytes(dir.fileSize()) + "</td></tr> "; 336 } 337 HTML 338 += "</table><br>"; 339 FSInfo fsInfo; 340 SPIFFS.info(fsInfo); 341 HTML += 342 "FS Bytes: "; 343 HTML += fsInfo.usedBytes; 344 HTML += " / "; 345 346 HTML += fsInfo.totalBytes; 347 HTML += "<br><br><button><a href=admin?reboot=''>restart</a></button><br>"; 348 349 HTML += "<br><button><a href=admin?format=''>format</a></button>"; 350 HTML 351 += "</center>" + footer; 352 server.send(200, "text/html", HTML); 353} 354void 355 handleFileUpload() { // upload a new file to the SPIFFS 356 HTTPUpload& upload 357 = server.upload(); 358 if (upload.status == UPLOAD_FILE_START) { 359 String 360 filename = upload.filename; 361 if (!filename.startsWith("/")) filename = "/" 362 + filename; 363 page = SPIFFS.open(filename, "w"); // Open the file 364 for writing in SPIFFS (create if it doesn't exist) 365 filename = String(); 366 367 } else if (upload.status == UPLOAD_FILE_WRITE) { 368 if (page) 369 page.write(upload.buf, 370 upload.currentSize); // Write the received bytes to the file 371 } else if (upload.status 372 == UPLOAD_FILE_END) { 373 if (page) { // If 374 the file was successfully created 375 page.close(); // 376 Close the file again 377 server.sendHeader("Location", "/admin"); // 378 Redirect the client to the success page 379 server.send(303); 380 } else 381 { 382 server.send(500, "text/plain", "500: couldn't create file"); 383 384 } 385 } 386} 387void indexHtml() { 388#if defined (DEV) 389 loadString(); 390 391 #endif 392 getMacAddr(); 393 page = SPIFFS.open("/visitors.txt", "r"); 394 395 test = true; 396 do { 397 admin = page.readStringUntil('|'); 398 if (admin.length() 399 > 0) { 400 if (admin == macAddr) { 401 page.close(); 402 test 403 = false; 404 } 405 } else { 406 test = false; 407 page = SPIFFS.open("/visitors.txt", 408 "a+"); 409 page.print(macAddr + "|"); 410 page.close(); 411 counter++; 412 413 } 414 } while (test); 415 page = SPIFFS.open("/count.txt", "w"); 416 page.print(counter); 417 418 HTML = head; 419 HTML += nav; 420 page = SPIFFS.open("/index", "r"); 421 422 HTML += page.readString(); 423 page.close(); 424 page = SPIFFS.open ("/count.txt", 425 "r" ); 426 Counter = page.readString(); 427 page.close(); 428 HTML += "<br><p>" 429 + Counter + " Vistors</p>"; 430 HTML += footer; 431 server.send(200, "text/html", 432 HTML); 433} 434void aboutHtml() { 435 #if defined (DEV) 436 loadString(); 437 438 #endif 439 HTML = head; 440 HTML += nav; 441 page = SPIFFS.open("/about", 442 "r"); 443 HTML += page.readString(); 444 page.close(); 445 Counter = counter; 446 447 HTML += "<br><p>" + Counter + " Vistors</p>"; 448 HTML += footer; 449 server.send(200, 450 "text/html", HTML); 451} 452void siteHtml() { 453 #if defined (DEV) 454 loadString(); 455 456 #endif 457 getMacAddr(); 458 page = SPIFFS.open("/comments.txt", "r"); 459 460 test = true; 461 do { 462 admin = page.readStringUntil('|'); 463 //Serial.println(admin); 464 465 if (admin.length() > 0) { 466 if (admin == macAddr) { 467 page.close(); 468 469 errorRegister(); 470 return; 471 } 472 } else { 473 test 474 = false; 475 page = SPIFFS.open("/comments.txt", "a+"); 476 page.print(macAddr 477 + "|"); 478 page.close(); 479 previousMillis = millis(); 480 } 481 482 } while (test); 483 page = SPIFFS.open (LOGBOOK, "a+" ); 484 page.print(server.arg(0).c_str()); 485 486 page.print("|"); 487 String Comm = (server.arg(1).c_str()); 488 Comm.replace("\ 489", 490 " "); 491 Comm.replace("\ ", " "); 492 page.print(Comm); 493 page.print("|"); 494 495 Comm = (server.arg(2).c_str()); 496 Comm.replace("\ 497", " "); 498 Comm.replace("\ ", 499 " "); 500 page.print(Comm); 501 page.print("|"); 502 Comm = server.arg(3).c_str(); 503 504 Comm.replace("\ 505", " "); 506 Comm.replace("\ ", " "); 507 page.println(Comm); 508 509 page.close(); 510 HTML = head; 511 HTML += nav; 512 page = SPIFFS.open("/site", 513 "r"); 514 HTML += page.readString(); 515 page.close(); 516 Counter = counter; 517 518 HTML += "<br><p>" + Counter + " Vistors</p>"; 519 HTML += footer; 520 server.send(200, 521 "text/html", HTML); 522} 523void errorRegister() { 524 #if defined (DEV) 525 526 loadString(); 527 #endif 528 HTML = head; 529 HTML += nav; 530 page = SPIFFS.open("/error", 531 "r"); 532 HTML += page.readString(); 533 page.close(); 534 Counter = counter; 535 536 HTML += "<br><p>" + Counter + " Vistors</p>"; 537 HTML += footer; 538 server.send(200, 539 "text/html", HTML); 540} 541 542void getMacAddr() { 543 544 struct station_info 545 *stat_info; 546 547 stat_info = wifi_softap_get_station_info(); 548 549 macAddr 550 = String(stat_info->bssid[0], HEX); 551 macAddr += String(stat_info->bssid[1], 552 HEX); 553 macAddr += String(stat_info->bssid[2], HEX); 554 macAddr += String(stat_info->bssid[3], 555 HEX); 556 macAddr += String(stat_info->bssid[4], HEX); 557 macAddr += String(stat_info->bssid[5], 558 HEX); 559} 560void loadString() { 561 page = SPIFFS.open("/count.txt", "r"); 562 563 Counter = page.readString(); 564 page.close(); 565 counter = Counter.toInt(); 566 567 page = SPIFFS.open("/head", "r"); 568 head = page.readString(); 569 page.close(); 570 571 page = SPIFFS.open("/nav", "r"); 572 nav = page.readString(); 573 page.close(); 574 575 page = SPIFFS.open("/footer", "r"); 576 footer = page.readString(); 577 page.close(); 578} 579String 580 getContentType(String filename) { // convert the file extension to the MIME type 581 582 if (filename.endsWith(".html")) return "text/html"; 583 else if (filename.endsWith(".css")) 584 return "text/css"; 585 else if (filename.endsWith(".js")) return "application/javascript"; 586 587 else if (filename.endsWith(".ico")) return "image/x-icon"; 588 return "text/plain"; 589} 590
Guestbook
arduino
A simple web server
1#include <ESP8266WebServer.h> 2#include <FS.h> 3#include <DNSServer.h> 4 5extern "C" { 6#include<user_interface.h> 7} 8 9#define LOGBOOK "/logbook.txt" 10 11//#define DEV //devoleper mode. disable it, for faster web sites 12 13const byte DNS_PORT = 53; 14const char* user = "admin"; 15const char* pass = "esp8266"; 16const char* dns = "guestbook.com"; 17const char* ssid = "Guestbook"; 18const char* password = ""; 19uint8_t ip[] = {192,168,0,1}; 20 21int x, xx, counter; 22 23unsigned long currentMillis; 24unsigned long previousMillis = 0; 25const long interval = 600000; //delay time for delete 26 27String HTML, fileName, fileEdit, admin; 28String Year, Month, Day, rows; 29String Vname, Nname, Comment, Counter, head, nav, footer, macAddr; 30 31String formatBytes(size_t bytes) { 32 if (bytes < 1024) { 33 return String(bytes) + "B"; 34 } else if (bytes < (1024 * 1024)) { 35 return String(bytes / 1024.0) + "KB"; 36 } else if (bytes < (1024 * 1024 * 1024)) { 37 return String(bytes / 1024.0 / 1024.0) + "MB"; 38 } else { 39 return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB"; 40 } 41} 42 43bool test; 44 45File page; 46DNSServer dnsServer; 47// Create an instance of the server on Port 80 48ESP8266WebServer server(80); 49 50String getContentType(String filename); 51void notFound();// Sendet "Not Found"-Seite 52void handleUnknown(); // Liefert Web-Seiten aus dem SPIFFS 53void handleFileUpload();// upload a new file to the SPIFFS 54void adminHtml(); 55void logHtml(); 56void indexHtml(); 57void aboutHtml(); 58void getMacAddr(); 59 60void setup() 61{ //Serial.begin(9600); 62 SPIFFS.begin(); 63 //delay(100); 64 //loadConfig(); 65 IPAddress apIP(ip[0],ip[1],ip[2],ip[3]); // set IP-address 66 WiFi.mode(WIFI_AP_STA); 67 WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // subnet FF FF FF 00 68 WiFi.softAP(ssid,password, 1, 0, 1); // ssid,pw,channel,hidden,max conections 69 70 dnsServer.setTTL(300); 71 dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure); 72 dnsServer.start(DNS_PORT, dns, apIP); 73 74 server.on("/admin", HTTP_POST, // if the client posts to the upload page 75 []() { 76 server.send(200); 77 }, // Send status 200 (OK) to tell the client we are ready to receive 78 handleFileUpload // Receive and save the file 79 ); 80 server.on("/admin", []() { 81 if (!server.authenticate(user, pass)) 82 return server.requestAuthentication(); 83 adminHtml(); 84 server.send(303); 85 }); 86 server.on("/log.html", logHtml); 87 server.on("/", indexHtml); 88 server.on("/index.html", indexHtml); 89 server.on("/about.html", aboutHtml); 90 server.on("/site.html", siteHtml); 91 server.onNotFound(handleUnknown); 92 server.begin(); 93 94#if not defined (DEV) 95 loadString(); 96#endif 97} 98 99void loop() 100{ 101 // Check if a client has connected 102 server.handleClient(); 103 dnsServer.processNextRequest(); 104 currentMillis = millis(); 105 if (currentMillis - previousMillis >= interval) { 106 previousMillis = currentMillis; 107 108 SPIFFS.remove("/comments.txt"); 109 } 110} 111// Sendet "Not Found"-Seite 112void notFound() 113{ HTML = F("<html><head><title>404 Not Found</title></head><body>" 114 "<body><h1>Page not found</h1>" 115 "<br><p>404-error</p>" 116 //"<body background='/404-error.jpg'>" 117 "</body></html>"); 118 server.send(404, "text/html", HTML); 119} 120 121// Es wird versucht, die angegebene Datei aus dem SPIFFS hochzuladen 122void handleUnknown() 123{ fileName = server.uri(); 124 page = SPIFFS.open(fileName, "r"); 125 if (page) 126 { String contentTyp = getContentType(fileName); 127 size_t sent = server.streamFile(page, contentTyp); 128 page.close(); 129 } 130 else { 131 notFound(); 132 } 133} 134void logHtml() 135{ 136 #if defined (DEV) 137 loadString(); 138 #endif 139 page = SPIFFS.open (LOGBOOK, "r" ) ; 140 HTML = head; 141 HTML += nav; 142 HTML += "<br><br><br><center><h1>Guestbook Log</h1>"; 143 HTML += "<p>Here you can see all people that have found the accesspoint.</p>"; 144 HTML += "<br><br>"; 145 HTML += "<table><th>Date</th><th>Name</th><th>Comment</th>"; 146 test = true; 147 do { 148 admin = page.readStringUntil('\n'); 149 if (admin.length() > 0) { 150 151 Year = admin.substring(0, 4); 152 Month = admin.substring(5, 7); 153 Day = admin.substring(8, 10); 154 for (x = 12; x < admin.length(); x++) 155 { 156 if (admin.charAt(x) == '|') { 157 Vname = admin.substring(11, x); 158 break; 159 } 160 } 161 for (xx = x + 1; xx < admin.length(); xx++) { 162 if (admin.charAt(xx) == '|') { 163 Nname = admin.substring(x + 1, xx); 164 break; 165 } 166 } 167 Comment = admin.substring(xx + 1, admin.length() - 1); 168 HTML += "<tr><td>" + Month + ", " + Day + ", " + Year + " </td>"; 169 HTML += "<td>" + Vname + " " + Nname + " </td>"; 170 rows = "1"; 171 if (Comment.length() > 80) rows = "2"; 172 if (Comment.length() > 160) rows = "3"; 173 if (Comment.length() > 240) rows = "4"; 174 HTML += "<td><textarea rows='" + rows + "' cols='80'>" + Comment + "</textarea></td></tr>"; 175 } else { 176 test = false; 177 page.close(); 178 } 179 } while (test); 180 Counter = counter; 181 HTML += "</table></center><br><br>"; 182 HTML += "<br><p>" + Counter + " Vistors</p>"; 183 //HTML+="<p>"+server.client().remoteIP().toString()+"</p>"; 184 //getMacAddr(); 185 //HTML+="<p>"+macAddr+"</p>"; 186 HTML += footer; 187 server.send(200, "text/html", HTML); 188} 189void adminHtml() { 190 #if defined (DEV) 191 loadString(); 192 #endif 193 if (server.hasArg("format")) { 194 SPIFFS.format(); 195 } 196 if (server.hasArg("message")) { 197 admin = server.arg(0).c_str(); 198 admin.replace("%0D%0A++", "\ 199"); 200 page = SPIFFS.open (fileEdit, "w" ); 201 page.print(admin); 202 page.close(); 203 } 204 if (server.hasArg("delete")) { 205 fileName = server.arg(0).c_str(); 206 fileName = "/" + fileName; 207 SPIFFS.remove(fileName); 208 } 209 if (server.hasArg("reboot")) { 210 ESP.restart(); 211 } 212 if (server.hasArg("show")) { 213 fileEdit = server.arg(0).c_str(); 214 fileEdit = "/" + fileEdit; 215 page = SPIFFS.open (fileEdit, "r" ); 216 HTML = head; 217 HTML += nav; 218 HTML += "<center><h1>Edit " + fileEdit.substring(1) + "</h1>"; 219 HTML += "<pre><form action='/admin'><br><textarea rows='20' cols='80' name='message'>"; 220 admin = page.readString(); 221 admin.replace("<", "<"); 222 admin.replace(">", ">"); 223 HTML += admin + "</textarea></pre>"; 224 page.close(); 225 HTML += "<br><input type='submit' value='save'></form><br><form action='/admin'><br><input type='submit' value='back'></form><br></center>"; 226 server.send(200, "text/html", HTML); 227 } 228 HTML = head; 229 HTML += nav; 230 HTML += "<center><br><br><br><form method='post' enctype='multipart/form-data'><input type='file' name='name' required><input class='button' type='submit' value='Upload'></form><br>"; 231 HTML += "<table>"; 232 Dir dir = SPIFFS.openDir("/"); 233 while (dir.next()) { 234 HTML += "<tr><td>" + dir.fileName().substring(1) + "</td> "; 235 //HTML+="<td><form action='/admin'><input type='submit' name='show' value='"+dir.fileName().substring(1)+"'></form>"; 236 HTML += "<td><button><a href=admin?show=" + dir.fileName().substring(1) + ">show/edit</a></button></td>"; 237 HTML += "<td><button><a href='" + dir.fileName() + "' download='" + dir.fileName().substring(1) + "'>download</a></button></td> "; 238 HTML += "<td><button><a href=admin?delete=" + dir.fileName().substring(1) + ">delete</a></button></td>"; 239 HTML += "<td style='text-align:right'>" + formatBytes(dir.fileSize()) + "</td></tr> "; 240 } 241 HTML += "</table><br>"; 242 FSInfo fsInfo; 243 SPIFFS.info(fsInfo); 244 HTML += "FS Bytes: "; 245 HTML += fsInfo.usedBytes; 246 HTML += " / "; 247 HTML += fsInfo.totalBytes; 248 HTML += "<br><br><button><a href=admin?reboot=''>restart</a></button><br>"; 249 HTML += "<br><button><a href=admin?format=''>format</a></button>"; 250 HTML += "</center>" + footer; 251 server.send(200, "text/html", HTML); 252} 253void handleFileUpload() { // upload a new file to the SPIFFS 254 HTTPUpload& upload = server.upload(); 255 if (upload.status == UPLOAD_FILE_START) { 256 String filename = upload.filename; 257 if (!filename.startsWith("/")) filename = "/" + filename; 258 page = SPIFFS.open(filename, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist) 259 filename = String(); 260 } else if (upload.status == UPLOAD_FILE_WRITE) { 261 if (page) 262 page.write(upload.buf, upload.currentSize); // Write the received bytes to the file 263 } else if (upload.status == UPLOAD_FILE_END) { 264 if (page) { // If the file was successfully created 265 page.close(); // Close the file again 266 server.sendHeader("Location", "/admin"); // Redirect the client to the success page 267 server.send(303); 268 } else { 269 server.send(500, "text/plain", "500: couldn't create file"); 270 } 271 } 272} 273void indexHtml() { 274#if defined (DEV) 275 loadString(); 276 #endif 277 getMacAddr(); 278 page = SPIFFS.open("/visitors.txt", "r"); 279 test = true; 280 do { 281 admin = page.readStringUntil('|'); 282 if (admin.length() > 0) { 283 if (admin == macAddr) { 284 page.close(); 285 test = false; 286 } 287 } else { 288 test = false; 289 page = SPIFFS.open("/visitors.txt", "a+"); 290 page.print(macAddr + "|"); 291 page.close(); 292 counter++; 293 } 294 } while (test); 295 page = SPIFFS.open("/count.txt", "w"); 296 page.print(counter); 297 HTML = head; 298 HTML += nav; 299 page = SPIFFS.open("/index", "r"); 300 HTML += page.readString(); 301 page.close(); 302 page = SPIFFS.open ("/count.txt", "r" ); 303 Counter = page.readString(); 304 page.close(); 305 HTML += "<br><p>" + Counter + " Vistors</p>"; 306 HTML += footer; 307 server.send(200, "text/html", HTML); 308} 309void aboutHtml() { 310 #if defined (DEV) 311 loadString(); 312 #endif 313 HTML = head; 314 HTML += nav; 315 page = SPIFFS.open("/about", "r"); 316 HTML += page.readString(); 317 page.close(); 318 Counter = counter; 319 HTML += "<br><p>" + Counter + " Vistors</p>"; 320 HTML += footer; 321 server.send(200, "text/html", HTML); 322} 323void siteHtml() { 324 #if defined (DEV) 325 loadString(); 326 #endif 327 getMacAddr(); 328 page = SPIFFS.open("/comments.txt", "r"); 329 test = true; 330 do { 331 admin = page.readStringUntil('|'); 332 //Serial.println(admin); 333 if (admin.length() > 0) { 334 if (admin == macAddr) { 335 page.close(); 336 errorRegister(); 337 return; 338 } 339 } else { 340 test = false; 341 page = SPIFFS.open("/comments.txt", "a+"); 342 page.print(macAddr + "|"); 343 page.close(); 344 previousMillis = millis(); 345 } 346 } while (test); 347 page = SPIFFS.open (LOGBOOK, "a+" ); 348 page.print(server.arg(0).c_str()); 349 page.print("|"); 350 String Comm = (server.arg(1).c_str()); 351 Comm.replace("\ 352", " "); 353 Comm.replace("\ ", " "); 354 page.print(Comm); 355 page.print("|"); 356 Comm = (server.arg(2).c_str()); 357 Comm.replace("\ 358", " "); 359 Comm.replace("\ ", " "); 360 page.print(Comm); 361 page.print("|"); 362 Comm = server.arg(3).c_str(); 363 Comm.replace("\ 364", " "); 365 Comm.replace("\ ", " "); 366 page.println(Comm); 367 page.close(); 368 HTML = head; 369 HTML += nav; 370 page = SPIFFS.open("/site", "r"); 371 HTML += page.readString(); 372 page.close(); 373 Counter = counter; 374 HTML += "<br><p>" + Counter + " Vistors</p>"; 375 HTML += footer; 376 server.send(200, "text/html", HTML); 377} 378void errorRegister() { 379 #if defined (DEV) 380 loadString(); 381 #endif 382 HTML = head; 383 HTML += nav; 384 page = SPIFFS.open("/error", "r"); 385 HTML += page.readString(); 386 page.close(); 387 Counter = counter; 388 HTML += "<br><p>" + Counter + " Vistors</p>"; 389 HTML += footer; 390 server.send(200, "text/html", HTML); 391} 392 393void getMacAddr() { 394 395 struct station_info *stat_info; 396 397 stat_info = wifi_softap_get_station_info(); 398 399 macAddr = String(stat_info->bssid[0], HEX); 400 macAddr += String(stat_info->bssid[1], HEX); 401 macAddr += String(stat_info->bssid[2], HEX); 402 macAddr += String(stat_info->bssid[3], HEX); 403 macAddr += String(stat_info->bssid[4], HEX); 404 macAddr += String(stat_info->bssid[5], HEX); 405} 406void loadString() { 407 page = SPIFFS.open("/count.txt", "r"); 408 Counter = page.readString(); 409 page.close(); 410 counter = Counter.toInt(); 411 page = SPIFFS.open("/head", "r"); 412 head = page.readString(); 413 page.close(); 414 page = SPIFFS.open("/nav", "r"); 415 nav = page.readString(); 416 page.close(); 417 page = SPIFFS.open("/footer", "r"); 418 footer = page.readString(); 419 page.close(); 420} 421String getContentType(String filename) { // convert the file extension to the MIME type 422 if (filename.endsWith(".html")) return "text/html"; 423 else if (filename.endsWith(".css")) return "text/css"; 424 else if (filename.endsWith(".js")) return "application/javascript"; 425 else if (filename.endsWith(".ico")) return "image/x-icon"; 426 return "text/plain"; 427} 428
Comments
Only logged in users can leave comments