Advertisement
Guest User

Untitled

a guest
Jan 4th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1.  
  2. #include <ESP8266WiFi.h>
  3.  
  4. //Nome da sua rede Wifi
  5.  
  6. const char* ssid = "moises";
  7.  
  8.  
  9. //Senha da rede
  10.  
  11. const char* password = "moises";
  12.  
  13.  
  14. IPAddress ip(000000000000000);
  15. IPAddress gateway(000000000000);
  16. IPAddress subnet(255, 255, 255, 0);
  17. WiFiServer server(80);
  18.  
  19.  
  20. //Pino do NodeMCU que estara conectado ao rele
  21. const int pin = D4; //Equivalente ao D2 no NodeMCU
  22.  
  23. //Funcao que sera executada apenas ao ligar o ESP8266
  24. void setup() {
  25. //Preparando o pino, que esta lidago ao rele
  26. pinMode(pin, OUTPUT);
  27. digitalWrite(pin, HIGH);
  28.  
  29. //Conectando a rede Wifi
  30. WiFi.config(ip, gateway, subnet);
  31. WiFi.begin(ssid, password);
  32.  
  33.  
  34. //Verificando se esta conectado,
  35. //caso contrario, espera um pouco e verifica de novo.
  36. while (WiFi.status() != WL_CONNECTED) {
  37. delay(500);
  38. }
  39.  
  40. //Iniciando o servidor Web
  41. server.begin();
  42. }
  43.  
  44. //Funcao que sera executada indefinidamente enquanto o NodeMCU estiver ligado.
  45. void loop() {
  46. //Verificando se o servidor esta pronto.
  47. WiFiClient client = server.available();
  48. if (!client) {
  49. return;
  50. }
  51.  
  52. //Verificando se o servidor recebeu alguma requisicao
  53. while (!client.available()) {
  54. delay(1);
  55. }
  56.  
  57. //Obtendo a requisicao vinda do browser
  58. String req = client.readStringUntil('\r');
  59.  
  60. //Sugestao dada por Enrico Orlando
  61. if(req == "GET /favicon.ico HTTP/1.1"){
  62. req = client.readStringUntil('\r');
  63. }
  64.  
  65. client.flush();
  66.  
  67. //Iniciando o buffer que ira conter a pagina HTML que sera enviada para o browser.
  68. String buf = "";
  69.  
  70. buf += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";
  71. buf += "<head> ";
  72. buf += "<meta charset='UTF-8'> ";
  73. buf += "<meta http-equiv='cache-control' content='max-age=0' /> ";
  74. buf += "<meta http-equiv='cache-control' content='no-cache' /> ";
  75. buf += "<meta http-equiv='expires' content='0' /> ";
  76. buf += "<meta http-equiv='expires' content='Tue, 01 Jan 1980 1:00:00 GMT' /> ";
  77. buf += "<meta http-equiv='pragma' content='no-cache' /> ";
  78. buf += "<title>Automa&ccedil;&atilde;o Residencial</title> ";
  79. buf += "<style> ";
  80. buf += "body{font-family:Open Sans; color:#555555;} ";
  81. buf += "h1{font-size:24px; font-weight:normal; margin:0.4em 0;} ";
  82. buf += ".container { width: 100%; margin: 0 auto; } ";
  83. buf += ".container .row { float: left; clear: both; width: 100%; } ";
  84. buf += ".container .col { float: left; margin: 0 0 1.2em; padding-right: 1.2em; padding-left: 1.2em; } ";
  85. buf += ".container .col.four, .container .col.twelve { width: 100%; } ";
  86. buf += "@media screen and (min-width: 767px) { ";
  87. buf += ".container{width: 100%; max-width: 1080px; margin: 0 auto;} ";
  88. buf += ".container .row{width:100%; float:left; clear:both;} ";
  89. buf += ".container .col{float: left; margin: 0 0 1em; padding-right: .5em; padding-left: .5em;} ";
  90. buf += ".container .col.four { width: 50%; } ";
  91. buf += ".container .col.tweleve { width: 100%; } ";
  92. buf += "} ";
  93. buf += "* {-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;} ";
  94. buf += "a{text-decoration:none;} ";
  95. buf += ".btn {font-size: 18px; white-space:nowrap; width:100%; padding:.8em 1.5em; font-family: Open Sans, Helvetica,Arial,sans-serif; ";
  96. buf += "line-height:18px; display: inline-block;zoom: 1; color: #fff; text-align: center; position:relative; ";
  97. buf += "-webkit-transition: border .25s linear, color .25s linear, background-color .25s linear; ";
  98. buf += "transition: border .25s linear, color .25s linear, background-color .25s linear;} ";
  99. buf += ".btn.btn-sea{background-color: #08bc9a; border-color: #08bc9a; -webkit-box-shadow: 0 3px 0 #088d74; box-shadow: 0 3px 0 #088d74;} ";
  100. buf += ".btn.btn-sea:hover{background-color:#01a183;} ";
  101. buf += ".btn.btn-sea:active{ top: 3px; outline: none; -webkit-box-shadow: none; box-shadow: none;} ";
  102. buf += "</style> ";
  103. buf += "</head> ";
  104. buf += "<body> ";
  105. buf += "<div class='container'> ";
  106. buf += "<div class='row'> ";
  107. buf += "<div class='col twelve'> ";
  108. buf += "<p align='center'><font size='20'>Controle de l&acirc;mpadas</font></p> ";
  109. buf += "</div> ";
  110. buf += "</div> ";
  111. buf += "<div class='row'> ";
  112. buf += "<div class='col four'> ";
  113. buf += "<a href='?f=on' class='btn btn-sea'>Ligar</a> ";
  114. buf += "</div> ";
  115. buf += "<div class='col four'> ";
  116. buf += "<a href='?f=off' class='btn btn-sea'>Desligar</a> ";
  117. buf += "</div> ";
  118. buf += "</div> ";
  119. buf += "<div class='col twelve'> ";
  120.  
  121. buf += "<p align='center'><font size='5'>Automation</font></p> ";
  122. buf += "</div> ";
  123. buf += "</div> ";
  124. buf += "</body> ";
  125. buf += "</html> ";
  126.  
  127. //Enviando para o browser a 'pagina' criada.
  128. client.print(buf);
  129. client.flush();
  130.  
  131. //Analisando a requisicao recebida para decidir se liga ou desliga a lampada
  132. if (req.indexOf("on") != -1)
  133. {
  134. digitalWrite(pin, LOW);
  135. }
  136. else if (req.indexOf("off") != -1)
  137. {
  138. digitalWrite(pin, HIGH);
  139. }
  140. else
  141. {
  142. //Requisicao invalida!
  143. client.stop();
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement