Attachment 'handleHTTP.c'

Download

   1 /** Handle root or redirect to captive portal */
   2 void handleRoot() {
   3   if (captivePortal()) { // If caprive portal redirect instead of displaying the page.
   4     return;
   5   }
   6   server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
   7   server.sendHeader("Pragma", "no-cache");
   8   server.sendHeader("Expires", "-1");
   9   server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
  10   server.sendContent(
  11     "<html><head></head><body>"
  12     "<h1>HELLO WORLD!!</h1>"
  13   );
  14   if (server.client().localIP() == apIP) {
  15     server.sendContent(String("<p>You are connected through the soft AP: ") + softAP_ssid + "</p>");
  16   } else {
  17     server.sendContent(String("<p>You are connected through the wifi network: ") + ssid + "</p>");
  18   }
  19   server.sendContent(
  20     "<p>You may want to <a href='/wifi'>config the wifi connection</a>.</p>"
  21     "</body></html>"
  22   );
  23   server.client().stop(); // Stop is needed because we sent no content length
  24 }
  25 
  26 /** Redirect to captive portal if we got a request for another domain. 
  27 Return true in that case so the page handler do not try to handle the request again. */
  28 boolean captivePortal() {
  29   if (!isIp(server.hostHeader()) && server.hostHeader() != (String(myHostname)+".local")) {
  30     Serial.print("Request redirected to captive portal");
  31     server.sendHeader("Location", String("http://") + toStringIp(server.client().localIP()), true);
  32     server.send ( 302, "text/plain", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
  33     server.client().stop(); // Stop is needed because we sent no content length
  34     return true;
  35   }
  36   return false;
  37 }
  38 
  39 /** Wifi config page handler */
  40 void handleWifi() {
  41   server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  42   server.sendHeader("Pragma", "no-cache");
  43   server.sendHeader("Expires", "-1");
  44   server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
  45   server.sendContent(
  46     "<html><head></head><body>"
  47     "<h1>Wifi config</h1>"
  48   );
  49   if (server.client().localIP() == apIP) {
  50     server.sendContent(String("<p>You are connected through the soft AP: ") + softAP_ssid + "</p>");
  51   } else {
  52     server.sendContent(String("<p>You are connected through the wifi network: ") + ssid + "</p>");
  53   }
  54   server.sendContent(
  55     "\r\n<br />"
  56     "<table><tr><th align='left'>SoftAP config</th></tr>"
  57   );
  58   server.sendContent(String() + "<tr><td>SSID " + String(softAP_ssid) + "</td></tr>");
  59   server.sendContent(String() + "<tr><td>IP " + toStringIp(WiFi.softAPIP()) + "</td></tr>");
  60   server.sendContent(
  61     "</table>"
  62     "\r\n<br />"
  63     "<table><tr><th align='left'>WLAN config</th></tr>"
  64   );
  65   server.sendContent(String() + "<tr><td>SSID " + String(ssid) + "</td></tr>");
  66   server.sendContent(String() + "<tr><td>IP " + toStringIp(WiFi.localIP()) + "</td></tr>");
  67   server.sendContent(
  68     "</table>"
  69     "\r\n<br />"
  70     "<table><tr><th align='left'>WLAN list (refresh if any missing)</th></tr>"
  71   );
  72   Serial.println("scan start");
  73   int n = WiFi.scanNetworks();
  74   Serial.println("scan done");
  75   if (n > 0) {
  76     for (int i = 0; i < n; i++) {
  77       server.sendContent(String() + "\r\n<tr><td>SSID " + WiFi.SSID(i) + String((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":" *") + " (" + WiFi.RSSI(i) + ")</td></tr>");
  78     }
  79   } else {
  80     server.sendContent(String() + "<tr><td>No WLAN found</td></tr>");
  81   }
  82   server.sendContent(
  83     "</table>"
  84     "\r\n<br /><form method='POST' action='wifisave'><h4>Connect to network:</h4>"
  85     "<input type='text' placeholder='network' name='n'/>"
  86     "<br /><input type='password' placeholder='password' name='p'/>"
  87     "<br /><input type='submit' value='Connect/Disconnect'/></form>"
  88     "<p>You may want to <a href='/'>return to the home page</a>.</p>"
  89     "</body></html>"
  90   );
  91   server.client().stop(); // Stop is needed because we sent no content length
  92 }
  93 
  94 /** Handle the WLAN save form and redirect to WLAN config page again */
  95 void handleWifiSave() {
  96   Serial.println("wifi save");
  97   server.arg("n").toCharArray(ssid, sizeof(ssid) - 1);
  98   server.arg("p").toCharArray(password, sizeof(password) - 1);
  99   server.sendHeader("Location", "wifi", true);
 100   server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
 101   server.sendHeader("Pragma", "no-cache");
 102   server.sendHeader("Expires", "-1");
 103   server.send ( 302, "text/plain", "");  // Empty content inhibits Content-length header so we have to close the socket ourselves.
 104   server.client().stop(); // Stop is needed because we sent no content length
 105   saveCredentials();
 106   connect = strlen(ssid) > 0; // Request WLAN connect with new credentials if there is a SSID
 107 }
 108 
 109 void handleNotFound() {
 110   if (captivePortal()) { // If caprive portal redirect instead of displaying the error page.
 111     return;
 112   }
 113   String message = "File Not Found\n\n";
 114   message += "URI: ";
 115   message += server.uri();
 116   message += "\nMethod: ";
 117   message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
 118   message += "\nArguments: ";
 119   message += server.args();
 120   message += "\n";
 121 
 122   for ( uint8_t i = 0; i < server.args(); i++ ) {
 123     message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
 124   }
 125   server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
 126   server.sendHeader("Pragma", "no-cache");
 127   server.sendHeader("Expires", "-1");
 128   server.send ( 404, "text/plain", message );
 129 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-12-20 15:34:00, 3.3 KB) [[attachment:GetTemp.c]]
  • [get | view] (2015-12-18 19:00:00, 79.9 KB) [[attachment:Thingspeak_WarmWasser.png]]
  • [get | view] (2015-12-21 15:53:00, 11.9 KB) [[attachment:WiFiLoggerOneWire.c]]
  • [get | view] (2015-12-20 15:35:00, 1.0 KB) [[attachment:WiFiLoggerOneWire.h]]
  • [get | view] (2015-12-15 21:02:00, 77.5 KB) [[attachment:WiFiLoggerOneWireVZ.png]]
  • [get | view] (2015-12-20 15:05:00, 103.1 KB) [[attachment:WiFiLoggerOneWire_config.png]]
  • [get | view] (2015-12-18 19:51:00, 89.0 KB) [[attachment:WiFiLogger_DSC06322.jpg]]
  • [get | view] (2016-05-08 10:40:00, 36.5 KB) [[attachment:WiFiLogger_screenshot.png]]
  • [get | view] (2016-06-20 05:37:00, 35.1 KB) [[attachment:WiFiLogger_screenshot2.png]]
  • [get | view] (2015-12-15 14:38:00, 100.5 KB) [[attachment:WiFi_Logger_DSC06319.jpg]]
  • [get | view] (2015-12-20 15:33:00, 0.7 KB) [[attachment:credentials.c]]
  • [get | view] (2015-12-20 15:33:00, 3.9 KB) [[attachment:getNTPtime.c]]
  • [get | view] (2015-12-20 15:34:00, 5.4 KB) [[attachment:handleHTTP.c]]
  • [get | view] (2015-12-20 15:35:00, 0.4 KB) [[attachment:tools.c]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.