Advertisement
Guest User

Untitled

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