echo '' ;

ESP8266 Arduino-core Tutorial – HTTP Post Data to Web Page

These various applications for sending data, especially in IoT projects. Below are some scenarios and use cases where sending data using an HTTP POST request can be beneficial. In this article will discuss about “ESP8266 Arduino-core Tutorial – HTTP Post Data to Web Page”.

Required

  • ESP8266 Any Module (Used NodeMCU Dev Kit Version 1.0)
  • Arduino IDE with ESP8266 Platform
  • Web Sever (Shared, Linux, Windows or Own Server like with any hosting providers: Amazon, GoDaddy, GlobeHost, BlueHost, HostingRaja and etc)
  • Cpanel Login Access and update index file contend

Cpanel index.php  code for web Server

Note I tested its code is working well.

<?php
    $age= $name = $TimeStamp = "";
    $Hum= $Temp =  "";
    $timezone = new DateTimeZone("Asia/Kolkata" );
        $date = new DateTime();
        $date->setTimezone($timezone );
        echo  $date->format('Ymd-H:i:s');
        $TimeStamp = $date;
        
    
     if( $_REQUEST["Hum"] ||  $_REQUEST["Temp"] ) 
    {
        echo " The Humidity is: ". $_REQUEST['Hum']. "%<br />";
        echo " The Temperature is: ". $_REQUEST['Temp']. " Celcius<br />";
        
        $Hum= $_REQUEST["Hum"];
        $Temp = $_REQUEST["Temp"];
        
        $myfile = fopen("arun.txt", "a") or die("Unable to open file!");
        fwrite($myfile, "TimeStamp : ".$date->format('Ymd-H:i:s')."\t | Hum : ".$Hum."\t | Temp : ".$Temp. " |\n");
        fclose($myfile);
        
    }
    
    if( $_GET["name"] || $_GET["age"] ) {
        echo "Welcome ". $_GET['name']. "<br />";
        echo "You are ". $_GET['age']. " years old.". "<br />";
        
        
        
        $age= $_GET["age"];
        $name = $_GET["name"];
        
        $myfile = fopen("arun.txt", "a") or die("Unable to open file!");
        fwrite($myfile, "TimeStamp : ".$date->format('Ymd-H:i:s')."\t | Name : ".$name."\t | Age : ".$age. " |\n");
        fclose($myfile);
    
        
        exit();
    }
    
    $myfile = fopen("arun.txt", "r") or die("Unable to open file!");
        echo "<table border='1' align ='center' >";
        while(!feof($myfile)) {
          echo "<tr><td border='1' align ='center' >";
          echo fgets($myfile) . "<br>";
          echo "</td></tr>";
        }
        echo "</table>";
        fclose($myfile);
?>

ESP8266 Arduino-Core Code for HTTP Post Data

//www.Aruneworld.com/embedded/esp8266/esp8266-arduino-core/

const char* hostGet = "iot.aruneworld.com/ESP8266/NodeMCU/HTTP-Post/"; 
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char* ssid="ArunEworld";
const char*password="8300026060INDIA";
WiFiClient client;

void postData() {

   WiFiClient clientGet;
   const int httpGetPort = 80;

   //the path and file to send the data to:
   String urlGet = "/index.php";

 
  // We now create and add parameters:
  String src = "12345";
  String typ = "6789";
  String nam = "temp";
  String vint = "92"; 

  urlGet += "?Hum=" + src + "&Temp=" + typ ;
   
      Serial.print(">>> Connecting to host: ");
      Serial.println(hostGet);
      
       if (!clientGet.connect(hostGet, httpGetPort)) {
        Serial.print("Connection failed: ");
        Serial.print(hostGet);
      } else {
          clientGet.println("GET " + urlGet + " HTTP/1.1");
          clientGet.print("Host: ");
          clientGet.println(hostGet);
          clientGet.println("User-Agent: ESP8266/1.0");
          clientGet.println("Connection: close\r\n\r\n");
          
          unsigned long timeoutP = millis();
          while (clientGet.available() == 0) {
            
            if (millis() - timeoutP > 10000) {
              Serial.print(">>> Client Timeout: ");
              Serial.println(hostGet);
              clientGet.stop();
              return;
            }
          }

          //just checks the 1st line of the server response. Could be expanded if needed.
          while(clientGet.available()){
            String retLine = clientGet.readStringUntil('\r');
            Serial.println(retLine);
            break; 
          }

      } //end client connection if else
                        
      Serial.print(">>> Closing host: ");
      Serial.println(hostGet);
          
      clientGet.stop();

}

void setup() {
    Serial.begin(115200); // Starts the serial communication
  WiFi.begin(ssid, password); //begin WiFi connection
  Serial.println("");
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println("ArunEworld");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  postData();

  delay(30000);

}

Make sure to replace the placeholders:

  • your-ssid and your-password with your WiFi credentials.
  • your-server-address with the address of your server.
  • /your-api-endpoint with the specific endpoint on your server that handles the incoming POST requests.

Applications and Uses

Security aspects & Encryption

When implementing HTTP POST requests in your application, consider security aspects such as using HTTPS for encrypted communication and implementing proper authentication mechanisms to protect sensitive data. Always follow best practices for secure communication, especially when transmitting data over the internet.

FAQ

if you have the following Questions and most of the repeated and same-meaning questions, then the Above article should help you

  • How send data to web page using ESP8266?
  • How to HTTP Post Data to Web Page using ESP8266 via Arduino-core Tutorial –
  • ESP8266 send data to website?
  • Send ESP8266 Data to Your Webpage – no AT Commands
  • Arduino-Esp8266-Post-Data-to-Website
  • ESP8266: HTTP POST Requests
  • Posting and getting data from ESP on Apache WebServer
  • How to Send Data from Arduino to Webpage using WiFi?
  • How to send data to a server using ESP8266 with Arduino?

NEXT

Arduino-Core IDE Setup
ESP8266 Arduino Core Interface
ESP8266 Interface LED
ESP8266 Interface Button
ESP8266 Interface ADC
ESP8266 Arduno Core Projects
ESP8266 Project WebServer
HTTP Post Data to Web Page
Arduino Based Platforms
Cayenne MyDevice Get Start
Others
ESP8266 Arduino-Core Sitemap
ESP8266 Arduino-Core All Post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading