====== Ceci n'est toujours pas une pipe ====== {{:esp8266:ceci_continue_denepas_etre_une_pipe_280.jpg?200|}} Le code de l'oeuvre dévelopée à [[htttp://snhack.org| SNHack]] {{:esp8266:cecinestpas_v1-2.tgz|}} /* "Ceci n'est pas" est un point d'acces WIFI sur l'absurdité du monde numérique Gepeto@du-libre.org licence artlibre (http://artlibre.org) V 1.2 au 22/5/2016 Base on FSWebServer - Example WebServer with SPIFFS backend for esp8266 Copyright (c) 2015 Hristo Gochkov. All rights reserved. This file is part of the ESP8266WebServer library for Arduino environment. Add on with DNS lib captiv portal This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA upload the contents of the data folder with MkSPIFFS Tool ("ESP8266 Sketch Data Upload" in Tools menu in Arduino IDE) or you can upload the contents of a folder if you CD in that folder and run the following command: for file in `ls -A1`; do curl -F "file=@$PWD/$file" esp8266fs.local/edit; done access the sample web page at http://esp8266fs.local edit the page by going to http://esp8266fs.local/edit */ #include #include #include #include #include "./DNSServer.h" // Patched lib #include #include #define DBG_OUTPUT_PORT Serial #define DEBUG const byte BUFFER_SIZE = 60; const byte DNS_PORT = 53; // Capture DNS requests on port 53 /* Soft AP network parameters */ IPAddress apIP(192, 168, 4, 1); IPAddress netMsk(255, 255, 255, 0); DNSServer dnsServer; // Create the DNS object /** Should I connect to WLAN asap? */ boolean connect; /** Last time I tried to connect to WLAN */ long lastConnectTry = 0; /** Current WLAN status */ int status = WL_IDLE_STATUS; char* nomssid = "cecicelacecicelacecicelacecicelacecicela"; char* passssid = ""; //char* nomhost = ""; //const char* ssid = "cecicelacecicelacecicelacecicelacecicela"; //const char* password = ""; const int TAILLE_NOM = 32; const int NBR_FIC = 100; // on scanne images max int nbr_fic_image = 0 ; // dans le tableau // tableau des fichiers dispo, idx,nom 8.3 char tabl_nom_fichier[NBR_FIC][TAILLE_NOM] = { "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123", "01234567.123" }; String nom_extrait = ""; File fic_image; Ticker flipper; // change index_htm Ticker regarde; // test A0 ESP8266WebServer server(80); //holds the current upload File fsUploadFile; String responseHTML = "" "CaptivePortal" "

Hello World!

This is a captive portal example. All requests will " "be redirected here.

"; String index_htm = "" "\n" "\n" "\n" "" "\n" "\n" "Ceci n'est pas\n" "\n" "

Ceci n'est pas "; String index_fin = "

\n" ""; //format bytes String formatBytes(size_t bytes) { if (bytes < 1024) { return String(bytes) + "B"; } else if (bytes < (1024 * 1024)) { return String(bytes / 1024.0) + "KB"; } else if (bytes < (1024 * 1024 * 1024)) { return String(bytes / 1024.0 / 1024.0) + "MB"; } else { return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB"; } } 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 handleFileReadold(String path) { DBG_OUTPUT_PORT.println(path); if (path.endsWith("/")) path += "index.htm"; server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); String contentType = getContentType(path); String pathWithGz = path + ".gz"; if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { if (SPIFFS.exists(pathWithGz)) path += ".gz"; DBG_OUTPUT_PORT.println("handleFileRead: " + path); File file = SPIFFS.open(path, "r"); size_t sent = server.streamFile(file, contentType); file.close(); return true; } else { //server.send ( 200, "text/html", index_htm ); DBG_OUTPUT_PORT.println("/index.htm"); File file = SPIFFS.open("/index.htm", "r"); size_t sent = server.streamFile(file, "text / html"); file.close(); return true; } } bool handleFileRead(String path) { DBG_OUTPUT_PORT.println("handleFileRead: " + 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"); server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); size_t sent = server.streamFile(file, contentType); file.close(); return true; } //File file = SPIFFS.open("/index.htm", "r"); server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); //size_t sent = server.streamFile(file, contentType); //file.close(); server.send(200, "text / plain", index_htm); return true; } 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; DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename); fsUploadFile = SPIFFS.open(filename, "w"); filename = String(); } else if (upload.status == UPLOAD_FILE_WRITE) { //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize); if (fsUploadFile) fsUploadFile.write(upload.buf, upload.currentSize); } else if (upload.status == UPLOAD_FILE_END) { if (fsUploadFile) fsUploadFile.close(); DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize); } } void handleFileDelete() { if (server.args() == 0) return server.send(500, "text / plain", "BAD ARGS"); String path = server.arg(0); DBG_OUTPUT_PORT.println("handleFileDelete: " + path); if (path == " / ") return server.send(500, "text / plain", "BAD PATH"); if (!SPIFFS.exists(path)) return server.send(404, "text / plain", "FileNotFound"); SPIFFS.remove(path); server.send(200, "text / plain", ""); path = String(); } void handleFileCreate() { if (server.args() == 0) return server.send(500, "text / plain", "BAD ARGS"); String path = server.arg(0); DBG_OUTPUT_PORT.println("handleFileCreate: " + path); if (path == " / ") return server.send(500, "text / plain", "BAD PATH"); if (SPIFFS.exists(path)) return server.send(500, "text / plain", "FILE EXISTS"); File file = SPIFFS.open(path, "w"); if (file) file.close(); else return server.send(500, "text / plain", "CREATE FAILED"); server.send(200, "text / plain", ""); path = String(); } void handleFileList() { if (!server.hasArg("dir")) { server.send(500, "text / plain", "BAD ARGS"); return; } String path = server.arg("dir"); DBG_OUTPUT_PORT.println("handleFileList: " + path); Dir dir = SPIFFS.openDir(path); path = String(); String output = "["; while (dir.next()) { File entry = dir.openFile("r"); if (output != "[") output += ','; bool isDir = false; output += " {\"type\":\""; output += (isDir) ? "dir" : "file"; output += "\",\"name\":\""; output += String(entry.name()).substring(1); output += "\"}"; entry.close(); } output += "]"; server.send(200, "text/json", output); } void randomchoix() { int no_random_fic = 0; no_random_fic = random(1, nbr_fic_image) ; String nom_fichier = tabl_nom_fichier[no_random_fic]; int pos1 = nom_fichier.indexOf('.'); nom_extrait = nom_fichier.substring(8, pos1); pos1 = strlen(nomssid); nom_extrait.toCharArray(nomssid, pos1) ; // le ssid prend le nom de l'image index_htm += tabl_nom_fichier[no_random_fic]; index_htm += index_milieu; index_htm += nom_extrait; index_htm += index_fin ; //File file = SPIFFS.open("/index.htm", "w"); //file.write(index_htm, (size_t)sizeof(index_htm)); //file.close(); DBG_OUTPUT_PORT.print(nom_extrait); DBG_OUTPUT_PORT.printf(" => %s\n" , tabl_nom_fichier[no_random_fic]); } void setup(void) { //init tableau des fichiers for ( int i = 0; i < NBR_FIC; i++) { for (int j = 0; j < 13; j++) tabl_nom_fichier[i][j] = '0'; } DBG_OUTPUT_PORT.begin(115200); DBG_OUTPUT_PORT.print("\n"); DBG_OUTPUT_PORT.setDebugOutput(true); SPIFFS.begin(); { Dir dir = SPIFFS.openDir("/"); char str[14] = ""; while (dir.next()) { String fileName = dir.fileName(); if (fileName.startsWith("/images/")) { nbr_fic_image++; fileName.toCharArray(tabl_nom_fichier[nbr_fic_image], TAILLE_NOM) ; // rempli le tableau des images } size_t fileSize = dir.fileSize(); DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); } DBG_OUTPUT_PORT.printf("\n"); randomchoix(); //WIFI INIT Serial.print("Configuring access point..."); WiFi.softAPConfig(apIP, apIP, netMsk); delay(500); WiFi.disconnect(); if (strlen(passssid) == 0) { WiFi.softAP(nomssid); delay(500); DBG_OUTPUT_PORT.print("ouverture de "); DBG_OUTPUT_PORT.println(nomssid); } else { WiFi.softAP(nomssid, passssid); delay(500); DBG_OUTPUT_PORT.print("ouverture et paswd de "); DBG_OUTPUT_PORT.print(nomssid); DBG_OUTPUT_PORT.print(" "); DBG_OUTPUT_PORT.println(passssid); } delay(100); // Setup the DNS server redirecting all the domains to the apIP dnsServer.setErrorReplyCode(DNSReplyCode::NoError); dnsServer.start(DNS_PORT, "*", apIP); Serial.print("AP IP address: "); Serial.println(WiFi.softAPIP()); delay(2000); DBG_OUTPUT_PORT.println(""); delay(100); //SERVER INIT //list directory server.on("/list", HTTP_GET, handleFileList); //load editor server.on("/edit", HTTP_GET, []() { if (!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound"); }); //create file server.on("/edit", HTTP_PUT, handleFileCreate); //delete file server.on("/edit", HTTP_DELETE, handleFileDelete); //first callback is called after the request has ended with all parsed arguments //second callback handles file uploads at that location server.on("/edit", HTTP_POST, []() { server.send(200, "text/plain", ""); }, handleFileUpload); //called when the url is not defined here //use it to load content from SPIFFS server.onNotFound([]() { if (!handleFileRead(server.uri())) server.send(404, "text/plain", "FileNotFound"); }); //get heap status, analog input value and all GPIO statuses in one json call server.on("/all", HTTP_GET, []() { String json = "{"; json += "\"heap\":" + String(ESP.getFreeHeap()); json += ", \"analog\":" + String(analogRead(A0)); json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16))); json += "}"; server.send(200, "text/json", json); json = String(); }); server.begin(); DBG_OUTPUT_PORT.println("HTTP server started"); //regarde.attach(10, changement ); // toutes les 10s on scanne A0 flipper.attach(600, suivant ); // toutes les 10m on reboot } } void suivant() { ESP.restart(); } void changement() { int val = analogRead(A0) / 4; if (val > 2) { DBG_OUTPUT_PORT.println(val); suivant(); } } void loop(void) { server.handleClient(); dnsServer.processNextRequest(); }