HTTP Client 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.

Yun Fritzing

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

Code

Include the Bridge, HttpClient and Console libraries

#include <Bridge.h>
#include <HttpClient.h>
#include <Console.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();

  Console.begin();

  while(!Console);
}

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

void loop() {

  HttpClient client;

  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.

while (client.available()) {

    char c = client.read();

    Console.print(c);

  }

  Console.flush();

  delay(5000);
}

The complete sketch is below :


/*

  Yún HTTP Client Console version for Arduino Uno and Mega using Yún Shield

 This example for the YunShield/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.

 created by Tom igoe

 May 2013

 modified by Marco Brianza to use Console

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/HttpClient

 */

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

void setup() {

  // Bridge takes about two seconds to start up

  // it can be helpful to use the on-board LED

  // as an indicator for when it has initialized

  pinMode(13, OUTPUT);

  digitalWrite(13, LOW);

  Bridge.begin();

  digitalWrite(13, HIGH);

  Console.begin();

  while (!Console); // wait for a serial connection
}

void loop() {

  // Initialize the client library

  HttpClient client;

  // Make a HTTP request:

  client.get("http://www.arduino.cc/asciilogo.txt");

  // if there are incoming bytes available

  // from the server, read them and print them:

  while (client.available()) {

    char c = client.read();

    Console.print(c);

  }

  Console.flush();

  delay(5000);
}

See Also

  • Bridge Library - Your reference to the Bridge Library

  • Bridge - Simple REST style calls to access analog and digital pins

  • Console Ascii Table - A complete ASCII table printed to the Console

  • Console Pixel - Turn an LED on and off through the Console

  • Console Read - Read data coming from bridge using the Console.read() function

  • Data Logger - Log data from three analog sensors to an SD card.

  • File Write - How to write file into the Yún filesystem.

  • Http Client - A basic HTTP client that connects to the internet and downloads content.

  • Mailbox Read Message - How to read the messages queue, called Mailbox, using the Bridge library.

  • Process - How to run linux processes using an Yún.

  • Remote Due Blink - How to upload remotely a sketch on DUE boards.

  • Shell Commands - How to run linux shell commands using a Yún.

  • Temperature Web Panel - How to serve data from an analog input via the Yún's built-in webserver.

  • Time check - Gets the time from Linux via Bridge then parses out hours, minutes and seconds.

  • WiFi Status - Prints information about the status of your wifi connection.

  • Yún First Configuration - Easily configure your Yún device using Serial Monitor and USB port.

  • Serial Terminal - Use the Yún's 32U4 processor as a serial terminal for the Linux side on the Yún.

Last revision 2016/05/25 by SM