# Simple Linear Regression in C/C++ (ESP32 Arduino) with Embedded Web Application

##### Introduction

In the previous tutorial, we have built a web application for bike-sharing prediction hosted in ESP32. The simple linear regression algorithm was implemented using TensorFlow.js. So, the algorithm is processed by the web browser not by the ESP32 itself. In this tutorial, we are going to implement the simple linear regression algorithm using C/C++ programming language in the ESP32. So, the algorithm will be processed by the ESP32 instead of the web browser.

##### System Overview

The following figure shows the system diagram of this tutorial. The ESP32 runs as a WiFi client that connected to the AP. It runs an embedded web server based on the ESPAsyncWebServer library. The web browser (client) makes HTTP request to the ESP32. Then, ESP32 sends the HTTP response along with the HTML, CSS, and JavaScript files. Then, when we enter the temperature value and click the predict button, The web page is going be redirected to the /predict URL followed by the temperature data, i.e. /predict?temperature=25. This method is called HTTP GET.

Also, there is a template placeholder on the web page that will display the prediction result. It is delimited with % symbols, e.g %RESULT%. The placeholder %RESULT% will be replaced with the prediction result by the web server. The web server calculates the prediction using linear regression.

##### Algorithm

In this tutorial, we are going to use the simple linear regression model to make a prediction. It takes one independent variable (temperature) and produce one dependent variable (number of users). The algorithm was modeled in this tutorial.

The following equation defines the hypothesis function of the simple linear regression. We obtained the parameters $$\theta_{0}=25$$ and $$\theta_{1}=1.25$$ from the training using gradient descent. We are going to implements this hypothesis function in C/C++ programming language.

$$h_{\theta}(x)=25+1.25x$$

##### HTML File

Firstly, we are going to modify the HTML file in the previous tutorial. We can delete the TensorFlow.js (tj.min.js) library within the head element. As shown in line 8, do not include that line.

Secondly, within the form element, we should change several lines. As in the following code, in line 1, add action and method attribute within the form element. The action=”/predict” corresponds to the redirection URL (/predict?temperature=25). Then, we use the HTTP GET method, which is specified by method=”GET”.

Thirdly, in line 4, delete the id attribute, and add the name attribute (name=”temperature”). This corresponds to the name/value pair of the HTTP GET request query (/predict?temperature=25). Then, in line 7, delete the id attribute, and add the button type attribute (type=”submit”).

Finally, in line 10, delete the span element that displays the result, and replace it with a template placeholder (%RESULT%) of the ESPAsyncWebServer library.

##### Arduino Sketch

The following code shows the full Arduino sketch for the web server.

This is how the code works:

• First, in line 9-10, we should declare two variables temp and req_root.
• The temp stores the temperature value from the client (web browser), and the req_root identifies whether it is a root request (/) or an HTTP GET request (/predict).
• In line 38, change the input arguments of the code, add the callback function called processor.
• Then, in line 53-61, it reads the HTTP GET name/value pair (/predict?temperature=25), and stores the the value in temp variable.
• After that, in line 62, it calls the callback function processor, and sends the HTTP response.
• The callback function processor is defined in line 74-84. It replaces the template placeholder (%RESULT%) with the prediction result. If the req_root is true, then it replaces the template placeholder (%RESULT%) with value 0, because it is when the client makes the first request the root page and there is no temperature input yet.
• Finally, in line 86-93, we define our hypothesis function of the simple linear regression algorithm. It predicts the number of bike-sharing users.

After that, compile and upload the Arduino sketch to the ESP32. Also, upload the web files from the data folder to the SPIFFS.

##### Source Code

You can get the source code from this repository.

##### Summary

In this tutorial, we have learned how to implement a simple linear regression in ESP32 Arduino. We implement the hypothesis function in C/C++. We develop an embedded web server in ESP32. It provides a web page that can be accessed from a web browser. We uses HTTP GET method to transfer temperature input and prediction result between ESP32 and web browser.