Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- const char* ssid = "SuaSSID";
- const char* password = "SuaSenha";
- #define VERSION "0.1"
- ESP8266WebServer server(80);
- int pin [] = {D1, BUILTIN_LED}; // pinos utilizados Array
- int pinCount = 2; // declara numero de pinos utilizados
- byte output_status = 0; // status dos pinos ou deveria ser hehe
- void shiftWrite(byte value) { // precisaria configurar as condições do array aqui
- for (int thisPin = 0; thisPin < pinCount; thisPin++) {
- // turn the pin on:
- digitalWrite(pin[thisPin], output_status);
- delay(timer);
- // turn the pin off:
- digitalWrite(pin[thisPin],output_status);
- }
- // loop from the highest pin to the lowest:
- for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
- // turn the pin on:
- digitalWrite(pin[thisPin], HIGH);
- delay(timer);
- // turn the pin off:
- digitalWrite(pin[thisPin], LOW);
- }
- }
- }
- void handleTest() {
- server.send(200, "text/html", F(""));
- }
- void handleAbout() {
- String message = "";
- message += "Firmware Version: " VERSION "\n";
- message += "Compile Date: " __DATE__ " " __TIME__ "\n";
- message += "Source: " __FILE__ "\n";
- message += "Flash Chip ID: ";
- message += ESP.getFlashChipId();
- message += "\nFlash Chip Real Size: ";
- message += ESP.getFlashChipRealSize();
- message += "\nFlash Chip Size: ";
- message += ESP.getFlashChipSize();
- message += "\nFlash Chip Speed: ";
- message += ESP.getFlashChipSpeed();
- message += "\nFlash Chip Mode: ";
- message += ESP.getFlashChipMode();
- message += "\nIP Address: ";
- message += WiFi.localIP();
- message += "\n";
- server.send(200, "text/plain", message);// concatena o texto da pagina HTML na string "message"
- }
- void handleRoot() {// monta a pagina HTML
- String message = ""
- "<!DOCTYPE html>"
- "<html lang=\"en\">"
- " <head>"
- " <meta charset=\"utf-8\">"
- " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">"
- " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
- " <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->"
- " <title>WebServer ESP8266 Controller</title>"
- ""
- " <!-- Bootstrap -->"
- " <link href=\"https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/darkly/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-kVo/Eh0sv7ZdiwZK32nRsp1FrDT3sLRLx3zVpSSTI9UdO5H02LJNLBg5F1gwvKg0\" crossorigin=\"anonymous\">"
- ""
- " <!-- Custom styles for this template -->"
- " <link href=\"bootstrap.css\" rel=\"stylesheet\">"
- ""
- " <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->"
- " <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->"
- " <!--[if lt IE 9]>"
- " <script src=\"https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js\"></script>"
- " <script src=\"https://oss.maxcdn.com/respond/1.4.2/respond.min.js\"></script>"
- " <![endif]-->"
- " </head>"
- " <body>"
- " "
- "<h1>WebServer ESP8266 Controller</h1>"
- " <!-- Fixed navbar -->"
- " <nav class=\"navbar navbar-default navbar-fixed-top\">"
- " <div class=\"container\">"
- " <div class=\"navbar-header\">"
- " <button type=\"button\" class=\"navbar-toggle collapsed\" data-toggle=\"collapse\" data-target=\"#navbar\" aria-expanded=\"false\" aria-controls=\"navbar\">"
- " <span class=\"sr-only\">Toggle navigation</span>"
- " <span class=\"icon-bar\"></span>"
- " <span class=\"icon-bar\"></span>"
- " <span class=\"icon-bar\"></span>"
- " </button>"
- " </div>"
- " <div id=\"navbar\" class=\"collapse navbar-collapse\">"
- " <ul class=\"nav navbar-nav\">"
- " </ul>"
- " <ul class=\"nav navbar-nav navbar-right\">"
- " </ul>"
- " </div><!--/.nav-collapse -->"
- " </div>"
- " </nav>"
- ""
- " <!-- Begin page content -->"
- " <div class=\"container\">"
- " <div class=\"page-header\">"
- " "
- " </div>"
- " </div>"
- ""
- " <div class=\"container\">"
- "";
- for (int c = 0; c < 2; c++) { // cria o numero de botões desejados, neste caso 2
- message += "<p>";
- message += "botão ";
- message += c + 1;
- message += " \n";
- // Print First Button (The OFF Button)// adiciona os botões on/off
- if (output_status & (pin << c)) {
- // Currently On
- message += " <a href=\"switch?pin=";
- message += c;
- message += "&state=0\" class=\"btn btn-default\">Off</a>";// state=0 condição OFF
- } else {
- // Currently Off
- message += " <a href=\"switch?pin=";
- message += c;
- message += "&state=0\" class=\"btn btn-success\">Off</a>";
- }
- // Print Second Button (The ON Button)
- if (output_status & (pin << c)) {
- // Currently On
- message += " <a href=\"switch?pin=";
- message += c;
- message += "&state=1\" class=\"btn btn-warning\">On</a>";// state=1 condição ON
- } else {
- // Currently Off
- message += " <a href=\"switch?pin=";
- message += c;
- message += "&state=1\" class=\"btn btn-default\">On</a>";
- }
- message += "</tr>\n";
- }
- message += ""
- " "
- " </div>"
- ""
- ""
- " <!-- Bootstrap core JavaScript"
- " ================================================== -->"
- " <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->"
- " <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>"
- " <!-- Include all compiled plugins (below), or include individual files as needed -->"
- " <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js\" integrity=\"sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS\" crossorigin=\"anonymous\"></script>"
- ""
- " </body>"
- "</html>"
- "";
- server.send(200, "text/html", message);
- }
- void handleSwitch() {// deveria recuperar a condição dos pinos do esp aqui
- int pin = server.arg(0).toInt(); // identifica os pinos utilizados
- int state = server.arg(1).toInt(); // identifica o estado do botão no html
- bitWrite(output_status, pin, state);// configura o estado dos pinos usando shiftWrite(output_status) lá de cima
- shiftWrite(output_status);
- handleRoot();
- }
- void handleNotFound() {
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET) ? "GET" : "POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i = 0; i < server.args(); i++) {
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- }
- void check_wifi(void) {
- // Wait for connection
- Serial.print("CONNECTING TO WIFI:");
- // Wait until we are reconnected
- while (WiFi.status() != WL_CONNECTED) {
- // Flash the LED that's connected to the enable pin while waiting for Wifi
- Serial.print(".");
- delay(250);
- }
- Serial.println(":CONNECTED");
- }
- void setup(void) {
- // Enable all outputs
- for (int thisPin = 0; thisPin < pinCount; thisPin++) {
- pinMode(pin[thisPin], OUTPUT);
- }
- Serial.begin(115200);
- Serial.println("Device Power Up");
- WiFi.begin(ssid, password);
- Serial.println("");
- check_wifi();
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- if (MDNS.begin("sprinkler")) {
- Serial.println("MDNS responder started");
- }
- server.on("/", handleRoot);
- server.on("/switch", handleSwitch);
- server.on("/about", handleAbout);
- server.on("/inline", []() {
- server.send(200, "text/plain", "this works as well");
- });
- server.onNotFound(handleNotFound);
- server.begin();
- Serial.println("HTTP server started");
- // Setup temperature sensor
- // locate devices on the bus
- Serial.print("Locating devices...");
- Serial.print("Found ");
- Serial.println(" devices.");
- Serial.println();
- Serial.println("DONE");
- //
- }
- void loop() {
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement