Advertisement
Guest User

Untitled

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