Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char* ssid = "INTEL......";
  4. const char* password = "moises.......";
  5.  
  6. int ledPin1 = D4;// pino D1
  7. int ledPin2 = BUILTIN_LED;//led onboard
  8.  
  9. WiFiServer server(80);
  10.  
  11. void setup() {
  12. WiFi.persistent(false);//melhora a estabilidade da placa
  13. Serial.begin(115200);
  14. delay(10);
  15.  
  16. Serial.println();
  17. Serial.println();
  18. Serial.print("Pino 1 = ");
  19. Serial.println(ledPin1);
  20. Serial.print("Pino 2 = ");
  21. Serial.println(ledPin2);
  22.  
  23. pinMode(ledPin1, OUTPUT);
  24. digitalWrite(ledPin1, LOW);
  25.  
  26. pinMode(ledPin2, OUTPUT);
  27. digitalWrite(ledPin2, LOW);
  28.  
  29. Serial.println();
  30. Serial.println();
  31. Serial.print("Connecting to ");
  32. Serial.println(ssid);
  33.  
  34. WiFi.begin(ssid, password);
  35.  
  36. while (WiFi.status() != WL_CONNECTED) {
  37. delay(500);
  38. Serial.print(".");
  39. }
  40. Serial.println("");
  41. Serial.println("WiFi connected");
  42.  
  43. server.begin();
  44. Serial.println("Server started");
  45.  
  46. Serial.print("Use this URL to connect: ");
  47. Serial.print("http://");
  48. Serial.print(WiFi.localIP());
  49. Serial.print("/");
  50. }
  51.  
  52. void loop() {
  53. WiFiClient client = server.available();
  54. if (!client) {
  55. return;
  56. }
  57.  
  58. Serial.println("new client");
  59. while (!client.available()) {
  60. delay(1);
  61. }
  62.  
  63. String request = client.readStringUntil('\r');
  64. Serial.println(request);
  65. client.flush();
  66.  
  67. int value1 = LOW;
  68. int value2 = HIGH; //BUILTIN_LED
  69.  
  70. if (request.indexOf("/LED1=ON") != -1) {
  71. digitalWrite(ledPin1, HIGH);
  72. Serial.println("Led 1 ligado");
  73. value1 = HIGH;
  74. }
  75.  
  76. if (request.indexOf("/LED1=OFF") != -1) {
  77. digitalWrite(ledPin1, LOW);
  78. Serial.println("Led 1 desligado");
  79. value1 = LOW;
  80. }
  81.  
  82. if (request.indexOf("/LED2=ON") != -1) {
  83. digitalWrite(ledPin2, LOW);
  84. Serial.println("Led 2 ligado");
  85. value2 = LOW;
  86. }
  87.  
  88. if (request.indexOf("/LED2=OFF") != -1) {
  89. digitalWrite(ledPin2, HIGH);
  90. Serial.println("Led 2 desligado");
  91. value2 = HIGH;
  92. }
  93.  
  94. // Return the response
  95. String b = "";
  96. b += (F("<!DOCTYPE html>"));
  97. b += (F("<html lang='en'>"));
  98. b += (F("<head>"));
  99. b += (F("<title>MOISES Example</title>"));
  100. b += (F("<meta charset='utf-8'>"));
  101. b += (F("<meta name='viewport' content='width=device-width, initial-scale=1'>"));
  102. b += (F("<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>"));
  103. b += (F("<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>"));
  104. b += (F("<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>"));
  105.  
  106. b += (F("</head>"));
  107. b += (F("<body>"));
  108. b +=(F("<input type='button' class='btn btn-info' id='btn1' onclick='clickBtn1()' value='Ligar'></input>"));
  109. b += (F("<input type='button' class='btn btn-info' id='btn2' onclick='clickBtn2()' value='Ligar'></input>"));
  110. b += (F("<script type='text/javascript'>"));
  111. b += (F("function clickBtn1(){"));
  112. b += (F("xmlhttp = new XMLHttpRequest();"));
  113. b += (F("btn1 = document.getElementById('btn1');"));
  114. b += (F("if (btn1.value == 'Ligar') {"));
  115. b += (F("xmlhttp.onreadystatechange = function() {"));
  116. b += (F("if (this.readyState == 4 && this.status == 200) {"));
  117. b += (F("btn1.value = 'Desligar';"));
  118. b += (F("}"));
  119. b += (F("};"));
  120. b += (F("xmlhttp.open('SET', '/LED1=ON', true);"));
  121. b += (F("xmlhttp.send(); "));
  122. b += (F("} else {"));
  123. b += (F("xmlhttp.onreadystatechange = function() {"));
  124. b += (F("if (this.readyState == 4 && this.status == 200) {"));
  125. b += (F("btn1.value = 'Ligar';"));
  126. b += (F("}"));
  127. b += (F("};"));
  128. b += (F("xmlhttp.open('SET', '/LED1=OFF', true);"));
  129. b += (F("xmlhttp.send(); "));
  130. b += (F("}"));
  131. b += (F("}"));
  132. b += (F("function clickBtn2(){"));
  133. b += (F("xmlhttp = new XMLHttpRequest();"));
  134. b += (F("btn1 = document.getElementById('btn2');"));
  135. b += (F("if (btn1.value == 'Ligar') {"));
  136. b += (F("xmlhttp.onreadystatechange = function() {"));
  137. b += (F("if (this.readyState == 4 && this.status == 200) {"));
  138. b += (F("btn1.value = 'Desligar';"));
  139. b += (F("}"));
  140. b += (F("};"));
  141. b += (F("xmlhttp.open('SET', '/LED2=ON', true);"));
  142. b += (F("xmlhttp.send();"));
  143. b += (F("} else {"));
  144. b += (F("xmlhttp.onreadystatechange = function() {"));
  145. b += (F("if (this.readyState == 4 && this.status == 200) {"));
  146. b += (F("btn1.value = 'Ligar';"));
  147. b += (F("}"));
  148. b += (F("};"));
  149. b += (F("xmlhttp.open('SET', '/LED2=OFF', true);"));
  150. b += (F("xmlhttp.send(); "));
  151. b += (F("}"));
  152. b += (F("}"));
  153. b += (F("</script>"));
  154. b += (F("</body>"));
  155. b += (F("</html>"));
  156.  
  157. client.print(b);
  158. client.flush();
  159. client.stop();
  160. delay(1);
  161. Serial.println("Client disconnected");
  162. Serial.println("");
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement