Arduino Nano ESP32 - Web Plotter
This tutorial instructs you on how to build a web-based plotter that resemble the Serial Plotter found in the Arduino IDE. This web-based plotter enables monitoring of real-time data from an Arduino Nano ESP32 using a web browser on either your smartphone or PC. The plotted data will be presented in a graph format, looks like what you typically observe in the Serial Plotter of the Arduino IDE.
Hardware Preparation
1 | × | Arduino Nano ESP32 | |
1 | × | USB Cable Type-C | |
1 | × | (Recommended) Screw Terminal Expansion Board for Arduino Nano | |
1 | × | (Recommended) Breakout Expansion Board for Arduino Nano |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
How Web Plotter Works
- The Arduino Nano ESP32 code creates both a web server and a WebSocket server.
- When a user accesses the webpage hosted on the Arduino Nano ESP32 board via a web browser, the Arduino Nano ESP32's web server sends back the web content (HTML, CSS, JavaScript) to the browser.
- The JavaScript code running in the web browser creates a graph resembling the Serial Plotter.
- Upon clicking the connect button on the webpage, the JavaScript code initiates a WebSocket connection to the WebSocket server running on the Arduino Nano ESP32 board.
- The Arduino Nano ESP32 sends data over the WebSocket connection to the web browser in a format similar to that used by the Serial Plotter (details provided in the next part).
- The JavaScript code in the web browser receives the data and plots it on the graph.
The data format that Arduino Nano ESP32 sends to web plotter
To plot multiple variables, we need to separate variables from each other by “\t” or " " character. The last value MUST be terminated by “\r\n” characters.
In detail:
- The first variable
- The middle variables
- The last variable
For more detail, please refer to Arduino Nano ESP32 - Serial Plotter tutorial
Arduino Nano ESP32 Code - Web Plotter
The webpage's content (HTML, CSS, JavaScript) is stored separately in an index.h file. Therefore, in the Arduino IDE, we will have two code files:
- An .ino file containing the Arduino Nano ESP32 code, which creates both a web server and a WebSocket server.
- An .h file that houses the webpage's content.
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Connect the Arduino Nano ESP32 board to your PC via a USB cable
- Open Arduino IDE on your PC.
- Select the right Arduino Nano ESP32 board (e.g. Arduino Nano ESP32 and COM port.
- Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE.
- Search “ESPAsyncWebServer”, then find the ESPAsyncWebServer created by lacamera.
- Click Install button to install ESPAsyncWebServer library.
- You will be asked to install the dependency. Click Install All button.
- Search “WebSockets”, then find the WebSockets created by Markus Sattler.
- Click Install button to install WebSockets library.
- On Arduino IDE, create new sketch, Give it a name, for example, newbiely.com.ino
- Copy the below code and open with Arduino IDE
- Modify the WiFi information (SSID and password) in the code to match your own network credentials.
- Create the index.h file On Arduino IDE by:
- Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
- Give the file's name index.h and click OK button
- Copy the below code and paste it to the index.h.
- Now you have the code in two files: newbiely.com.ino and index.h
- Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32.
- Open the Serial Monitor
- Check out the result on Serial Monitor.
- Take note of the IP address displayed, and enter this address into the address bar of a web browser on your smartphone or PC.
- You will see the webpage it as below:
- Click the CONNECT button to connect the webpage to Arduino Nano ESP32 via WebSocket.
- You will see the plotter plots data as the below image.
- You can open the Serial Plotter on Arduino IDE to compare with the Web Plotter on the web browser.
- If you modify the HTML content in the index.h file and do not make any changes to the newbiely.com.ino file, when you compile and upload the code to the Arduino Nano ESP32 using the Arduino IDE, the IDE will not update the HTML content.
- To force the Arduino IDE to update the HTML content in this scenario, you need to make a change in the newbiely.com.ino file. For example, you can add an empty line or include a comment.
※ NOTE THAT:
Line-by-line Code Explanation
The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!