Arduino Yún HTTP Client Console

Create a simple client that downloads a webpage and prints it to the serial monitor via WiFi using Console.

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. This version uses Console and shows the output on your Arduino Software (IDE) Console through a WiFi connection and not the USB one.

Select the IP port as connection to your board and 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 the Bridge, HttpClient and Console libraries

1#include <Bridge.h>
2#include <HttpClient.h>
3#include <Console.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 Console.begin();
10
11 while(!Console);
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 Console.print(c);
6
7 }
8
9 Console.flush();
10
11 delay(5000);
12}

The complete sketch is below :

1/*
2
3 Yún HTTP Client Console version for Arduino Uno and Mega using Yún Shield
4
5 This example for the YunShield/Yún shows how create a basic
6
7 HTTP client that connects to the internet and downloads
8
9 content. In this case, you'll connect to the Arduino
10
11 website and download a version of the logo as ASCII text.
12
13 created by Tom igoe
14
15 May 2013
16
17 modified by Marco Brianza to use Console
18
19 This example code is in the public domain.
20
21 http://www.arduino.cc/en/Tutorial/HttpClient
22
23 */
24
25#include <Bridge.h>
26#include <HttpClient.h>
27#include <Console.h>
28
29void setup() {
30
31 // Bridge takes about two seconds to start up
32
33 // it can be helpful to use the on-board LED
34
35 // as an indicator for when it has initialized
36
37 pinMode(13, OUTPUT);
38
39 digitalWrite(13, LOW);
40
41 Bridge.begin();
42
43 digitalWrite(13, HIGH);
44
45 Console.begin();
46
47 while (!Console); // wait for a serial connection
48}
49
50void loop() {
51
52 // Initialize the client library
53
54 HttpClient client;
55
56 // Make a HTTP request:
57
58 client.get("http://www.arduino.cc/asciilogo.txt");
59
60 // if there are incoming bytes available
61
62 // from the server, read them and print them:
63
64 while (client.available()) {
65
66 char c = client.read();
67
68 Console.print(c);
69
70 }
71
72 Console.flush();
73
74 delay(5000);
75}

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.