Advertisement
tbatista

WEBserverRelayControlSwfit

Jan 22nd, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5.  
  6. const char* ssid = "SuaSSID";
  7. const char* password = "SuaSenha";
  8.  
  9.  
  10. #define VERSION "0.1"
  11.  
  12. ESP8266WebServer server(80);
  13.  
  14. int pin [] = {D1, BUILTIN_LED}; // pinos utilizados Array
  15. int pinCount = 2; // declara numero de pinos utilizados
  16. byte output_status = 0; // status dos pinos ou deveria ser hehe
  17.  
  18.  
  19. void shiftWrite(byte value) { // precisaria configurar as condições do array aqui
  20.  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
  21.     // turn the pin on:
  22.     digitalWrite(pin[thisPin], output_status);
  23.     delay(timer);
  24.     // turn the pin off:
  25.     digitalWrite(pin[thisPin],output_status);
  26.  
  27.   }
  28.   // loop from the highest pin to the lowest:
  29.   for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
  30.     // turn the pin on:
  31.     digitalWrite(pin[thisPin], HIGH);
  32.     delay(timer);
  33.     // turn the pin off:
  34.     digitalWrite(pin[thisPin], LOW);
  35.   }
  36. }
  37. }
  38.  
  39. void handleTest() {
  40.   server.send(200, "text/html", F(""));
  41. }
  42.  
  43. void handleAbout() {
  44.   String message = "";
  45.   message += "Firmware Version: " VERSION "\n";
  46.   message += "Compile Date: " __DATE__ " " __TIME__ "\n";
  47.   message += "Source: " __FILE__ "\n";
  48.   message += "Flash Chip ID: ";
  49.   message += ESP.getFlashChipId();
  50.   message += "\nFlash Chip Real Size: ";
  51.   message += ESP.getFlashChipRealSize();
  52.   message += "\nFlash Chip Size: ";
  53.   message += ESP.getFlashChipSize();
  54.   message += "\nFlash Chip Speed: ";
  55.   message += ESP.getFlashChipSpeed();
  56.   message += "\nFlash Chip Mode: ";
  57.   message += ESP.getFlashChipMode();
  58.   message += "\nIP Address: ";
  59.   message += WiFi.localIP();
  60.   message += "\n";
  61.  
  62.   server.send(200, "text/plain", message);// concatena o texto da pagina HTML na string "message"
  63. }
  64.  
  65. void handleRoot() {// monta a pagina HTML
  66.   String message = ""
  67.                    "<!DOCTYPE html>"
  68.                    "<html lang=\"en\">"
  69.                    "  <head>"
  70.                    "    <meta charset=\"utf-8\">"
  71.                    "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">"
  72.                    "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
  73.                    "    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->"
  74.                    "    <title>WebServer ESP8266 Controller</title>"
  75.                    ""
  76.                    "    <!-- Bootstrap -->"
  77.                    "    <link href=\"https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/darkly/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-kVo/Eh0sv7ZdiwZK32nRsp1FrDT3sLRLx3zVpSSTI9UdO5H02LJNLBg5F1gwvKg0\" crossorigin=\"anonymous\">"
  78.                    ""
  79.                    "    <!-- Custom styles for this template -->"
  80.                    "    <link href=\"bootstrap.css\" rel=\"stylesheet\">"
  81.                    ""
  82.                    "    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->"
  83.                    "    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->"
  84.                    "    <!--[if lt IE 9]>"
  85.                    "      <script src=\"https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js\"></script>"
  86.                    "      <script src=\"https://oss.maxcdn.com/respond/1.4.2/respond.min.js\"></script>"
  87.                    "    <![endif]-->"
  88.                      "  </head>"
  89.                    "  <body>"
  90.                    "    "
  91.                    "<h1>WebServer ESP8266 Controller</h1>"
  92.                    "    <!-- Fixed navbar -->"
  93.                    "    <nav class=\"navbar navbar-default navbar-fixed-top\">"
  94.                    "      <div class=\"container\">"
  95.                    "        <div class=\"navbar-header\">"
  96.                    "          <button type=\"button\" class=\"navbar-toggle collapsed\" data-toggle=\"collapse\" data-target=\"#navbar\" aria-expanded=\"false\" aria-controls=\"navbar\">"
  97.                    "            <span class=\"sr-only\">Toggle navigation</span>"
  98.                    "            <span class=\"icon-bar\"></span>"
  99.                    "            <span class=\"icon-bar\"></span>"
  100.                    "            <span class=\"icon-bar\"></span>"
  101.                    "          </button>"
  102.  
  103.                    "        </div>"
  104.                    "        <div id=\"navbar\" class=\"collapse navbar-collapse\">"
  105.                    "          <ul class=\"nav navbar-nav\">"
  106.                    "          </ul>"
  107.                    "          <ul class=\"nav navbar-nav navbar-right\">"
  108.                    "          </ul>"
  109.                    "        </div><!--/.nav-collapse -->"
  110.                    "      </div>"
  111.                    "    </nav>"
  112.                    ""
  113.                    "    <!-- Begin page content -->"
  114.                    "    <div class=\"container\">"
  115.                    "      <div class=\"page-header\">"
  116.                    "        "
  117.                    "      </div>"
  118.                    "    </div>"
  119.                    ""
  120.                    "    <div class=\"container\">"
  121.                    "";
  122.  
  123.   for (int c = 0; c < 2; c++) { // cria o numero de botões desejados, neste caso 2
  124.     message += "<p>";
  125.  
  126.     message += "botão ";
  127.     message += c + 1;
  128.     message += " &nbsp;\n";
  129.  
  130.     // Print First Button (The OFF Button)// adiciona os botões on/off
  131.     if (output_status & (pin << c)) {
  132.       // Currently On
  133.       message += "            <a href=\"switch?pin=";
  134.       message += c;
  135.       message += "&state=0\" class=\"btn btn-default\">Off</a>";// state=0 condição OFF
  136.     } else {
  137.       // Currently Off
  138.       message += "            <a href=\"switch?pin=";
  139.       message += c;
  140.       message += "&state=0\" class=\"btn btn-success\">Off</a>";
  141.     }
  142.  
  143.     // Print Second Button (The ON Button)
  144.     if (output_status & (pin << c)) {
  145.       // Currently On
  146.       message += "            <a href=\"switch?pin=";
  147.       message += c;
  148.       message += "&state=1\" class=\"btn btn-warning\">On</a>";// state=1 condição ON
  149.     } else {
  150.       // Currently Off
  151.       message += "            <a href=\"switch?pin=";
  152.       message += c;
  153.       message += "&state=1\" class=\"btn btn-default\">On</a>";
  154.     }
  155.     message += "</tr>\n";
  156.   }
  157.   message += ""
  158.              "       "
  159.              "    </div>"
  160.              ""
  161.              ""
  162.              "    <!-- Bootstrap core JavaScript"
  163.              "    ================================================== -->"
  164.              "    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->"
  165.              "    <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>"
  166.              "    <!-- Include all compiled plugins (below), or include individual files as needed -->"
  167.              "    <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js\" integrity=\"sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS\" crossorigin=\"anonymous\"></script>"
  168.              ""
  169.              "  </body>"
  170.              "</html>"
  171.              "";
  172.  
  173.  
  174.   server.send(200, "text/html", message);
  175. }
  176.  
  177. void handleSwitch() {// deveria recuperar a condição dos pinos do esp aqui
  178.   int pin = server.arg(0).toInt(); // identifica os pinos utilizados
  179.   int state = server.arg(1).toInt(); // identifica o estado do botão no html
  180.   bitWrite(output_status, pin, state);// configura o estado dos pinos usando   shiftWrite(output_status) lá de cima
  181.   shiftWrite(output_status);
  182.   handleRoot();
  183. }
  184.  
  185. void handleNotFound() {
  186.   String message = "File Not Found\n\n";
  187.   message += "URI: ";
  188.   message += server.uri();
  189.   message += "\nMethod: ";
  190.   message += (server.method() == HTTP_GET) ? "GET" : "POST";
  191.   message += "\nArguments: ";
  192.   message += server.args();
  193.   message += "\n";
  194.   for (uint8_t i = 0; i < server.args(); i++) {
  195.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  196.   }
  197.   server.send(404, "text/plain", message);
  198. }
  199.  
  200. void check_wifi(void) {
  201.   // Wait for connection
  202.  
  203.  
  204.   Serial.print("CONNECTING TO WIFI:");
  205.   // Wait until we are reconnected
  206.   while (WiFi.status() != WL_CONNECTED) {
  207.     // Flash the LED that's connected to the enable pin while waiting for Wifi
  208.  
  209.     Serial.print(".");
  210.     delay(250);
  211.   }
  212.   Serial.println(":CONNECTED");
  213. }
  214.  
  215.  
  216. void setup(void) {
  217.   // Enable all outputs
  218.  
  219.   for (int thisPin = 0; thisPin < pinCount; thisPin++) {
  220.     pinMode(pin[thisPin], OUTPUT);
  221.   }
  222.  
  223.     Serial.begin(115200);
  224.     Serial.println("Device Power Up");
  225.  
  226.     WiFi.begin(ssid, password);
  227.     Serial.println("");
  228.  
  229.     check_wifi();
  230.  
  231.     Serial.println("");
  232.     Serial.print("Connected to ");
  233.     Serial.println(ssid);
  234.     Serial.print("IP address: ");
  235.     Serial.println(WiFi.localIP());
  236.  
  237.     if (MDNS.begin("sprinkler")) {
  238.       Serial.println("MDNS responder started");
  239.     }
  240.  
  241.  
  242.     server.on("/", handleRoot);
  243.  
  244.     server.on("/switch", handleSwitch);
  245.     server.on("/about", handleAbout);
  246.  
  247.     server.on("/inline", []() {
  248.       server.send(200, "text/plain", "this works as well");
  249.     });
  250.  
  251.     server.onNotFound(handleNotFound);
  252.  
  253.     server.begin();
  254.     Serial.println("HTTP server started");
  255.  
  256.     // Setup temperature sensor
  257.     // locate devices on the bus
  258.     Serial.print("Locating devices...");
  259.  
  260.     Serial.print("Found ");
  261.     Serial.println(" devices.");
  262.     Serial.println();
  263.     Serial.println("DONE");
  264.  
  265.     //
  266.  
  267.   }
  268.  
  269.   void loop() {
  270.  
  271.     server.handleClient();
  272.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement