Advertisement
Guest User

Untitled

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