HTTP Client
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
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://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();
Serial.print(c);
}
Serial.flush();
delay(5000);
}
The complete sketch is below :
/*
Yún HTTP Client
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
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/HttpClient
*/
#include <Bridge.h>
#include <HttpClient.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);
SerialUSB.begin(9600);
while (!SerialUSB); // 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();
SerialUSB.print(c);
}
SerialUSB.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 Console - HTTP client that connects, downloads content and shows it using WiFi and Console.
- 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