Transfer a number from Android device to NodeMCU, Arduino Program

Upload the following program to your NodeMCU.

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>

const char *APSSID = "ESP8266-WEB-SERVER";
const char *APPSK = "123456789";

AsyncWebServer server(80);

/******************************************************************/

const char MAIN_page[] PROGMEM = R"=====(

  <!DOCTYPE HTML>
  <html>
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>

      <table class="blueTable" id = "delayTable">
          <tbody>
            <tr>
              <td><input id ="delayValueTextBox" type="number" name="fname" ></td>
              <td><input type="button" class="buttonDelay" name="delayValue" id="setDelay" value="   Set On/Off Delay   " onclick="location.href='/setDelay?delayValue='+ document.getElementById('delayValueTextBox').value;">
              </td>
            </tr>
          </tbody>
      </table>
    </body>
  </html>

)=====";


const char delayValueHTML[] PROGMEM = R"rawliteral(
  <!DOCTYPE HTML>
  <html>
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
    
        <H2>
         Delay sent successfully 
       </H2>
    
       <H3>
          <a href = "/">Home</a>
       </H3>
    </body>
  </html>

)rawliteral";

/*******************************************************************/

void setup() {
  Serial.begin(115200);
  WiFi.softAP(APSSID, APPSK);
  char networkNames[] = "";

  IPAddress IP = WiFi.softAPIP();
  Serial.println(IP);

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    request -> send_P(200, "text/html", MAIN_page);
  });

  // Route for root / web page
  server.on("/setDelay", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", delayValueHTML);

    String strDelayValue = request -> arg("delayValue");
    int intDelayValue = strDelayValue.toInt();

    Serial.println(intDelayValue);
    
  });

  server.begin();
}

void loop() {

  delay(1000);

}

After successful uploading, open your arduino serial monitor. Connect your Android device to NodeMCU through WiFi. Open a web browser and navigate to 192.168.4.1 . Type a number in the text box and press the button. That number will be displayed in the arduino serial monitor.

0 0 vote
Article Rating

Published by

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
X