Learning   Examples | Foundations | Hacking | Links

Examples > HttpClient

HTTP Client

This example for the Arduino Yún shows how create a basic HTTP client that connects to the internet and downloads content. In this case, you'll connect to the Arduino website and download a version of the logo as ASCII text.

Open the Serial Monitor in the IDE once you've programmed the board.

Hardware Required

  • Arduino Yún
  • a wireless network connection to the internet

Circuit

There is no circuit for this example.

Code

Include both the Bridge and HttpClient libraries

#include <Bridge.h>
#include <HttpClient.h>

In setup() start Bridge, and wait for a serial connection before going into loop().

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  Serial.begin(9600);
  while(!Serial);
}

In loop(), create a named instance of HttpClient, and call a URL with client.get(url).

void loop() {
  HttpClient client;
  client.get("http://arduino.cc/asciilogo.txt");

As long as there are bytes from the server in the client buffer, read the bytes and print them to the serial monitor. Repeat every 5 seconds.

while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  Serial.flush();

  delay(5000);
}

The complete sketch is below :


#include <Bridge.h>
#include <HttpClient.h>

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  Serial.begin(9600);
  while(!Serial);
}

void loop() {
  HttpClient client;
  client.get("http://arduino.cc/asciilogo.txt");

  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  Serial.flush();

  delay(5000);
}

 

Share