The code for other WiFi or Ethernet Shield/Board are similar. The difference is only in library
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:
Your PC/smartphone (as a web client) makes an HTTP request to a web server
The web server returns an HTTP response to your PC/smartphone
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.
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.
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
The HTTP request is composed of:
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
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
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";
char HOST_NAME[] = "example.phpoc.com";
String PATH_NAME = "";
| |
Arduino Ethernet Shield | EthernetClient client; |
Arduino Uno WiFi | WiFiClient client; |
if(client.connect(HOST_NAME, HTTP_PORT)) {
Serial.println("Connected to server");
} else {
Serial.println("connection failed");
}
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
while(client.available())
{
char c = client.read();
Serial.print(c);
}
if(!client.connected())
{
Serial.println("disconnected");
client.stop();
}
We can include data into HTTP request. data format is depended on HTTP request method:
HTTP GET request
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:
int temp =
int humi =
String queryString = String("?temperature=") + String(temp) + String("&humidity=") + String(humi);
Modify the code for sending Data in the HTTP request
client.println("GET " + PATH_NAME + queryString + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
client.println("POST " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
client.println(queryString);
while(client.available())
{
char c = client.read();
Serial.print(c);
}
if(!client.connected())
{
Serial.println("disconnected");
client.stop();
}
The blow is the complete arduino code for making HTTP GET/POST request
#include <WiFiS3.h>
const char ssid[] = "YOUR_WIFI_SSID";
const char pass[] = "YOUR_WIFI_PASSWORD";
WiFiClient client;
int status = WL_IDLE_STATUS;
int HTTP_PORT = 80;
String HTTP_METHOD = "GET";
char HOST_NAME[] = "maker.ifttt.com";
String PATH_NAME = "/trigger";
void setup() {
Serial.begin(9600);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true)
;
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
if (client.connect(HOST_NAME, HTTP_PORT)) {
Serial.println("Connected to server");
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println();
Serial.println("disconnected");
} else {
Serial.println("connection failed");
}
}
void loop() {
}
#include <SPI.h>
#include <Ethernet.h>
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";
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address using DHCP");
while(true);
}
if(client.connect(HOST_NAME, HTTP_PORT)) {
Serial.println("Connected to server");
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
while(client.connected()) {
if(client.available()){
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println();
Serial.println("disconnected");
} else {
Serial.println("connection failed");
}
}
void loop() {
}
#include <WiFiS3.h>
const char ssid[] = "YOUR_WIFI_SSID";
const char pass[] = "YOUR_WIFI_PASSWORD";
WiFiClient client;
int status = WL_IDLE_STATUS;
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);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true)
;
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
if (client.connect(HOST_NAME, HTTP_PORT)) {
Serial.println("Connected to server");
client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println();
Serial.println("disconnected");
} else {
Serial.println("connection failed");
}
}
void loop() {
}
#include <SPI.h>
#include <Ethernet.h>
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);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address using DHCP");
while(true);
}
if(client.connect(HOST_NAME, HTTP_PORT)) {
Serial.println("Connected to server");
client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
while(client.connected()) {
if(client.available()){
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println();
Serial.println("disconnected");
} else {
Serial.println("connection failed");
}
}
void loop() {
}
#include <WiFiS3.h>
const char ssid[] = "YOUR_WIFI_SSID";
const char pass[] = "YOUR_WIFI_PASSWORD";
WiFiClient client;
int status = WL_IDLE_STATUS;
int HTTP_PORT = 80;
String HTTP_METHOD = "POST";
char HOST_NAME[] = "maker.ifttt.com";
String PATH_NAME = "/trigger";
String queryString = "?value1=26&value2=70";
;
void setup() {
Serial.begin(9600);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true)
;
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
if (client.connect(HOST_NAME, HTTP_PORT)) {
Serial.println("Connected to server");
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
client.println(queryString);
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println();
Serial.println("disconnected");
} else {
Serial.println("connection failed");
}
}
void loop() {
}
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
int HTTP_PORT = 80;
String HTTP_METHOD = "POST";
char HOST_NAME[] = "maker.ifttt.com";
String PATH_NAME = "/trigger";
String queryString = "?value1=26&value2=70";;
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address using DHCP");
while(true);
}
if(client.connect(HOST_NAME, HTTP_PORT)) {
Serial.println("Connected to server");
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
client.println(queryString);
while(client.connected()) {
if(client.available()){
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println();
Serial.println("disconnected");
} else {
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:
WARNING
Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.