ESP8266 NodeMCU WebSerial: Web-based Remote Serial Monitor

In this guide, you’ll learn how to create and use a web-based “Serial Monitor” for your ESP8266 NodeMCU projects using the WebSerial library. This creates a web-based interface to output debugging messages, as you would do with a regular serial monitor. You can also send messages from the web-based serial monitor to the ESP8266.

ESP8266 NodeMCU WebSerial Web-based Remote Serial Monitor Arduino Core IDE

We have a similar tutorial for the ESP32 board: ESP32 WebSerial Web-based Remote Serial Monitor

Web-based Serial Monitor

In most of your ESP8266 projects, you use the serial monitor to output debugging messages that help better understand what’s happening with the microcontroller.

You create a Serial communication between your board and your computer, and then you can visualize the messages using the serial monitor. However, when your board is not connected to your computer, you can’t see the debugging messages.

A workaround for this issue is to use a web-based serial monitor—the ESP8266 hosts a web server that serves a page to visualize the messages as you would with the “regular” serial monitor. The WebSerial web page also allows you to send data from the web page to your board.

WebSerial Library ESP32 ESP8266 web-based Serial Monitor

For this tutorial, we’ll use the WebSerial library.

If you like this library and you’ll use it in your projects, consider supporting the developer’s work.

WebSerial Features

List of WebSerial features:

  • Works on WebSockets;
  • Realtime logging;
  • Any number of serial monitors can be opened on the browser;
  • Uses AsyncWebserver for better performance.

WebSerial Functions

Using WebSerial is similar to use the serial monitor. Its main functions are print() and println():

  • print(): prints the data on the web-based serial monitor without newline character (on the same line);
  • println(): prints the data on the web-based serial monitor with a newline character (on the next line);

Installing the WebSerial Library

For this project, we’ll use the WebSerial.h library. To install the library:

  1. In your Arduino IDE, go to Sketch > Include Library > Manage Libraries …
  2. Search for webserial.
  3. Install the WebSerial library by Ayush Sharma.
Installing Web Serial Library Arduino IDE

You also need to install the ESPAsyncWebServer and the AsyncTCP libraries. Click the following links to download the libraries’ files.

To install these libraries, click on the previous links to download the libraries’ files. Then, in your Arduino IDE, go to Sketch > Include Library > Add .ZIP Library…

If you’re using VS Code with the PlatformIO extension, copy the following to the platformio.ini file to include the libraries.

lib_deps = ESP Async WebServer
  ayushsharma82/WebSerial @ ^1.1.0

ESP8266 WebSerial Example

The library provides a simple example of creating the Web Serial Monitor to output and receive messages. We’ve modified the example a bit to make it more interactive.

This example prints Hello! to the web-based serial monitor every two seconds. Additionally, you can send messages from the web-based serial monitor to the board. You can send the message ON to light up the board’s built-in LED or the message OFF to turn it off.

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-webserial-library/
  
  This sketch is based on the WebSerial library example: ESP8266_Demo
  https://github.com/ayushsharma82/WebSerial
*/

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

#define LED 2

AsyncWebServer server(80);

const char* ssid = "REPLACE_WITH_YOUR_SSID";          // Your WiFi SSID
const char* password = "REPLACE_WITH_YOUR_PASSWORD";  // Your WiFi Password

void recvMsg(uint8_t *data, size_t len){
  WebSerial.println("Received Data...");
  String d = "";
  for(int i=0; i < len; i++){
    d += char(data[i]);
  }
  WebSerial.println(d);
  if (d == "ON"){
    digitalWrite(LED, LOW);
  }
  if (d=="OFF"){
    digitalWrite(LED, HIGH);
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.printf("WiFi Failed!\n");
    return;
  }
  Serial.println("IP Address: ");
  Serial.println(WiFi.localIP());
  // WebSerial is accessible at "<IP Address>/webserial" in browser
  WebSerial.begin(&server);
  WebSerial.msgCallback(recvMsg);
  server.begin();
}

void loop() {
  WebSerial.println("Hello!");
  delay(2000);
}

View raw code

Before uploading the code to your board, don’t forget to insert your network credentials.

In this example, the ESP8266 is in station mode. This example also works in access point mode. To learn how to set up your ESP8266 as an access point, read:

How the Code Works

Continue reading to learn how the code works or skip to the demonstration section.

First, you need to include the required libraries for WebSerial. The ESP8266WiFi.h library is needed to connect the ESP8266 to a Wi-Fi network.

#include <ESP8266WiFi.h>

The WebSerial library uses the ESPAsyncTCP and the ESPAsyncWebServer libraries to create the web-based serial monitor.

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

Finally, the WebSerial library provides easy methods to build the web-based serial monitor.

#include <WebSerial.h>

Create a variable called LED for the built-in LED on GPIO 2.

#define LED 2

Initialize an AsyncWebServer object on port 80 to set up the web server.

AsyncWebServer server(80);

Insert your network credentials in the following variables:

const char* ssid = "REPLACE_WITH_YOUR_SSID"; // Your WiFi SSID
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Your WiFi Password

Handling Received Messages

The following function receives incoming messages sent from the web-based serial monitor. The message is saved on the d variable. Then, it is printed on the web serial monitor using WebSerial.println(d).

void recvMsg(uint8_t *data, size_t len){
  WebSerial.println("Received Data...");
  String d = "";
  for(int i=0; i < len; i++){
    d += char(data[i]);
  }
  WebSerial.println(d);

Next, we check if the content of the d variable is ON or OFF and light up the LED accordingly.

if (d == "ON"){
  digitalWrite(LED, LOW);
}
if (d=="OFF"){
  digitalWrite(LED, HIGH);
}

The built-in LED works with inverted logic: send a HIGH signal to turn it off and a LOW signal to turn it on.

setup()

In the setup(), set the LED as an OUTPUT and turn it off by default.

pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);

Connect your board to your local network:

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  Serial.printf("WiFi Failed!\n");
  return;
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());

Initialize the web-based serial monitor with the begin() method on the WebSerial object. This function accepts as an argument an AsyncWebServer object.

WebSerial.begin(&server);

Register the recvMsg() as a callback function using the msgCallback() method on the WebSerial object. The recvMsg() function will run whenever you send a message from the monitor to the board.

WebSerial.msgCallback(recvMsg);

Finally, initialize the server.

server.begin();

It is just after calling this line that the web-based serial monitor will start working.

loop()

In the loop(), print the Hello! message every 2000 milliseconds (2 seconds) using the println() function on the WebSerial object.

void loop() {
  WebSerial.println("Hello!");
  delay(2000);
}

Demonstration

After inserting your network credentials, you can upload the code to your board.

After uploading, open the “regular” serial monitor at a baud rate of 115200. The board’s IP address will be printed.

ESP8266 NodeMCU Get IP address Arduino IDE Serial Monitor

Now, open a browser on your local network and type the ESP IP address followed by /webserial. For example, in my case:

192.168.1.100/webserial

The WebSerial page should load.

ESP8266 NodeMCU Testing Web Serial Library Basic Example Demonstration

As you can see, it is printing Hello! every two seconds. Additionally, you can send commands to the ESP8266. All the commands that you send are printed back on the web serial monitor. You can send the ON and OFF commands to control the built-in LED.

ESP8266 Built-in LED ON

This was just a simple example showing how you can use the WebSerial library to create a web-based serial monitor to send and receive data.

Now, you can easily add a web-based serial monitor to any of your projects using the WebSerial library.

Wrapping Up

In this quick tutorial, you learned how to create a web-based serial monitor. This is especially useful if your project is not connected to your computer via Serial communication and you still want to visualize debugging messages. The communication between the web-based serial monitor and the ESP8266 uses WebSocket protocol.

We hope you find this tutorial useful. We have other web server tutorials you may like:

Learn more about the ESP8266 board with our resources:

Thank you for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

19 thoughts on “ESP8266 NodeMCU WebSerial: Web-based Remote Serial Monitor”

  1. Hai! I have the error message “call of overloaded ‘println(IPAddress)’ is ambiguous” while compiling code attached below. Please find the solution for that.

    #include <Arduino.h>
    #include <ESP8266WiFi.h>
    #include <ESPAsyncTCP.h>
    #include <ESPAsyncWebServer.h>
    #include <WebSerial.h>
    #include <AsyncElegantOTA.h>;

    AsyncWebServer server(80);

    const char* ssid = “”; // Your WiFi SSID
    const char* password = “”; // Your WiFi Password

    // only for sent message from webserial
    //void recvMsg(uint8_t *data, size_t len) {
    // WebSerial.println(“Received Data…”);
    // String d = “”;
    // for (int i = 0; i < len; i++) {
    // d += char(data[i]);
    // }
    // WebSerial.println(d);
    //}

    void setup() {
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.printf(“WiFi Failed!\n”);
    WebSerial.print(“WiFi Failed!\n”);
    return;
    }
    Serial.print(“IP Address: “);
    Serial.println(WiFi.localIP());
    WebSerial.print(“IP Address: “);
    WebSerial.println(WiFi.localIP());
    // WebSerial is accessible at “/webserial” in browser
    WebSerial.begin(&server);
    //WebSerial.msgCallback(recvMsg);//Sent message from webserial
    AsyncElegantOTA.begin(&server);
    server.begin();
    }

    void loop() {
    AsyncElegantOTA.loop();
    WebSerial.print(“Hello”);

    }

    Reply
    • Hi.
      Instead of the following lines:

      Serial.print("IP Address: ");
      Serial.println(WiFi.localIP());
      WebSerial.print("IP Address: ");
      WebSerial.println(WiFi.localIP());

      Use these ones instead:

      Serial.print("IP Address: ");
      String IP = WiFi.localIP().toString();
      Serial.println(IP);
      WebSerial.print("IP Address: ");
      WebSerial.println(IP);

      I hope this helps.
      Regards,
      Sara

      Reply
  2. There seems to be a slight error in the examples, that crept into your example as well:
    it is not necessary to declare
    #include <ESPAsyncTCP.h>
    #include <ESPAsyncWebServer.h>
    as these are declared in the webserial.h file

    Reply
  3. When first run The board’s IP address will be printed. e.g. 192.168.1.16.
    Is there a way to specify the local IP address?

    Reply
  4. Hai Sara,

    I want to use WebSerial with an I2C-scanner. Problem is that a “Serial.print(address,HEX);” works but a “WebSerial.println(address,HEX);” not.

    The error message gives: no matching function for call to ‘WebSerialClass::println(byte&, int)’…

    Can you help? Thanks…

    Reply
  5. Hi Sara, Very pleased to see this tutorial as I have some project boxes in inaccessible places!

    However, will I still be able to serial print via USB cable with this solution, or has it over WiFi only?

    Also can I use this to set values on the ESP?

    Many thansk,

    Paul

    Reply
    • Hi Paul.
      You can still use the Serial.print() functions via USB cable.
      Yes, you can use this to set values on the ESP. You just need to save the received messages on a variable, check its content, and then act according to its value.
      Regards,
      Sara

      Reply
      • Hi Sara, I have tried to implement WebSerial print in my existing code, but firstly I noticed that the regular serial print commands no longer produced an output, so I was unable to check the IP address. However I had several log files from my code previously, so I used that IP address/webserial but I couldn’t connect. So then I changed references to Serial.print to WebSerial.print and now the code will not compile. I am using PlatformIO. I am getting several warnings and an error:-

        Compiling .pio\build\nodemcuv2\src\main.cpp.o
        src\main.cpp: In function ‘void recvMsg(uint8_t*, size_t)’:
        src\main.cpp:59:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
        for(int i=0; i < len; i++)
        ^
        src\main.cpp: In function ‘void loop()’:
        src\main.cpp:180:50: error: call of overloaded ‘println(long unsigned int)’ is ambiguous
        WebSerial.println (currentTime – previousTime);
        ^
        src\main.cpp:180:50: note: candidates are:
        In file included from src\main.cpp:8:0:
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:57:10: note: void WebSerialClass::println(String)
        void println(String m = “”);
        ^
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:57:10: note: no known conversion for argument 1 from ‘long unsigned int’ to ‘String’
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:59:10: note: void WebSerialClass::println(const char)
        void println(const char *m);
        ^
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:59:10: note: no known conversion for argument 1 from ‘long unsigned int’ to ‘const char

        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:61:10: note: void WebSerialClass::println(char)
        void println(char *m);
        ^
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:61:10: note: no known conversion for argument 1 from ‘long unsigned int’ to ‘char

        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:63:10: note: void WebSerialClass::println(int)
        void println(int m);
        ^
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:65:10: note: void WebSerialClass::println(uint8_t)
        void println(uint8_t m);
        ^
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:67:10: note: void WebSerialClass::println(uint16_t)
        void println(uint16_t m);
        ^
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:69:10: note: void WebSerialClass::println(uint32_t)
        void println(uint32_t m);
        ^
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:71:10: note: void WebSerialClass::println(float)
        void println(float m);
        ^
        .pio\libdeps\nodemcuv2\WebSerial\src/WebSerial.h:73:10: note: void WebSerialClass::println(double)
        void println(double m);
        ^
        src\main.cpp:223:45: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
        if (((pIRiNStatus != priorpIRiNStatus && pIRiNStatus == 1
        ^
        *** [.pio\build\nodemcuv2\src\main.cpp.o] Error 1
        =========================================== [FAILED] Took 13.35 seconds =

        Reply
        • Hi Sara – I noticed that when I changed back WebSerial on one line to Serial.Print, the code compiled fine. The previousTime became unrecognised when I used WebSerial.Print….

          Here is the error message:-src\main.cpp: In function ‘void loop()’:
          src\main.cpp:180:50: error: call of overloaded ‘println(long unsigned int)’ is ambiguous
          WebSerial.println (currentTime – previousTime);
          ^
          And here is the block of code, which now compiles when I change one specific reference from WebSerial to Serial.

          Any ideas what is happening here?

          if (systemState != priorSystemState)
          {
          WebSerial.print (F(“States – FROM:- “));
          WebSerial.print (messages[priorSystemState]);
          WebSerial.print (F(” TO -> “));
          WebSerial.println (messages[systemState]);
          WebSerial.print (F(“doorSensorStatus:- “));
          WebSerial.println (doorSensorStatus);
          WebSerial.print (F(“masterSwitchStatus:- “));
          WebSerial.println (masterSwitchStatus);
          WebSerial.print (F(“Current Time:- “));
          WebSerial.print (currentHour);
          WebSerial.print (F(“H:”));
          WebSerial.print (currentMin);
          WebSerial.print (F(“m:”));
          WebSerial.print (currentSecs);
          WebSerial.println (F(“s:”));
          WebSerial.print (F(“PriorPIR / CurrentPIR – INSIDE “));
          WebSerial.print (priorpIRiNStatus);
          WebSerial.print (F(” / “));
          WebSerial.println (pIRiNStatus);
          WebSerial.print (F(“PriorPIR / CurrentPIR – OUTSIDE “));
          WebSerial.print (priorpIRoUTStatus);
          WebSerial.print (F(” / “));
          WebSerial.println (pIRoUTStatus);
          WebSerial.print (F(“HBridge 1 / HBridge 2:- “));
          WebSerial.print (digitalRead(HBridge1));
          WebSerial.print (F(” / “));
          WebSerial.println (digitalRead(HBridge2));
          WebSerial.print (F(“Flap Elapsed Time: “));
          Serial.println (currentTime – previousTime);
          WebSerial.print(F(“motorEnable Pin: “));
          WebSerial.println(digitalRead(motorEnable));
          WebSerial.println (F(“***********************”));

          Reply
          • Hi.
            Save the result of the currentTime – previousTime in a variable and convert it to a string before printing it using webserial.
            Regards,
            Sara

    • I was able to change the port on which either the server you want, or webserial, when instantiating the server. Of course, you will have to change the name of the server from the examples, and any references to them in your code.

      Example:
      AsyncWebServer serial_server(80);
      AsyncWebServer ota_server(81);

      I have webserial running on port 80, and then I start the OTA server on port 81. I can assume any number of servers running all on different ports.

      Reply
  6. I am more interested to know if there is a color implementation for this. Particularly using the standard ANSI color codes.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.