echo '' ;

Archives

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

ESP8266 Arduino-Core Tutorial – Web Server

TCP Server Listener

The below Arduino code will also create a server and Access Point in ESP8266 which will continuously listen for a connection.

Code

//www.ArunEworld.com

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char *ssid = "ArunEworld";
const char *password = "Arun";

ESP8266WebServer server(80);

void handleRoot() 
{
    server.send(200, "text/html", "<t1>ArunEworld</t1>");
    server.send(200, "text/html", "<h1>ArunEworld : TCP WebServer Listener</h1>");
    server.send(200, "text/html", "<h2>You are connected</h2>");
}

void setup()
{
    delay(1000);
    Serial.begin(115200);
    Serial.println();
    Serial.print("Configuring access point...");
    WiFi.softAP(ssid, password);
    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    server.on("/", handleRoot);
    server.begin();
    Serial.println("HTTP server started");
}

void loop() 
{
    server.handleClient();
}

After uploading this sketch, you can find a new Access Point named “test” from your Laptop or PC.

Result


 

ESP32 ArduinoCore Interface – OW (DS18B20)

The ESP32 ArduinoCore Interface for OneWire (OW) communication protocol is a crucial aspect of interfacing with digital temperature sensors like the DS18B20. This interface allows the ESP32 microcontroller to communicate with one or more DS18B20 temperature sensors using the OneWire protocol.

Read more: ESP32 ArduinoCore Interface – OW (DS18B20)

Components

ComponentDescription
ESP32 MicrocontrollerA powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities.
DS18B20 Temperature SensorsDigital temperature sensors manufactured by Maxim Integrated, known for high accuracy readings.
OneWire LibraryProvides functions for communication over the OneWire bus, essential for interfacing with DS18B20 sensors.
DallasTemperature LibrarySimplifies communication with DS18B20 sensors by providing high-level functions and features.

Code

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 22
#define TEMPERATURE_PRECISION 12 // Lower resolution

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int numberOfDevices;
DeviceAddress tempDeviceAddress;

void setup(void) {
  Serial.begin(115200);
  Serial.println("Dallas Temperature IC Control Library Demo");
  sensors.begin();
  numberOfDevices = sensors.getDeviceCount();
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
  for(int i=0;i<numberOfDevices; i++) {
    if(sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Found device ");
      Serial.print(i, DEC);
      Serial.print(" with address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
      Serial.print("Setting resolution to ");
      Serial.println(TEMPERATURE_PRECISION, DEC);
      sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
      Serial.print("Resolution actually set to: ");
      Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
      Serial.println();
    } else {
      Serial.print("Found ghost device at ");
      Serial.print(i, DEC);
      Serial.print(" but could not detect address. Check power and cabling");
    }
  }
  delay(5000);
}

void printTemperature(DeviceAddress deviceAddress) {
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.println(tempC);
  Serial.print("Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}

void loop(void) { 
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures();
  Serial.println("DONE");
  for(int i=0;i<numberOfDevices; i++) {
    if(sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Temperature for device: ");
      Serial.println(i,DEC);
      printTemperature(tempDeviceAddress);
      delay(3000);
    }
  }
}

void printAddress(DeviceAddress deviceAddress) {
  for (uint8_t i = 0; i < 8; i++) {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

Code Explanation

Code SectionExplanation
#include <OneWire.h>Includes the OneWire library for communication with devices using the OneWire protocol.
#include <DallasTemperature.h>Includes the Dallas Temperature library for interfacing with Dallas/Maxim temperature ICs.
#define ONE_WIRE_BUS 22Defines the GPIO pin (pin 22) where the OneWire data wire is connected to the ESP32.
#define TEMPERATURE_PRECISION 12Defines the resolution for temperature readings (12 bits for lower resolution).
OneWire oneWire(ONE_WIRE_BUS);Initializes a OneWire object with the specified GPIO pin.
DallasTemperature sensors(&oneWire);Initializes a DallasTemperature object using the previously created OneWire object.
int numberOfDevices;Declares a variable to store the number of temperature devices found on the bus.
DeviceAddress tempDeviceAddress;Declares a variable to store the address of a found temperature device.
void setup(void)Begins the setup function, which is called once when the program starts. Initializes serial communication and the sensor library.
Serial.begin(115200);Starts serial communication with a baud rate of 115200.
sensors.begin();Initializes the Dallas Temperature library.
numberOfDevices = sensors.getDeviceCount();Retrieves the number of temperature devices found on the OneWire bus.
for(int i=0;i<numberOfDevices; i++)Loops through each temperature device found on the bus.
sensors.getAddress(tempDeviceAddress, i)Retrieves the address of the i-th device and stores it in tempDeviceAddress.
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);Sets the resolution of the temperature device to the defined precision.
void printTemperature(DeviceAddress deviceAddress)Declares a function to print the temperature of a device with the given address.
void loop(void)Begins the loop function, which runs continuously after setup.
sensors.requestTemperatures();Requests temperature readings from all connected devices on the bus.
for(int i=0;i<numberOfDevices; i++)Loops through each temperature device found on the bus.
printTemperature(tempDeviceAddress);Prints the temperature of the i-th device.
void printAddress(DeviceAddress deviceAddress)Declares a function to print the address of a device.

Functionality of

  1. Initialization: The interface initializes the OneWire communication by defining the GPIO pin to which the OneWire data wire is connected. It also initializes the DallasTemperature library, which simplifies communication with DS18B20 sensors.
  2. Device Detection: Upon initialization, the interface detects the number of DS18B20 sensors connected to the OneWire bus. It retrieves the unique address of each sensor and sets their resolution if detected.
  3. Temperature Reading: The interface periodically requests temperature readings from all connected DS18B20 sensors. It then retrieves the temperature data and converts it to Celsius and Fahrenheit scales for further processing or display.

NEXT

Arduino-Core Get Start (Soon)
ESP32 Arduino Core Interface
ArduinoCore Interface Basics
ArduinoCore Interface WiFi
ArduinoCore Interface – LED
ArduinoCore Interface ADC
ArduinoCore Interface DS18B20
ESP32 Arduino Core Projects
ArduinoCore Project – WebServer
Others
Arduino-Core Sitemap
Arduino-Core All Post

ESP32 ArduinoCore Interface – ADC

The “ESP32 ArduinoCore Interface – ADC” provides a seamless integration between the ESP32 microcontroller and the Arduino development environment, specifically focusing on the Analog-to-Digital Converter (ADC) functionality.

ADC

TermDescription
ADCAnalog-to-Digital Converter – A device or circuit that converts analog signals to digital data.
FunctionalityConverts continuous analog signals into discrete digital values.
ProcessSamples the analog input signal at regular intervals and quantizes the sampled values into digital values.
ApplicationsUsed in microcontrollers, data acquisition systems, sensors, audio equipment, communication devices, and more.
ResolutionThe number of digital bits used to represent the analog signal. Higher resolution ADCs provide more precise representations.
Sampling RateDetermines how frequently the ADC samples the analog input signal. Higher sampling rates enable more accurate representation of fast-changing signals.
TypesSuccessive approximation, delta-sigma, pipeline, and flash ADCs are common types, each with specific advantages and applications.
InterfaceInterfaces with digital systems such as microcontrollers or computers, where the digital output values can be processed or stored.

ADC Pins

PinADC ChannelGPIO Number
GPIO32ADC1_CH432
GPIO33ADC1_CH533
GPIO34ADC1_CH634
GPIO35ADC1_CH735
GPIO36ADC1_CH036
GPIO37ADC1_CH137
GPIO25ADC2_CH825
GPIO26ADC2_CH926

This table lists the ADC pins available on the ESP32 microcontroller along with their corresponding ADC channels and GPIO numbers.

Code

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

Code Explanation of ESP32 ArduinoCore Interface ADC

Code Purpose: Reading an analog input from pin A0 and printing the value to the serial monitor.

Setup Routine: This part of the code initializes serial communication at a baud rate of 9600 bits per second.

   // the setup routine runs once when you press reset:
   void setup() {
     // initialize serial communication at 9600 bits per second:
     Serial.begin(9600);
   }

Loop Routine:

  1. This section continuously reads the analog value from pin A0 using the analogRead() function.
  2. It then prints the value to the serial monitor using Serial.println().
  3. A small delay of 1 millisecond is added between reads for stability using delay().
   // the loop routine runs over and over again forever:
   void loop() {
     // read the input on analog pin 0:
     int sensorValue = analogRead(A0);
     // print out the value you read:
     Serial.println(sensorValue);
     delay(1);        // delay in between reads for stability
   }

Overall Functionality: This code can be useful for testing analog sensors or for basic data-logging applications.

Advantage of ESP32 ArduinoCore Interface ADC

AdvantageDescription
Analog Signal ProcessingADCs enable microcontrollers to process analog signals from the physical world, converting them into digital values that can be processed by digital systems.
Sensor InterfacingADCs facilitate interfacing with various sensors that produce analog output, such as temperature sensors, light sensors, and pressure sensors, allowing accurate measurement and response to real-world phenomena.
Signal ConditioningADCs can be used for signal conditioning tasks, including amplification, filtering, and noise reduction, before converting analog signals to digital form, improving accuracy and reliability of measured data.
Data AcquisitionADCs enable microcontrollers to acquire data from analog sources at high speeds and with high precision, suitable for applications such as data logging, instrumentation, and control systems.
VersatilityADCs come in various resolutions, sampling rates, and input voltage ranges, allowing developers to choose the most suitable ADC for their specific application requirements.
IntegrationMany microcontrollers, including the ESP32, feature built-in ADCs, eliminating the need for external ADC components and reducing system complexity and cost.

NEXT

Arduino-Core Get Start (Soon)
ESP32 Arduino Core Interface
ArduinoCore Interface Basics
ArduinoCore Interface WiFi
ArduinoCore Interface – LED
ArduinoCore Interface ADC
ArduinoCore Interface DS18B20
ESP32 Arduino Core Projects
ArduinoCore Project – WebServer
Others
Arduino-Core Sitemap
Arduino-Core All Post

ESP8266 Arduino-Core Interface – ADC

ADC

Required

  • Required Hardware – ESP8266 with Programmer (or)  NodeMCU Dev Kit
  • Required Software Tools  – Arduino IDE with ESP8266 Core

Circuit

Code

/* 
  http://www.ArunEworld.com/Embedded/ESPressif/ESP8266/ESP8266_Arduino-Core/
  Tested By  : Arun(20170219)
  Example Name : AEW_ADC-Interface.ino
 */
 /*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

 

Result

3
2
3
4
3
3
4
3
2
3
4
2
2

 


Next :

Previous :


 

ESP8266 Arduino-Core Interface – Button

Button – Digital Read Serial

  • Required Hardware ESP8266 with Programmer (or)  NodeMCU Dev Kit
  • Required Software Tools Arduino IDE with ESP8266 Core

Code

/* 
  http://www.ArunEworld.com/Embedded/ESPressif/ESP8266/ESP8266_Arduino-core/ESP8266-Arduino-Core-Interface-Button
  Tested By  : Arun(20170219)
  Example Name : AEW_ADC-Interface.ino
 */
 /*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

 

Output

0
1
1
1
0
0

 


Next :

Previous :


 

ESP32 ArduinoCore Interface – Wi-Fi

Scan WiFi

Note : You can use  arduino example code instead of below code because both are same  (File > Example > WiFi> WiFiScan)

/* https://aruneworld.com/embedded/espressif/esp32
 * Tested By  : Arun(20170429)
 * Example Name : AEW_WiFi_Scan.ino
 *  This sketch demonstrates how to scan WiFi networks.
 *  The API is almost the same as with the WiFi Shield library,
 *  the most obvious difference being the different file you need to include:
 */
#include "WiFi.h"

void setup()
{
    Serial.begin(115200);

    // Set WiFi to station mode and disconnect from an AP if it was previously connected
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(100);

    Serial.println("Setup done");
}

void loop()
{
    Serial.println("scan start");

    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
            // Print SSID and RSSI for each network found
            Serial.print(i + 1);
            Serial.print(": ");
            Serial.print(WiFi.SSID(i));
            Serial.print(" (");
            Serial.print(WiFi.RSSI(i));
            Serial.print(")");
            Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
            delay(10);
        }
    }
    Serial.println("");

    // Wait a bit before scanning again
    delay(5000);
}

Serial Terminal Output

scan start
scan done
5 networks found
1: ArunEworld (-34)*
2: Exuber_365 (-59)*
3: Bangalore Police (-66)*
4: Tagos-2.4 (-80)*
5: RRL_Internet (-93)*

 

ESP32 ArduinoCore Interface – Basic Example

When programming the ESP32 using the Arduino IDE, you typically use the ESP32 Arduino core, which provides a set of libraries and APIs to facilitate development. In this tutorial, we can discuss ESP32 ArduinoCore Interface Basic Example of Serial, GPIO, Timer, etc

Explore the fundamental features of the ESP32 microcontroller through an introductory example utilizing the ArduinoCore interface. This tutorial covers Serial communication, GPIO configuration for digital input and output, and Timer usage for precise timing operations. Ideal for beginners and hobbyists, this basic example demonstrates how to implement essential functionalities such as blinking an LED, reading button inputs, and handling timer interrupts, providing a solid foundation for further exploration and development with the ESP32 platform.”

Read more… →