Arduino Yún HTTP Client

Create a simple client that downloads a webpage and prints it to the serial monitor.

This example for a Yún device 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

  • Yún board or shield

  • a wireless network connection to the internet

Circuit

There is no circuit for this example.

The circuit for this tutorial.
The circuit for this tutorial.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

Include both the Bridge and HttpClient libraries

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

In

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

1void setup() {
2
3 pinMode(13, OUTPUT);
4
5 digitalWrite(13, LOW);
6
7 Bridge.begin();
8
9 Serial.begin(9600);
10
11 while(!Serial);
12}

In

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

1void loop() {
2
3 HttpClient client;
4
5 client.get("http://www.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.

1while (client.available()) {
2
3 char c = client.read();
4
5 Serial.print(c);
6
7 }
8
9 Serial.flush();
10
11 delay(5000);
12}

The complete sketch is below :

Last revision 2016/05/25 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.