In the previous two IoT articles, I have introduced you to the esp8266 module and the AT commands also. Today I will explain how you can send data to a web server using ESP8266. In other words, you can say how you can make an HTTP request using ESP8266. For those who don’t know what an HTTP request is, let me explain.
Introduction to the HTTP request
HTTP is a protocol to transfer the data to and fro the client and server. Here the client is your browser and the server is the remote (cloud) storage where a website is hosted. To access the files of the server we make an HTTP request with the address of that file. For example, if you search anything on google then your browser will embed your query and some other information about your browser in a link with the server location of google. This process happens in the background but you can see the link that your browser is requesting in the URL bar of the browser.
You can see the search text in the URL. This is how they send any parameter in the URL. Here you can also see that our search term equates to the q parameter and many other parameters are there in the URL. They contain information about the client (browser) and are separated with &. That means the server is listening to these parameters in the URL. As the parameter matches, the server catches and stores the value of that parameter. If you didn’t get it now, don’t worry. I will explain this practically by creating my own server in localhost (computer).
Types of HTTP requests
There are two types of HTTP requests. The first is the get method and the second is the post method.
In the get method, parameters are embedded in the URL and can be seen. As we have seen in google’s example. We send non-sensitive data to the server using this method. We cannot send binary data using this method like images, songs, videos, etc.
In the post method, parameters are not shown in the URL. Data transfer happens in an encrypted way in the background. Using this method we transfer sensitive data like passwords. We can send the binary data also.
Creating the web server on the localhost
To demonstrate to you I will create a server on my computer. To create the server on the localhost we need some tools that allow our computer to serve files to the browser as a server. The tool (software) I will be using is the XAMPP.
You need to download and install the XAMPP on the computer. Click here to download. This software enables the apache web server on your computer. Through this tool, you can enable an apache web server, MySQL database, FileZilla (to upload files to the webserver), and many more things. The XAMPP control panel looks like this.
To create the server you need to just start the Apache in the control panel. Now the server is running on our computer. To access the server you need to type localhost in the browser URL section. You will be redirected to the dashboard of the server. To create a directory (website) you need to search the file named htdocs under the XAMPP installation directory. You can simply search in the start menu of your computer.
Make a directory through which you want to access your website (just like the domain name). I will make the directory with the name “IoTDemo”. Now I can access the files under this directory using localhost/IoTDemo.
To make this look like a webpage you need to add some HTML files under this directory. So create a file with the name “index.html”. Now the reason why we generally use the home page with the name index is that server treats it primarily as a home page. Until you don’t specify the home page in the .htaccess file. Now add this code to the file.