Advertisement
Guest User

Untitled

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