Advertisement
Guest User

esp8266 webserver

a guest
Mar 23rd, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.35 KB | None | 0 0
  1. // Load Wi-Fi library
  2. #include <ESP8266WiFi.h>
  3.  
  4. // NETWORK: Static IP details...
  5. IPAddress ip(10, 0, 0, 30);
  6. IPAddress gateway(10, 0, 0, 1);
  7. IPAddress subnet(255, 255, 255, 0);
  8.  
  9. // Replace with your network credentials
  10. const char* ssid = "";
  11. const char* password = "";
  12.  
  13. // Set web server port number to 80
  14. WiFiServer server(80);
  15.  
  16. // Variable to store the HTTP request
  17. String header;
  18.  
  19. // Auxiliar variables to store the current output state
  20. String output2State = "off";
  21. String output4State = "off";
  22. String output5State = "off";
  23. String output12State = "off";
  24. String output13State = "off";
  25. String output14State = "off";
  26. String output15State = "off";
  27. String output16State = "off";
  28.  
  29. // Assign output variables to GPIO pins
  30. const int output5 = 2;
  31. const int output4 = 4;
  32. const int output2 = 5;
  33. const int output12 = 12;
  34. const int output13 = 13;
  35. const int output14 = 14;
  36. const int output15 = 15;
  37. const int output16 = 16;
  38.  
  39. void setup() {
  40. Serial.begin(115200);
  41. // Initialize the output variables as outputs
  42. pinMode(output2, OUTPUT);
  43. pinMode(output4, OUTPUT);
  44. pinMode(output5, OUTPUT);
  45. pinMode(output12, OUTPUT);
  46. pinMode(output13, OUTPUT);
  47. pinMode(output14, OUTPUT);
  48. pinMode(output15, OUTPUT);
  49. pinMode(output16, OUTPUT);
  50.  
  51. // Set outputs to LOW
  52. digitalWrite(output2, LOW);
  53. digitalWrite(output4, LOW);
  54. digitalWrite(output5, LOW);
  55. digitalWrite(output12, LOW);
  56. digitalWrite(output13, LOW);
  57. digitalWrite(output14, LOW);
  58. digitalWrite(output15, LOW);
  59. digitalWrite(output16, LOW);
  60.  
  61. // Connect to Wi-Fi network with SSID and password
  62. Serial.print("Connecting to ");
  63. Serial.println(ssid);
  64.  
  65. // Static IP Setup Info Here...
  66. WiFi.config(ip, gateway, subnet);
  67.  
  68. WiFi.begin(ssid, password);
  69. while (WiFi.status() != WL_CONNECTED) {
  70. delay(500);
  71. Serial.print(".");
  72. }
  73. // Print local IP address and start web server
  74. Serial.println("");
  75. Serial.println("WiFi connected.");
  76. Serial.println("IP address: ");
  77. Serial.println(WiFi.localIP());
  78. server.begin();
  79. }
  80.  
  81. void loop(){
  82. WiFiClient client = server.available(); // Listen for incoming clients
  83.  
  84. if (client) { // If a new client connects,
  85. Serial.println("New Client."); // print a message out in the serial port
  86. String currentLine = ""; // make a String to hold incoming data from the client
  87. while (client.connected()) { // loop while the client's connected
  88. if (client.available()) { // if there's bytes to read from the client,
  89. char c = client.read(); // read a byte, then
  90. Serial.write(c); // print it out the serial monitor
  91. header += c;
  92. if (c == '\n') { // if the byte is a newline character
  93. // if the current line is blank, you got two newline characters in a row.
  94. // that's the end of the client HTTP request, so send a response:
  95. if (currentLine.length() == 0) {
  96. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  97. // and a content-type so the client knows what's coming, then a blank line:
  98. client.println("HTTP/1.1 200 OK");
  99. client.println("Content-type:text/html");
  100. client.println("Connection: close");
  101. client.println();
  102.  
  103. // turns the GPIOs on and off
  104.  
  105. //GPIO 2
  106. if (header.indexOf("GET /2/on") >= 0) {
  107. Serial.println("GPIO 2 on");
  108. output2State = "on";
  109. digitalWrite(output2, HIGH);
  110. } else if (header.indexOf("GET /2/off") >= 0) {
  111. Serial.println("GPIO 2 off");
  112. output2State = "off";
  113. digitalWrite(output2, LOW);
  114.  
  115. //GPIO 4
  116. } else if (header.indexOf("GET /4/on") >= 0) {
  117. Serial.println("GPIO 4 on");
  118. output4State = "on";
  119. digitalWrite(output4, HIGH);
  120. } else if (header.indexOf("GET /4/off") >= 0) {
  121. Serial.println("GPIO 4 off");
  122. output4State = "off";
  123. digitalWrite(output4, LOW);
  124.  
  125. // GPIO 5
  126. } else if (header.indexOf("GET /5/on") >= 0) {
  127. Serial.println("GPIO 5 on");
  128. output5State = "on";
  129. digitalWrite(output5, HIGH);
  130. } else if (header.indexOf("GET /5/off") >= 0) {
  131. Serial.println("GPIO 5 off");
  132. output5State = "off";
  133. digitalWrite(output5, LOW);
  134.  
  135. // GPIO12
  136. } else if (header.indexOf("GET /12/on") >= 0) {
  137. Serial.println("GPIO 12 on");
  138. output12State = "on";
  139. digitalWrite(output12, HIGH);
  140. } else if (header.indexOf("GET /12/off") >= 0) {
  141. Serial.println("GPIO 12 off");
  142. output12State = "off";
  143. digitalWrite(output12, LOW);
  144.  
  145. //GPIO 13
  146. } else if (header.indexOf("GET /13/on") >= 0) {
  147. Serial.println("GPIO 13 on");
  148. output13State = "on";
  149. digitalWrite(output13, HIGH);
  150. } else if (header.indexOf("GET /13/off") >= 0) {
  151. Serial.println("GPIO 13 off");
  152. output13State = "off";
  153. digitalWrite(output13, LOW);
  154.  
  155. //GPIO 14
  156. } else if (header.indexOf("GET /14/on") >= 0) {
  157. Serial.println("GPIO 14 on");
  158. output14State = "on";
  159. digitalWrite(output14, HIGH);
  160. } else if (header.indexOf("GET /14/off") >= 0) {
  161. Serial.println("GPIO 14 off");
  162. output14State = "off";
  163. digitalWrite(output14, LOW);
  164.  
  165. //GPIO 15
  166. } else if (header.indexOf("GET /15/on") >= 0) {
  167. Serial.println("GPIO 15 on");
  168. output15State = "on";
  169. digitalWrite(output15, HIGH);
  170. } else if (header.indexOf("GET /15/off") >= 0) {
  171. Serial.println("GPIO 15 off");
  172. output15State = "off";
  173. digitalWrite(output15, LOW);
  174.  
  175. //GPIO 16
  176. } else if (header.indexOf("GET /16/on") >= 0) {
  177. Serial.println("GPIO 16 on");
  178. output16State = "on";
  179. digitalWrite(output16, HIGH);
  180. } else if (header.indexOf("GET /16/off") >= 0) {
  181. Serial.println("GPIO 16 off");
  182. output16State = "off";
  183. digitalWrite(output16, LOW);
  184. }
  185.  
  186. // Display the HTML web page
  187. client.println("<!DOCTYPE html><html>");
  188. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  189. client.println("<title>Elfnet Control Node #1</title>");
  190. client.println("<link rel=\"icon\" href=\"data:,\">");
  191. // CSS to style the on/off buttons
  192. // Feel free to change the background-color and font-size attributes to fit your preferences
  193. client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
  194. client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
  195. client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
  196. client.println(".button2 {background-color: #77878A;}</style></head>");
  197.  
  198. // Web Page Heading
  199. client.println("<body><h1>System Control Node #1</h1>");
  200.  
  201. // Display current state, and ON/OFF buttons for GPIO 2
  202. client.println("<p>Zone 1 - State: " + output2State + "</p>");
  203. // If the output5State is off, it displays the ON button
  204. if (output2State=="off") {
  205. client.println("<p><a href=\"/2/on\"><button class=\"button\">ON</button></a></p>");
  206. } else {
  207. client.println("<p><a href=\"/2/off\"><button class=\"button button2\">OFF</button></a></p>");
  208. }
  209.  
  210. // Display current state, and ON/OFF buttons for GPIO 4
  211. client.println("<p>Zone 2 - State: " + output4State + "</p>");
  212. // If the output4State is off, it displays the ON button
  213. if (output4State=="off") {
  214. client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
  215. } else {
  216. client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
  217. }
  218.  
  219. // Display current state, and ON/OFF buttons for GPIO 5
  220. client.println("<p>Zone 3 - State: " + output5State + "</p>");
  221. // If the output4State is off, it displays the ON button
  222. if (output5State=="off") {
  223. client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
  224. } else {
  225. client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
  226. }
  227.  
  228. // Display current state, and ON/OFF buttons for GPIO 12
  229. client.println("<p>Zone 4 - State: " + output12State + "</p>");
  230. // If the output4State is off, it displays the ON button
  231. if (output12State=="off") {
  232. client.println("<p><a href=\"/12/on\"><button class=\"button\">ON</button></a></p>");
  233. } else {
  234. client.println("<p><a href=\"/12/off\"><button class=\"button button2\">OFF</button></a></p>");
  235. }
  236.  
  237. // Display current state, and ON/OFF buttons for GPIO 13
  238. client.println("<p>Zone 5 - State: " + output13State + "</p>");
  239. // If the output4State is off, it displays the ON button
  240. if (output13State=="off") {
  241. client.println("<p><a href=\"/13/on\"><button class=\"button\">ON</button></a></p>");
  242. } else {
  243. client.println("<p><a href=\"/13/off\"><button class=\"button button2\">OFF</button></a></p>");
  244. }
  245.  
  246. // Display current state, and ON/OFF buttons for GPIO 14
  247. client.println("<p>Zone 6 - State: " + output14State + "</p>");
  248. // If the output4State is off, it displays the ON button
  249. if (output14State=="off") {
  250. client.println("<p><a href=\"/14/on\"><button class=\"button\">ON</button></a></p>");
  251. } else {
  252. client.println("<p><a href=\"/14/off\"><button class=\"button button2\">OFF</button></a></p>");
  253. }
  254.  
  255. client.println("</body></html>");
  256.  
  257. // The HTTP response ends with another blank line
  258. client.println();
  259. // Break out of the while loop
  260. break;
  261. } else { // if you got a newline, then clear currentLine
  262. currentLine = "";
  263. }
  264. } else if (c != '\r') { // if you got anything else but a carriage return character,
  265. currentLine += c; // add it to the end of the currentLine
  266. }
  267. }
  268. }
  269. // Clear the header variable
  270. header = "";
  271. // Close the connection
  272. client.stop();
  273. Serial.println("Client disconnected.");
  274. Serial.println("");
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement