ademosh

node3-20

May 3rd, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <ESP8266mDNS.h>
  4. #include <WiFiClient.h>
  5. #include<SoftwareSerial.h>
  6. #include <stdlib.h>
  7. SoftwareSerial s(3,1);
  8.  
  9.  
  10.  
  11. /* Put your SSID & Password */
  12. const char* ssid = "Keenetic-9068"; // Enter SSID here
  13. const char* password = ""; //Enter Password here
  14.  
  15. /* Put IP Address details */
  16. IPAddress local_ip(192,168,1,1);
  17. IPAddress gateway(192,168,1,1);
  18. IPAddress subnet(255,255,255,0);
  19.  
  20. ESP8266WebServer server(80);
  21.  
  22. const int led = LED_BUILTIN;
  23.  
  24. const String postForms = "<html>\
  25. <head>\
  26. <title>Controller Settings</title>\
  27. <style>\
  28. body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
  29. </style>\
  30. </head>\
  31. <body>\
  32. <h1>POST form data to /postform/</h1><br>\
  33. <form action=\"/mode_one/\" target=\"_blank\">\
  34. <button>First mode<button>\
  35. </form>\
  36. </body>\
  37. </html>";
  38.  
  39. const String modeOne= "<html>\
  40. <head>\
  41. <title> Mode 1 Settings </title>\
  42. <style>\
  43. body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
  44. </style>\
  45. </head>\
  46. <body>\
  47. <h1>Mode 1</h1><br>\
  48. <form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/postform/\">\
  49. <div>\Mode code <input type=\"text\" name=\"mode\" value=\"100\"></div>\
  50. <div>\ Red \<input type=\"text\" name=\"red\"> </div>\
  51. <div>\ Green \<input type=\"text\" name=\"green\"> </div>\
  52. <div>\ Blue \<input type=\"text\" name=\"blue\"> </div>\
  53. <input type=\"submit\" value=\"Submit\">\
  54. </form>\
  55. </body>\
  56. </html>";
  57.  
  58. void handleRoot() {
  59. server.send(200, "text/html", postForms);
  60. }
  61.  
  62. void handleForm() {
  63. if (server.method() != HTTP_POST) {
  64. digitalWrite(led, 1);
  65. server.send(405, "text/plain", "Method Not Allowed");
  66. digitalWrite(led, 0);
  67. } else {
  68. digitalWrite(led, 1);
  69. String message;
  70. for (uint8_t i = 0; i < server.args()-1; i++) {
  71. message+=server.arg(i);
  72. Serial.print(atoi(server.arg(i).c_str()),DEC);
  73. }
  74. server.send(200, "text/plain", message);
  75. }
  76. }
  77.  
  78. void handleModeOne() {
  79. server.send(200, "text/html", modeOne);
  80. }
  81.  
  82. void setup(void) {
  83. pinMode(led, OUTPUT);
  84. Serial.begin(9600);
  85. WiFi.begin(ssid, password);
  86. Serial.println("");
  87.  
  88. // Wait for connection
  89. while (WiFi.status() != WL_CONNECTED) {
  90. delay(500);
  91. Serial.print(".");
  92. }
  93. Serial.println("");
  94. Serial.print("Connected to ");
  95. Serial.println(ssid);
  96. Serial.print("IP address: ");
  97. Serial.println(WiFi.localIP());
  98.  
  99. if (MDNS.begin("esp8266")) {
  100. Serial.println("MDNS responder started");
  101. }
  102.  
  103. server.on("/", handleRoot);
  104.  
  105. server.on("/postform/", handleForm);
  106. server.on("/mode_one/", handleModeOne);
  107. server.on("/mode_one/postform", handleForm);
  108.  
  109.  
  110. server.begin();
  111. Serial.println("HTTP server started");
  112. }
  113.  
  114. void loop(void) {
  115. server.handleClient();
  116. }
Add Comment
Please, Sign In to add comment