Advertisement
aripon

NodeMCU ESP8266

Dec 15th, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char *ssid = "RIPON"; //ganti nama wifi
  4. const char *password = "1234567890101112";//ganti password
  5.  
  6. WiFiServer server(80);
  7.  
  8. // Variable to store the HTTP request
  9. String header;
  10.  
  11. // Auxiliar variables to store the current output state
  12. String output5State = "off";
  13. String output4State = "off";
  14.  
  15. // Assign output variables to GPIO pins
  16. const int output5 = 5;
  17. const int output4 = 4;
  18.  
  19. // Current time
  20. unsigned long currentTime = millis();
  21. // Previous time
  22. unsigned long previousTime = 0;
  23. // Define timeout time in milliseconds (example: 2000ms = 2s)
  24. const long timeoutTime = 2000;
  25.  
  26. void setup() {
  27. Serial.begin(9600);
  28. // Initialize the output variables as outputs
  29. pinMode(output5, OUTPUT);
  30. pinMode(output4, OUTPUT);
  31. // Set outputs to LOW
  32. digitalWrite(output5, LOW);
  33. digitalWrite(output4, LOW);
  34.  
  35. // Connect to Wi-Fi network with SSID and password
  36. Serial.print("Connecting to ");
  37. Serial.println(ssid);
  38. WiFi.begin(ssid, password);
  39. while (WiFi.status() != WL_CONNECTED) {
  40. delay(500);
  41. Serial.print(".");
  42. }
  43. // Print local IP address and start web server
  44. Serial.println("");
  45. Serial.println("WiFi connected.");
  46. Serial.println("IP address: ");
  47. Serial.println(WiFi.localIP());
  48. server.begin();
  49. }
  50.  
  51. void loop() {
  52. WiFiClient client = server.available(); // Listen for incoming clients
  53.  
  54. if (client) { // If a new client connects,
  55. Serial.println("New Client."); // print a message out in the serial port
  56. String currentLine = ""; // make a String to hold incoming data from the client
  57. currentTime = millis();
  58. previousTime = currentTime;
  59. while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
  60. currentTime = millis();
  61. if (client.available()) { // if there's bytes to read from the client,
  62. char c = client.read(); // read a byte, then
  63. Serial.write(c); // print it out the serial monitor
  64. header += c;
  65. if (c == '\n') { // if the byte is a newline character
  66. // if the current line is blank, you got two newline characters in a row.
  67. // that's the end of the client HTTP request, so send a response:
  68. if (currentLine.length() == 0) {
  69. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  70. // and a content-type so the client knows what's coming, then a blank line:
  71. client.println("HTTP/1.1 200 OK");
  72. client.println("Content-type:text/html");
  73. client.println("Connection: close");
  74. client.println();
  75.  
  76. // turns the GPIOs on and off
  77. if (header.indexOf("GET /5/on") >= 0) {
  78. Serial.println("GPIO 5 on");
  79. output5State = "on";
  80. digitalWrite(output5, HIGH);
  81. } else if (header.indexOf("GET /5/off") >= 0) {
  82. Serial.println("GPIO 5 off");
  83. output5State = "off";
  84. digitalWrite(output5, LOW);
  85. } else if (header.indexOf("GET /4/on") >= 0) {
  86. Serial.println("GPIO 4 on");
  87. output4State = "on";
  88. digitalWrite(output4, HIGH);
  89. } else if (header.indexOf("GET /4/off") >= 0) {
  90. Serial.println("GPIO 4 off");
  91. output4State = "off";
  92. digitalWrite(output4, LOW);
  93. }
  94.  
  95. // Display the HTML web page
  96. client.println("<!DOCTYPE html><html>");
  97. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  98. client.println("<link rel=\"icon\" href=\"data:,\">");
  99. // CSS to style the on/off buttons
  100. // Feel free to change the background-color and font-size attributes to fit your preferences
  101. client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
  102. client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
  103. client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
  104. client.println(".button2 {background-color: #77878A;}</style></head>");
  105.  
  106. // Web Page Heading
  107. client.println("<body><h1>ESP8266 Web Server</h1>");
  108.  
  109. // Display current state, and ON/OFF buttons for GPIO 5
  110. client.println("<p>GPIO 5 - State " + output5State + "</p>");
  111. // If the output5State is off, it displays the ON button
  112. if (output5State == "off") {
  113. client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
  114. } else {
  115. client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
  116. }
  117.  
  118. // Display current state, and ON/OFF buttons for GPIO 4
  119. client.println("<p>GPIO 4 - State " + output4State + "</p>");
  120. // If the output4State is off, it displays the ON button
  121. if (output4State == "off") {
  122. client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
  123. } else {
  124. client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
  125. }
  126. client.println("</body></html>");
  127.  
  128. // The HTTP response ends with another blank line
  129. client.println();
  130. // Break out of the while loop
  131. break;
  132. } else { // if you got a newline, then clear currentLine
  133. currentLine = "";
  134. }
  135. } else if (c != '\r') { // if you got anything else but a carriage return character,
  136. currentLine += c; // add it to the end of the currentLine
  137. }
  138. }
  139. }
  140. // Clear the header variable
  141. header = "";
  142. // Close the connection
  143. client.stop();
  144. Serial.println("Client disconnected.");
  145. Serial.println("");
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement