HTML Text Box to Send Text to Arduino Web Server

An Arduino and Ethernet shield are used as a web server to host a web page that contains a text box. Text can be typed into the web page text box using a web browser and sent to the Arduino by clicking a button on the web page.

An HTML textarea is used in an HTML form to create the text box. JavaScript is used to send the text to the Arduino using a HTTP GET request when the button on the web page is clicked. This is useful for any Arduino project that needs to receive text from a web page using a text box.

The Arduino code for this project follows the format of the Ajax I/O web server from the Arduino Ethernet shield web server tutorial, except that it calls the JavaScript function that sends the GET request when the button on the web page is clicked rather than periodically sending the GET request to the Arduino web server.

The video below shows the Arduino web page being accessed by a web browser and text being sent to the Arduino.


Arduino Hardware, Software and HTML Page Setup


An Arduino Uno and Arduino Ethernet shield with 2GB micro SD card were used to test the project. Most Arduino boards that are compatible with the Ethernet shield should work.


Copy the HTML from below to a file called index.htm on the micro SD card and insert it into the Ethernet shield micro SD card socket. Load the Arduino sketch from below to the Arduino — first change the MAC and IP address in the sketch to suit your own network. In the Arduino IDE Serial Monitor window, set the baud rate at the bottom of the window to 115200.

Running the Project

With the Arduino connected to the Ethernet network, first open the Serial Monitor window, then open a web browser and surf to the IP address set in the sketch. Text can be typed into the text box on the web page and sent to the Arduino. The Arduino will display the text in the Serial Monitor window if the line of text is not too long (the length is set by buffer arrays in the Arduino code).

Arduino Text Box Sketch

The Arduino text box sketch listing called text_area is shown below. Copy and paste it to the Arduino IDE.

  Program:      text_area

  Description:  Arduino web server that gets text from an HTML
                textarea text box on the hosted web page.
                The web page is stored on the micro SD card.
  Date:         23 June 2015
  Author:       W.A. Smith,

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ   90
// size of buffer that stores the incoming string
#define TXT_BUF_SZ   50

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 20);   // IP address, may need to change depending on network
EthernetServer server(80);       // create a server at port 80
File webFile;                    // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0;              // index into HTTP_req buffer
char txt_buf[TXT_BUF_SZ] = {0};  // buffer to save text to

void setup()
    // disable Ethernet chip
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);
    Serial.begin(115200);       // for debugging
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    Serial.println("SUCCESS - Found index.htm file.");
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients

void loop()
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c =; // read 1 byte (character) from client
                // limit the size of the stored received HTTP request
                // buffer first part of HTTP request in HTTP_req array (string)
                // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
                if (req_index < (REQ_BUF_SZ - 1)) {
                    HTTP_req[req_index] = c;          // save HTTP request character
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    // remainder of header follows below, depending on if
                    // web page or XML page is requested
                    // Ajax request - send XML file
                    if (StrContains(HTTP_req, "ajax_inputs")) {
                        // send rest of HTTP header
                        client.println("Content-Type: text/xml");
                        client.println("Connection: keep-alive");

                        // print the received text to the Serial Monitor window
                        // if received with the incoming HTTP GET string
                        if (GetText(txt_buf, TXT_BUF_SZ)) {
                          Serial.println("\r\nReceived Text:");
                    else {  // web page request
                        // send rest of HTTP header
                        client.println("Content-Type: text/html");
                        client.println("Connection: keep-alive");
                        // send web page
                        webFile ="index.htm");        // open web page file
                        if (webFile) {
                            while(webFile.available()) {
                                client.write(; // send web page to client
                    // reset buffer index and all buffer elements to 0
                    req_index = 0;
                    StrClear(HTTP_req, REQ_BUF_SZ);
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)

// extract text from the incoming HTTP GET data string
// returns true only if text was received
// the string must start with "&txt=" and end with "&end"
// if the string is too long for the HTTP_req buffer and
// "&end" is cut off, then the function returns false
boolean GetText(char *txt, int len)
  boolean got_text = false;    // text received flag
  char *str_begin;             // pointer to start of text
  char *str_end;               // pointer to end of text
  int str_len = 0;
  int txt_index = 0;
  // get pointer to the beginning of the text
  str_begin = strstr(HTTP_req, "&txt=");
  if (str_begin != NULL) {
    str_begin = strstr(str_begin, "=");  // skip to the =
    str_begin += 1;                      // skip over the =
    str_end = strstr(str_begin, "&end");
    if (str_end != NULL) {
      str_end[0] = 0;  // terminate the string
      str_len = strlen(str_begin);

      // copy the string to the txt buffer and replace %20 with space ' '
      for (int i = 0; i < str_len; i++) {
        if (str_begin[i] != '%') {
          if (str_begin[i] == 0) {
            // end of string
          else {
            txt[txt_index++] = str_begin[i];
            if (txt_index >= (len - 1)) {
              // keep the output string within bounds
        else {
          // replace %20 with a space
          if ((str_begin[i + 1] == '2') && (str_begin[i + 2] == '0')) {
            txt[txt_index++] = ' ';
            i += 2;
            if (txt_index >= (len - 1)) {
              // keep the output string within bounds
      // terminate the string
      txt[txt_index] = 0;
      got_text = true;

  return got_text;

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
    for (int i = 0; i < length; i++) {
        str[i] = 0;

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
    char found = 0;
    char index = 0;
    char len;

    len = strlen(str);
    if (strlen(sfind) > len) {
        return 0;
    while (index < len) {
        if (str[index] == sfind[found]) {
            if (strlen(sfind) == found) {
                return 1;
        else {
            found = 0;

    return 0;

Text Box HTML Page

Copy the HTML below and save it to a file called index.htm on the SD card.

<!DOCTYPE html>
<html lang="en">
        <meta charset="utf-8">
        <title>Text Box using textarea</title>

        strText = "";
        function SendText()
            nocache = "&nocache=" + Math.random() * 1000000;
            var request = new XMLHttpRequest();
            strText = "&txt=" + document.getElementById("txt_form").form_text.value + "&end=end";
  "GET", "ajax_inputs" + strText + nocache, true);

    <body onload="GetArduinoIO()">
        <form id="txt_form" name="frmText">
            <textarea name="form_text" rows="10" cols="40"></textarea>
        <input type="submit" value="Send Text" onclick="SendText()" />


How the Text Box Sketch Works

JavaScript embedded in the web page sends the text from the text box to the Arduino as part of a HTTP GET request when the button on the web page is clicked.

Format of the String

This image shows what the string looks like before it is sent to the Arduino.

HTML Text Box and Text String

HTML Text Box and Text String

The text from the text box is put between &txt= and &end=end before being sent. The Arduino uses this text to find the string in the incoming HTTP GET request.

When the text reaches the Arduino, it has been altered with the spaces in the text changed to %20 as shown in the image below.

String from the Text Box Received by Arduino

String from the Text Box Received by Arduino

The Arduino sketch must change %20 in the text back to spaces.

Processing the String in the Arduino Sketch

The function GetText() is used to get the text box string from the incoming HTTP GET request. The HTTP_req array holds the beginning of the incoming GET request as shown in the above image — starting with GET /ajax_inputs&txt…

The GetText() function first gets a pointer to the beginning of the text box string by searching for &txt= and then terminating the string when it finds &end. A for loop is used to copy the string to the txt array (which is a pointer to the global txt_buf array). While the copying is taking place, the code looks for spaces that are encoded as %20 and changes them into spaces.

Limitations of the Arduino Text Box Sketch

The sketch is limited by the size of the buffer that saves the incoming HTTP GET request as well as the size of the array that stores the string from the text box. These two array buffers have been kept small so that the code will be able to run on an Arduino Uno.

If the string from the text box is too big for the HTTP buffer on the Arduino so that the string is truncated before the terminating &end, then the text will not be displayed in the Serial Monitor window because the GetText() function will return false.

This is just a simple demonstration that uses GET to send text. It would be better to send the text using POST instead and this will be added as a tutorial at a later stage.


32 thoughts on “HTML Text Box to Send Text to Arduino Web Server

      • Do you have any tutorial for this ? I can’t seem to get it working and to mount from SD card. What I’m trying to do is send a string such as (12:11:10) to activate an alarm clock. But I can;t get it working. Please some help.

        – jean

        • I don’t have a Yun, so can’t develop the code for it. When I get enough donations I will buy a Yun because I have been asked about code on a Yun several times before.

  1. Great tutorial, i implemented it to input text into txt files in my SD card.
    A little question though, how do i clear the text box after hitting send?

  2. Hi, If i wanted to place a function to run a stepper motor based of a input into the textarea, where would i call the function? I tried calling it in the
    if (GetText(txt_buf, TXT_BUF_SZ)) statment but once ou enter a specific text it doesn’t anymore. This is currently what I have added.

    if(strcmp(txt_buf, “Parcel Position 1”)==0) {

    As stated before once I add this it wont save any received text from the form. Any help or advice would be much appreciated

  3. Hi, and many compliments for your fully detailed and well documented tutorials, just one question on this one, what extactly is doing, in the HTM code, the ? I know that the onload is calling a function on the body load but I can’t see te GetArduinoIO function. Can you explain me plese? 🙂 Thanks!

  4. Hi, nice tutorial. but I am getting an error like this:
    warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    if (StrContains(HTTP_req, “ajax_inputs”)) {

    Any idea why its arising?

    • Hi Aseef. The problem is occurring because constant strings are passed to StrContains(), but string arguments to StrContains() are non const. The solution is to change the StrContains() function at the bottom of the code from:
      char StrContains(char *str, char *sfind)
      char StrContains(const char *str, const char *sfind)
      i.e. make the string arguments const.

      The compiler used with older versions of the Arduino IDE never issued a warning. Only the newer Arduino software does because it uses a newer compiler.

  5. Hello! I’m super happy to get this working. First try, too. I have a question about increasing the length of the string. Is there a way to do this? I have a MEGA, so I would like to max out the string length. Thanks!
    (I can get up to 64 characters, but no more)

  6. Hi After I follow your examples I immediatly bougth one ethernet sheild to my arduino.
    Now I am testing your examples one by one and trying to learn some more.
    I wonder is it possible use same text box at first time to read a variable from arduino to see and after that write a new value to same box and press submit buton to change value in arduino.
    Maybe this is so simple for you but I am very new at arduino and HTML things.

    I thank you for your complete deatiled tutorials .

    • It would require some changes. I will be adding a ESP8266 Arduino web server tutorial at some stage…

        • If you convert all of the communications from using the Ethernet shield to using the ESP8266, it should work.

  7. hy, .. how to make a command in arduino to send data and data appear in textarea web based php. Anyone knows?
    Please help me 🙁

    • You write something like this code in else block of if(c == ‘\n’).

      // client.println(“strLine1 = \”/\”/ function SendText(){nocache = ‘&nocache=’ + Math.random() * 1000000;var request = new XMLHttpRequest();strLine1 = ‘&L1=’ + document.getElementById(‘txt_form’).line1.value;‘GET’, ‘ajax_inputs’ + strLine1 + nocache, true);request.send(null);}”);
      client.println(“strLine1 = \”/\”/ function SendText(){nocache = ‘&nocache=’ + Math.random() * 1000000;var request = new XMLHttpRequest();strLine1 = ‘&L1=’ + document.getElementById(‘txt_form’).line1.value;‘GET’, ‘ajax_inputs’ + strLine1 + nocache, true);request.send(null);}”);
      client.println(“Enter text to send to Arduino LCD:“);
      client.println(“”); //uses IP/port of web page
      client.println(“Enter any name:”);

      It is working for me.

  8. hi, great video. will i be able to send text to the arduino anywhere i am standing ? or there is a limit range where i need to be placed. thanks

  9. I have number of files saved in folder, I want to browse and select text file whichever I want to send. In this regard what need to do.

  10. Hi.
    Nice tutorial.
    I have used this concept.
    I have changed the web server to WiFiESP8266 (so I could increase the size of the textarea buffers) and modified the sketch so that it can deal with UTF-8 characters (so the user can send the spanish, greek, russian etc. characters and display them in Serial Monitor)
    The only thing, that bothers me is that I can not send the linebreaks.
    The linebreaks are gone already on arrival to the webserver.

  11. how could I send text message from browser to ethernet which should display on the serial port of Arduino Uno .It will be great help if u give any suggestion or advice

Leave a Reply

Your email address will not be published. Required fields are marked *