ESP8266 (NodeMCU) post request data to website

In this example we learn how to send post 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 GET 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


The POST method requests that the target resource process the representation enclosed in the request according to the resource’s own specific semantics. For example, POST is used for the following functions (among others):

  1. Providing a block of data, such as the fields entered into an HTML form, to a data-handling process;
  2. Posting a message to a bulletin board, newsgroup, mailing list, blog, or similar group of articles;
  3. Creating a new resource that has yet to be identified by the origin server; and
  4. Appending data to a resource’s existing representation(s).

An origin server indicates response semantics by choosing an appropriate status code depending on the result of processing the POST request; almost all of the status codes defined by this specification might be received in a response to POST (the exceptions being 206 (Partial Content), 304 (Not Modified), and 416 (Range Not Satisfiable)).

The POST Method

Note that the query string (name/value pairs) is sent in the HTTP message body of a POST request:

Some other notes on POST requests:

  • POST requests are never cached
  • POST requests do not remain in the browser history
  • POST requests cannot be bookmarked
  • POST requests have no restrictions on data length

ESP8266 POST Example Code

Make changes in wifi settings, SSID and password of your wifi network and change server ip. Also change POST 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.


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

Data logging PHP Code which handles POST request

postdemo.php file

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 GET request ? explained here

9 thoughts on “ESP8266 (NodeMCU) post request data to website

  1. When I run my post data sketch and the MCU submiuts the post reequest, the target PHP script does not execute. The HTTP return code is is 200 and the Request response payload is tge text of the target php script ( the same as if i had browsed to the script).
    I have set the permissions to 777. What am I doing wrong?

  2. I have already tried this tutorial and GET tutorial too. It worked, but when i put the php files on hosting server, it didnt work. I already change the “host” address in the sketch and already try to open the web through a web browser (which can be opened) but the data that should be post by nodemcu did not get there (it is not like when i still use the localhost server). Could you help me to solve this?
    Thank you

    1. Most common problem is HTTPS Secured server See this tutorial

      There are few reasons for that
      1. Your ESP is not getting internet connection.
      2. Linux server is case SenSiTive.
      3. Check http and https of your website
      4. Check GET request sent through browser is working properly .
      5. Also check response from server, on serial monitor.
      6. Check Database is created properly.

          1. Thank you for answer.I tried to reach localhost.should i specify to port i don’t know anything about it.

  3. This a really good tutorial, well done. I am implementing a small project and i would like to ask how i could modify your tutorial. I have a server running LAMP (Linux, Apache, Mysql, Php7) and i need it to keep track on its database the value of 2 sensors.
    An ESP8266 nodemcu needs to have 2 magnetic door switches (named Door_A & Door_B). Once one of them change its state then send an update to the MYSQL, instead of polling it every x secs. Is there any way that i could implement that please?

Leave a Reply