HTTP Request – Arduino Tutorial

4,828 views

Introduction

Interfacing Arduino with HTTP requests involves connecting an Arduino board to a network and using it to send and receive HTTP messages. This allows Arduino to communicate with web servers, cloud services, and other devices on the internet. By integrating HTTP requests with Arduino, it is possible to create powerful IoT applications and smart devices that can perform a variety of tasks such as sending data to cloud services, controlling home automation systems, or interacting with social media platforms. With the popularity of the Arduino platform and the widespread use of HTTP requests, this approach has become increasingly popular and offers a simple and accessible way to connect the physical and digital worlds.

Hardware Components

You will require the following hardware for Arduino HTTP Request Tutorial.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Arduino Ethernet Shield 21
4.Ethernet Cable1
5.Power Adapter for Arduino9V1

HTTP Request with Arduino

  1. Include necessary libraries:
#include <SPI.h>
#include <Ethernet.h>

The code includes the SPI and Ethernet libraries.

  1. Set up network parameters:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
int HTTP_PORT = 80;
String HTTP_METHOD = "GET"; // or POST
char HOST_NAME[] = "maker.ifttt.com";
String PATH_NAME = "/trigger";

The code sets up the MAC address of the Ethernet shield, creates an Ethernet client object, sets the HTTP port, HTTP method, hostname, and path of the web server.

  1. Set up Serial communication and initialize Ethernet:
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
  Serial.println("Failed to obtain an IP address using DHCP");
  while(true);
}

The code sets up serial communication and initializes Ethernet using the MAC address. If it fails to obtain an IP address using DHCP, it prints a message and enters an infinite loop.

  1. Connect to web server and make HTTP request:
if(client.connect(HOST_NAME, HTTP_PORT)) {
  // if connected:
  Serial.println("Connected to server");
  // make a HTTP request:
  // send HTTP header
  client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
  client.println("Host: " + String(HOST_NAME));
  client.println("Connection: close");
  client.println(); // end HTTP header

  while(client.connected()) {
    if(client.available()){
      // read an incoming byte from the server and print it to serial monitor:
      char c = client.read();
      Serial.print(c);
    }
  }

  // the server's disconnected, stop the client:
  client.stop();
  Serial.println();
  Serial.println("Disconnected");
} else {// if not connected:
  Serial.println("Connection failed");
}

The code connects to the web server using the hostname and port, and makes an HTTP request using the HTTP method, hostname, and path. It reads the server’s response byte by byte and prints it to the serial monitor. When the server disconnects, it stops the client and prints a message to the serial monitor.

  1. Loop indefinitely:
void loop() {

}

Schematic

Make connections according to the circuit diagram given below.

HTTP-request-get-post-data

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Progam Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

#include <SPI.h>
#include <Ethernet.h>

// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

EthernetClient client;

int    HTTP_PORT   = 80;
String HTTP_METHOD = "GET"; // or POST
char   HOST_NAME[] = "maker.ifttt.com";
String PATH_NAME   = "/trigger";

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

  // initialize the Ethernet shield using DHCP:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to obtaining an IP address using DHCP");
    while(true);
  }

  // connect to web server on port 80:
  if(client.connect(HOST_NAME, HTTP_PORT)) {
    // if connected:
    Serial.println("Connected to server");
    // make a HTTP request:
    // send HTTP header
    client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
    client.println("Host: " + String(HOST_NAME));
    client.println("Connection: close");
    client.println(); // end HTTP header

    while(client.connected()) {
      if(client.available()){
        // read an incoming byte from the server and print it to serial monitor:
        char c = client.read();
        Serial.print(c);
      }
    }

    // the server's disconnected, stop the client:
    client.stop();
    Serial.println();
    Serial.println("disconnected");
  } else {// if not connected:
    Serial.println("connection failed");
  }
}

void loop() {

}

Working Explanation

In the setup() function, the sketch initializes the serial communication and attempts to obtain an IP address using DHCP. If it fails to obtain an IP address, the sketch will print a message to the serial monitor and enter an infinite loop.

Next, the sketch attempts to connect to the Maker IFTTT web server on port 80 using the EthernetClient object. If the connection is successful, the sketch sends an HTTP GET request to the server with the specified path name. It then waits for a response from the server and prints the response to the serial monitor.

Finally, the sketch stops the client and prints a message to the serial monitor indicating that the connection has been closed. In the loop() function, there is no code, so the sketch will simply loop indefinitely without doing anything.

Applications

  • Weather monitoring systems
  • Smart agriculture systems
  • Industrial automation systems
  • Robotics and automation projects
  • Data logging and analytics projects
  • Remote monitoring and control systems

Conclusion.

We hope you have found this HTTP Request Tutorial very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.