Arduino - HTTP Request

Arduino can play a role as a web client to make HTTP to a web server. Web Server can be a website, Web API or REST API, Web service ...

In this tutorial, we are going to learn:

The code for other WiFi or Ethernet Shield/Board are similar. The difference is only in libaray

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Arduino Ethernet Shield 2
1×Ethernet Cable

OR

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×PHPoC WiFi/Ethernet Shield
1×(optional)Ethernet Cable
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

Basic Knowledge of Web Client and Web Server

When you access a website from your PC or smartphone, you just type the web address (URL) on a web browser, waits for a second, and then the webpage is displayed on your PC/smartphone. You do not know what your PC/smartphone does behind the screen. So, what happens behind the screen:

  1. Your PC/smartphone (as a web client) makes an HTTP request to a web server
  2. The web server returns an HTTP response to your PC/smartphone
  3. Your PC/smartphone receives the HTTP response and visualizes HTTP response on your screen

In this tutorial, We will make Arduino become a web client to do something similar to your PC/smartphone.

Web Address (URL)

An URL includes two parts: hostname and pathname. The hostname can be replaced by the IP address of the web server. For example: example.com/test

In HTTP GET request, URL can includes the query string. For example: example.com/test?temperature=20&humidity=70.

Query String

The query string is a set of name-value pairs, which are included in HTTP request to send data from web client to web server.

The name and value is separated by "=" character. The name-value pairs are separated by "&" character.

For example: temperature=26&humidity=70&state=2

HTTP Request

The HTTP request is composed of:

  • HTTP request header
  • (Optional) HTTP request body

HTTP request header and HTTP request body is seperated by two pair of a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').

There are many request method. Among them, there are two popular methods: GET and POST.

Usually, we use GET method when we want to get data from web server, and use POST method to send data to web server. However, GET method can be used for either get and send data from/to webserver

Prerequisite

We need to determine the following values:

  • Web Address (URL)
  • Request method (POST or GET)
  • HTTP port that web server uses (most of web server uses port 80 for HTTP)
  • (Optional) Data to be sent to web server (query string). In this tutorial, we suppose that we send temperature and humidity to a web server with format ?temperature={t-value}&humidity={h-value}.

Web Address (URL) is splitted into hostname and pathname

How to Make an HTTP Request

This part presents only code related to HTTP. Full code for each shield will be presents in the last part.

  • Declare request method, HTTP port, hostname, pathname, query string
int HTTP_PORT = 80; String HTTP_METHOD = "GET"; // or "POST" char HOST_NAME[] = "example.phpoc.com"; // hostname of web server: String PATH_NAME = "";

  • Declare a web client object
Arduino Ethernet Shield 2 EthernetClient client;
PHPoC WiFi/Ethernet Shield PhpocClient client;
Arduino Uno WiFi WiFiClient client;
  • Connect to web server
if(client.connect(HOST_NAME, HTTP_PORT)) { Serial.println("Connected to server"); } else { Serial.println("connection failed"); }

  • If connected to server, send HTTP request
// send HTTP request header client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP request header

  • Read the response data from web server
while(client.available()) { // read an incoming byte from the server and print them to serial monitor: char c = client.read(); Serial.print(c); } if(!client.connected()) { // if the server's disconnected, stop the client: Serial.println("disconnected"); client.stop(); }

How to Send Data to a Web Server

We can include data into HTTP request. data format is depended on HTTP request method:

  • HTTP GET request
    • Can only send data in query string format.
    • Data is attached to the pathname.
  • HTTP POST request
    • Can send data NOT ONLY in query string format BUT ALSO any other format such as Json, XML, image ...
    • Data is HTTP request body.

We just need to modify the code for sending the HTTP request:

  • Build query string
int temp = // read from sensor int humi = // read from sensor String queryString = String("?temperature=") + String(temp) + String("&humidity=") + String(humi);

  • Modify the code for sending Data in the HTTP request
    • HTTP GET: append query string to pathname
// send HTTP header client.println("GET " + PATH_NAME + queryString + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header

  • HTTP POST: send query string as HTTP body
// send HTTP header client.println("POST " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header // send HTTP body client.println(queryString);

  • Read the response data from web server
while(client.available()) { // read an incoming byte from the server and print them to serial monitor: char c = client.read(); Serial.print(c); } if(!client.connected()) { // if the server's disconnected, stop the client: Serial.println("disconnected"); client.stop(); }

Complete Arduino Code for Making HTTP Request

The blow is the complete arduino code for making HTTP GET/POST request

Arduino HTTP GET/POST request using Arduino Ethernet Shield 2

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-http-request */ #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("GET " + 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() { }

Arduino HTTP GET/POST request using PHPoC Shields

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-http-request */ #include <Phpoc.h> PhpocClient client; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; // or POST char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger/button-press/with/key/xxxxx"; void setup() { Serial.begin(9600); // initialize PHPoC [WiFi] Shield: Phpoc.begin(); // 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("GET " + 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() { }

Complete Arduino Code for Making HTTP GET Request with data

Arduino HTTP GET request with data using Arduino Ethernet Shield 2

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-http-request */ #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"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger"; String queryString = "?value1=26&value2=70";; 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("GET " + PATH_NAME + queryString + " 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() { }

Arduino HTTP GET request with data using PHPoC Shields

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-http-request */ #include <Phpoc.h> PhpocClient client; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger/button-press/with/key/xxxxx"; String queryString = "?value1=26&value2=70";; void setup() { Serial.begin(9600); // initialize PHPoC [WiFi] Shield: Phpoc.begin(); // 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("GET " + PATH_NAME + queryString + " 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() { }

Complete Arduino Code for Making HTTP POST Request with data

Arduino HTTP POST request with data using Arduino Ethernet Shield 2

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-http-request */ #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"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger"; String queryString = "?value1=26&value2=70";; 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("GET " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header // send HTTP body client.println(queryString); 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() { }

Arduino HTTP POST request with data using PHPoC Shields

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-http-request */ #include <Phpoc.h> PhpocClient client; int HTTP_PORT = 80; String HTTP_METHOD = "GET"; char HOST_NAME[] = "maker.ifttt.com"; String PATH_NAME = "/trigger/button-press/with/key/xxxxx"; String queryString = "?value1=26&value2=70";; void setup() { Serial.begin(9600); // initialize PHPoC [WiFi] Shield: Phpoc.begin(); // 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("GET " + PATH_NAME + " HTTP/1.1"); client.println("Host: " + String(HOST_NAME)); client.println("Connection: close"); client.println(); // end HTTP header // send HTTP body client.println(queryString); 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() { }

※ NOTE THAT:

This tutorial used the dynamic IP address (via DHCP). If you want to use the static IP address:

The Best Arduino Starter Kit

※ OUR MESSAGES