Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4.  
  5. const char *ssid = "Mobile_Mesh3";
  6. const char *password = "";
  7. int stateLED = LOW;
  8.  
  9. WiFiClient client;
  10. ESP8266WebServer server(80);
  11.  
  12. String Argument_Name, Clients_Response1, Clients_Response2, Message1;
  13. String Message2 ="";
  14. String Message3 ="\n";
  15.  
  16. const String HtmlScript = "<script type=\"text/javascript\"> var conversation = \"\";"
  17. "function addMessages(){ var input = document.getElementById(\"userInput\").value; if (input.length == 0) { alert(\"Please enter a messages\"); return; }var name = document.getElementById(\"nameInput\").value; if (name.length == 0) { alert(\"Please enter a name\"); return; }"
  18. "conversation = name + \" :\" + input; var content = document.createTextNode(conversation); var theDiv = document.getElementById(\"nameUser\"); theDiv.appendChild(content); theDiv.appendChild(document.createElement(\"br\")); document.getElementById(\"userInput\").value = \"\"; }</script></head>";
  19.  
  20.  
  21. void HandleClient() {
  22. String webpage;
  23. webpage = "<html>";
  24. webpage += "<meta charset='utf-8' />";
  25. webpage += "<head><title>ESP8266 Input Example</title>";
  26. webpage += "<style>";
  27. webpage += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  28. webpage += "div.box { background-color: black; color: white; margin: 20px 0 20px 0; padding: 20px;}";
  29. webpage += "</style>";
  30. //webpage += "<script type ='text/javascrpit'> var conversation = ''; function addMessages(){ var input = document.getElementByName('userInput').value; if (input.length === 0) { alert('Please enter a messages'); return; } var name = document.getElementByName('nameInput').value; if (name.length === 0) { alert('Please enter a name'); return; } conversation = name + ' : ' + input; var content = document.createTextNode(conversation); var theDiv = document.getElementByName('nameUser'); theDiv.appendChild(content); theDiv.appendChild(document.createElement('br')); document.getElementByName('userInput').value = ''; }";
  31. //webpage+= "</script>";
  32. webpage += "</head>";
  33. webpage += "<body>";
  34. String IPaddress = WiFi.localIP().toString();
  35. webpage += "<form action='http://192.168.4.1' method='POST'>";
  36. webpage += "<div class = 'box' <p>This screen for see the messages</p> <p1 name = 'nameUser'> </p1> </div>";
  37. webpage += "Type you name <input type = 'text' name = 'nameInput' id ='nameInput' /><br>";
  38. webpage += "Messages : <input type='text' name='userInput' id='userInput'>&nbsp; <input type='submit' value='Submit' onclick='addMessages()'>";
  39. webpage += "</form>";
  40. webpage += "</body>";
  41. webpage += "</html>";
  42.  
  43. server.send(200, "text/html", webpage); // Send a response to the client asking for input
  44.  
  45.  
  46. if (server.args() > 0 ) { // Arguments were received
  47. for ( uint8_t i = 0; i < server.args(); i++ ) {
  48. Serial.print(server.argName(i)); // Display the argument
  49. Argument_Name = server.argName(i);
  50. if (server.argName(i) == "nameInput") {
  51. Serial.print(" Username : ");
  52. Serial.println(server.arg(i));
  53. Clients_Response1 = server.arg(i);
  54.  
  55. }
  56. if (server.argName(i) == "userInput") {
  57. Serial.print(" Input received was: ");
  58. Serial.println(server.arg(i));
  59. Clients_Response2 = server.arg(i);
  60. }
  61. }
  62. }
  63.  
  64. Message1 = Clients_Response1 + Clients_Response2;
  65. //Message2 += Message1;
  66. //Message2 += Message3;
  67. }
  68.  
  69. void ShowClientResponse() {
  70. String webpage;
  71. webpage = "<html>";
  72. webpage += "<head><meta charset='utf-8'><title>ESP8266 Input Example</title>";
  73. webpage += "<style>";
  74. webpage += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  75. webpage += "div.box { background-color: black; color: white; margin: 20px 0 20px 0; padding: 20px;}";
  76. webpage += "</style>";
  77. webpage += "<script type ='text/javascrpit'> var conversation = ''; function addMessages(){ var input = document.getElementByName('userInput').value; if (input.length === 0) { alert('Please enter a messages'); return; } var name = document.getElementByName('nameInput').value; if (name.length === 0) { alert('Please enter a name'); return; } conversation = name + ' : ' + input; var content = document.createTextNode(conversation); var theDiv = document.getElementByName('nameUser'); theDiv.appendChild(content); theDiv.appendChild(document.createElement('br')); document.getElementByName('userInput').value = ''; }";
  78. webpage+= "</script>";
  79. webpage += "</head>";
  80. webpage += "<body>";
  81. webpage += "<form action='http://192.168.4.1' method='GET'>";
  82. webpage += "<h1><br>ESP8266 Server - This was what the client sent</h1>";
  83. webpage += "<div class = 'box' <p>This screen for see the messages</p> <p1 name = 'nameUser'> </p1> </div>";
  84. webpage += "<p>Username : " + Clients_Response1 + "</p>";
  85. webpage += "<p>Message: " + Clients_Response2 + "</p>";
  86. webpage += "<p> "+ Message1 +"</p>";
  87. //webpage += "<p> "+ Message2 +"</p>";
  88. webpage += "</form>";
  89. webpage += "</body>";
  90. webpage += "</html>";
  91.  
  92. server.send(200, "text/html", webpage); // Send a response to the client asking for input
  93. }
  94.  
  95. void setup() {
  96. Serial.begin(115200);
  97.  
  98. //Access point to wifi
  99. WiFi.softAP(ssid, password);
  100. IPAddress apip = WiFi.softAPIP();
  101. Serial.print("AP IP address: \n");
  102. Serial.println(apip);
  103. server.begin();
  104. Serial.println("HTTP server beginned");
  105. server.on("/", HandleClient); // The client connected with no arguments e.g. http:192.168.4.1/
  106. server.on("/result", ShowClientResponse); //The server respone when user want to sent data
  107. }
  108.  
  109. void loop() {
  110. server.handleClient();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement