ESP8266 HTTP GET Request Example

In this example we learn how to send get request to a web page using NodeMCU or ESP8266? As we know all webpages are HTTP protocols, GET and POST are methods of communicating between web browser and the server. Also we look towards server side php coding. If you are looking for POST method read here.

What is HTTP?

The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers.

HTTP works as a request-response protocol between a client and server. Each Hypertext Transfer Protocol (HTTP) message is either a request or a response. A server listens on a connection for a request, parses each message received, interprets the message semantics in relation to the identified request target, and responds to that request with one or more response messages. A client constructs request messages to communicate specific intentions, examines received responses to see if the intentions were carried out, and determines how to interpret the results.

A web browser may be the client, and an application on a computer that hosts a web site may be the server.

Example: A client (browser) submits an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content.

Two HTTP Request Methods: GET and POST

Two commonly used methods for a request-response between a client and server are: GET and POST.

  • GET – Requests data from a specified resource
  • POST – Submits data to be processed to a specified resource

GET

The GET method requests transfer of a current selected representation for the target resource. GET is the primary mechanism of information retrieval and the focus of almost all performance optimizations.  Hence, when people speak of retrieving some identifiable information via HTTP, they are generally referring to making a GET request.

The GET Method

Note that the query string (name/value pairs) is sent in the URL of a GET request:

/test/demo_form.php?name1=value1&name2=value2

Some other notes on GET requests:

  • GET requests can be cached
  • GET requests remain in the browser history
  • GET requests can be bookmarked
  • GET requests should never be used when dealing with sensitive data
  • GET requests have length restrictions
  • GET requests should be used only to retrieve data

ESP8266 GET Example Code

Make changes in wifi settings, SSID and password of your wifi network and change server ip. Also change GET request data as per your server requirements.

Upload the code and open serial monitor, in case of any problem it will show errors.

Server side programs

Server side program consists of three parts 1. Database creation 2. Data logging and 3. Display logged data. To run this codes you need WAMP or LAMP installed (apache2, mySql and PHP)

Database creation PHP Code i.e. install.php

Enter your mySQL usename and password in code.

install.php

This program will create database and tables required for our demo application. This is run once code.

Data logging PHP Code which handles GET request

getdemo.php

This is responsible for storing data into database. it will give only OK or Errors.

Display logged data

view.php File

This will show logged data in web browser

Display of logged data

This way you can make your own cloud server for data logging and displaying it.

How to make POST request ? explained here

One thought on “ESP8266 HTTP GET Request Example

Leave a Reply