Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MAXIMUM_DATA 4
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- const char *ssid = "Mobile_Mesh3";
- const char *password = "";
- int stateLED = LOW;
- WiFiClient client;
- ESP8266WebServer server(80);
- String Argument_Name, Message1;
- String Clients_Response1[MAXIMUM_DATA];
- String Clients_Response2[MAXIMUM_DATA];
- int dataCounting = 0;
- boolean isMax = false;
- String Message2 = "";
- String Message3 = "\n";
- const String HtmlScript = "<script type=\"text/javascript\"> var conversation = \"\";"
- "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; }"
- "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>";
- void HandleClient() {
- String webpage;
- webpage = "<html>";
- webpage += "<meta charset='utf-8' />";
- webpage += "<head><title>ESP8266 Input Example</title>";
- webpage += "<style>";
- webpage += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
- webpage += "div.box { background-color: black; color: white; margin: 20px 0 20px 0; padding: 20px;}";
- webpage += "</style>";
- //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 = ''; }";
- //webpage+= "</script>";
- webpage += "</head>";
- webpage += "<body>";
- String IPaddress = WiFi.localIP().toString();
- webpage += "<form action='http://192.168.4.1' method='POST'>";
- webpage += "<div class = 'box' <p>This screen for see the messages</p> <p1 name = 'nameUser'> </p1> </div>";
- webpage += "Type you name <input type = 'text' name = 'nameInput' id ='nameInput' /><br>";
- webpage += "Messages : <input type='text' name='userInput' id='userInput'> <input type='submit' value='Submit' onclick='addMessages()'>";
- webpage += "</form>";
- webpage += "</body>";
- webpage += "</html>";
- server.send(200, "text/html", webpage); // Send a response to the client asking for input
- if (server.args() > 0 ) { // Arguments were received
- for ( uint8_t i = 0; i < server.args(); i++ ) {
- Serial.print(server.argName(i)); // Display the argument
- Argument_Name = server.argName(i);
- if (server.argName(i) == "nameInput") {
- Serial.print(" Username : ");
- Serial.println(server.arg(i));
- Clients_Response1[dataCounting % MAXIMUM_DATA] = server.arg(i);
- // Clients_Response1 = server.arg(i);
- }
- if (server.argName(i) == "userInput") {
- Serial.print(" Input received was: ");
- Serial.println(server.arg(i));
- Clients_Response2[dataCounting % MAXIMUM_DATA] = server.arg(i);
- // Clients_Response2 = server.arg(i);
- }
- }
- dataCounting++;
- if(dataCounting >= MAXIMUM_DATA) {
- isMax = true;
- }
- }
- // Message1 = Clients_Response1 + Clients_Response2;
- //Message2 += Message1;
- //Message2 += Message3;
- }
- void ShowClientResponse() {
- String webpage;
- int loopMax;
- webpage = "<html>";
- webpage += "<head><meta charset='utf-8'><title>ESP8266 Input Example</title>";
- webpage += "<style>";
- webpage += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
- webpage += "div.box { background-color: black; color: white; margin: 20px 0 20px 0; padding: 20px;}";
- webpage += "</style>";
- 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 = ''; }";
- webpage+= "</script>";
- webpage += "</head>";
- webpage += "<body>";
- webpage += "<form action='http://192.168.4.1' method='GET'>";
- webpage += "<h1><br>ESP8266 Server - This was what the client sent</h1>";
- webpage += "<div class = 'box' <p>This screen for see the messages</p> <p1 name = 'nameUser'> </p1> </div>";
- // webpage += "<p>Username : " + Clients_Response1 + "</p>";
- // webpage += "<p>Message: " + Clients_Response2 + "</p>";
- if(isMax == true) {
- loopMax = MAXIMUM_DATA;
- } else {
- loopMax = dataCounting - 1;
- }
- for(int i = 0;i<=loopMax;i++) {
- webpage += "<p>Username : " + Clients_Response1[i] + "</p>";
- webpage += "<p>Message: " + Clients_Response2[i] + "</p>";
- }
- // webpage += "<p> "+ Message1 +"</p>";
- //webpage += "<p> "+ Message2 +"</p>";
- webpage += "</form>";
- webpage += "</body>";
- webpage += "</html>";
- server.send(200, "text/html", webpage); // Send a response to the client asking for input
- }
- void setup() {
- Serial.begin(115200);
- //Access point to wifi
- WiFi.softAP(ssid, password);
- IPAddress apip = WiFi.softAPIP();
- Serial.print("AP IP address: \n");
- Serial.println(apip);
- server.begin();
- Serial.println("HTTP server beginned");
- server.on("/", HandleClient); // The client connected with no arguments e.g. http:192.168.4.1/
- server.on("/result", ShowClientResponse); //The server respone when user want to sent data
- // server.on("/data", ShowClientResponse); //The server respone when user want to sent data
- }
- void loop() {
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement