Guest User

Untitled

a guest
Apr 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5.  
  6. const char* ssid = "Asif Wifi";
  7. const char* password = "asif0172";
  8.  
  9. ESP8266WebServer server(80);
  10.  
  11. const int output1 = D1;
  12. const int output2 = D2;
  13. const int output3 = D3;
  14. const int output4 = D4;
  15.  
  16. boolean device1 = false;
  17. boolean device2 = false;
  18. boolean device3 = false;
  19. boolean device4 = false;
  20.  
  21. void handleRoot() {
  22. //digitalWrite(led, 1);
  23. //server.send(200, "text/plain", "hello from esp8266!");
  24. //digitalWrite(led, 0);
  25.  
  26. String cmd;
  27. cmd += "<!DOCTYPE HTML>rn";
  28. cmd += "<html>rn";
  29. //cmd += "<header><title>ESP8266 Webserver</title><h1>"ESP8266 Web Server Control"</h1></header>";
  30. cmd += "<head>";
  31. cmd += "<meta http-equiv='refresh' content='3'/>";
  32. cmd += "</head>";
  33.  
  34. if(device1){
  35. cmd +=("<br/>Device1 : ON");
  36. }
  37. else{
  38. cmd +=("<br/>Device1 : OFF");
  39. }
  40.  
  41. if(device2){
  42. cmd +=("<br/>Device2 : ON");
  43. }
  44. else{
  45. cmd +=("<br/>Device2 : OFF");
  46. }
  47.  
  48. if(device3){
  49. cmd +=("<br/>Device3 : ON");
  50. }
  51. else{
  52. cmd +=("<br/>Device3 : OFF");
  53. }
  54.  
  55. if(device4){
  56. cmd +=("<br/>Device4 : ON");
  57. }
  58. else{
  59. cmd +=("<br/>Device4 : OFF");
  60. }
  61.  
  62. cmd += "<html>rn";
  63. server.send(200, "text/html", cmd);
  64. }
  65.  
  66. void handleNotFound(){
  67. //digitalWrite(led, 1);
  68. String message = "File Not Foundnn";
  69. message += "URI: ";
  70. message += server.uri();
  71. message += "nMethod: ";
  72. message += (server.method() == HTTP_GET)?"GET":"POST";
  73. message += "nArguments: ";
  74. message += server.args();
  75. message += "n";
  76. for (uint8_t i=0; i<server.args(); i++){
  77. message += " " + server.argName(i) + ": " + server.arg(i) + "n";
  78. }
  79. server.send(404, "text/plain", message);
  80. //digitalWrite(led, 0);
  81. }
  82.  
  83. void setup(void){
  84. pinMode(output1, OUTPUT);
  85. pinMode(output2, OUTPUT);
  86. pinMode(output3, OUTPUT);
  87. pinMode(output4, OUTPUT);
  88.  
  89. digitalWrite(output1, LOW);
  90. digitalWrite(output2, LOW);
  91. digitalWrite(output3, LOW);
  92. digitalWrite(output4, LOW);
  93.  
  94. Serial.begin(115200);
  95. WiFi.begin(ssid, password);
  96. Serial.println("");
  97.  
  98.  
  99.  
  100. // Wait for connection
  101. while (WiFi.status() != WL_CONNECTED) {
  102. delay(500);
  103. Serial.print(".");
  104.  
  105. }
  106. Serial.println("");
  107. Serial.print("Connected to ");
  108. Serial.println(ssid);
  109. Serial.print("IP address: ");
  110. Serial.println(WiFi.localIP());
  111.  
  112.  
  113. if (MDNS.begin("esp8266")) {
  114. Serial.println("MDNS responder started");
  115. }
  116.  
  117. server.on("/", handleRoot);
  118.  
  119. server.on("/status1=1", [](){
  120. server.send(200, "text/plain", "device1 = ON");
  121. digitalWrite(output1, HIGH);
  122. device1 = true;
  123. });
  124.  
  125. server.on("/status1=0", [](){
  126. server.send(200, "text/plain", "device1 = OFF");
  127. digitalWrite(output1, LOW);
  128. device1 = false;
  129. });
  130.  
  131. server.on("/status2=1", [](){
  132. server.send(200, "text/plain", "device2 = ON");
  133. digitalWrite(output2, HIGH);
  134. device2 = true;
  135. });
  136.  
  137. server.on("/status2=0", [](){
  138. server.send(200, "text/plain", "device2 = OFF");
  139. digitalWrite(output2, LOW);
  140. device2 = false;
  141. });
  142.  
  143. server.on("/status3=1", [](){
  144. server.send(200, "text/plain", "device3 = ON");
  145. digitalWrite(output3, HIGH);
  146. device3 = true;
  147. });
  148.  
  149. server.on("/status3=0", [](){
  150. server.send(200, "text/plain", "device3 = OFF");
  151. digitalWrite(output3, LOW);
  152. device3 = false;
  153. });
  154.  
  155. server.on("/status4=1", [](){
  156. server.send(200, "text/plain", "device4 = ON");
  157. digitalWrite(output4, HIGH);
  158. device4 = true;
  159. });
  160.  
  161. server.on("/status4=0", [](){
  162. server.send(200, "text/plain", "device4 = OFF");
  163. digitalWrite(output4, LOW);
  164. device4 = false;
  165. });
  166.  
  167. server.onNotFound(handleNotFound);
  168. server.begin();
  169. Serial.println("HTTP server started");
  170. }
  171.  
  172. void loop(void){
  173. server.handleClient();
  174. }
  175.  
  176. #include <ESP8266WiFi.h>
  177. #include <ESP8266mDNS.h>
  178. #include <ArduinoOTA.h>
  179. #include <ESP8266WebServer.h>
  180.  
  181. const char* ssid = "........";
  182. const char* password = "........";
  183.  
  184. ESP8266WebServer server(80);
  185.  
  186. const char* www_username = "admin";
  187. const char* www_password = "esp8266";
  188.  
  189. void setup() {
  190. Serial.begin(115200);
  191. WiFi.mode(WIFI_STA);
  192. WiFi.begin(ssid, password);
  193. if(WiFi.waitForConnectResult() != WL_CONNECTED) {
  194. Serial.println("WiFi Connect Failed! Rebooting...");
  195. delay(1000);
  196. ESP.restart();
  197. }
  198. ArduinoOTA.begin();
  199.  
  200. server.on("/", [](){
  201. if(!server.authenticate(www_username, www_password))
  202. return server.requestAuthentication();
  203. server.send(200, "text/plain", "Login OK");
  204. });
  205. server.begin();
  206.  
  207. Serial.print("Open http://");
  208. Serial.print(WiFi.localIP());
  209. Serial.println("/ in your browser to see it working");
  210. }
  211.  
  212. void loop() {
  213. ArduinoOTA.handle();
  214. server.handleClient();
  215. }
  216.  
  217. if(!server.authenticate(www_username, www_password))
  218. return server.requestAuthentication();
Add Comment
Please, Sign In to add comment