最新消息:

Arduino库教程-Bridge-HTTP Client Console

Arduino 少儿编程 1696浏览 0评论
Arduino库教程

HTTP Client Console(HTTP客户端控制台)

  • 这个yun设备例子展示怎么创建一个基本HTTP客户端(可以连接到互联网,并且下载内容)。在这种情况下,你可以连接Arduino网站,并且下载logo版本(如ASCII 文本)。这个版本使用控制台,并且展示了Arduino IDE 控制台通过wifi连接(不是USB那个)的输出。

  • 选择连到开发板的IP接口,然后当你编译好开发板,打开IDE的串口监视器。

硬件要求

  • Yún 开发板 或者 shield
  • 连接到互联网的无线网络

电路

  • 这个例子没有额外的电路
    Arduino库教程-Bridge-HTTP Client Console
    图由 Fritzing 软件绘制

样例代码

  • 包括Bridge, HttpClient 和 Console 库。
#include <Bridge.h>
#include <HttpClient.h>
#include <Console.h>

[Get Code]

  • 在setup()里,开始Bridge,并且在进入loop()之前等待串口连接。
void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  Console.begin();
  while(!Console);
}

[Get Code]

  • 在loop()里,创建一个命名好的 HttpClient 例子,然后通过 client.get(url) 调用URL。
void loop() {
  HttpClient client;
  client.get("http://www.arduino.cc/asciilogo.txt");

[Get Code]

  • 一旦客户端缓存器有字节,读取这些字节,并且打印到串口监视器。每5秒重复一次。
while (client.available()) {
    char c = client.read();
    Console.print(c);
  }
  Console.flush();

  delay(5000);
}

[Get Code]

  • 完整代码如下:
/*
  Yún HTTP Client Console version for Arduino Uno and Mega using Yún Shield

 This example for the Yún101/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);
}

[Get Code]

更多

  • Bridge: 从网页浏览器进入开发板的引脚。
  • Console ASCII Table: 示范了怎样打印多种格式到控制台。
  • Console Pixel: 通过控制台控制一个LED灯。
  • Console Read: 从控制台那里分析信息,然后重复发送返回。
  • Datalogger: 在SD卡上保存传感器信息。
  • File Write Script: 示范怎样在Process上写入和执行外壳脚本。
  • HTTP Client: 建造一个简单的客户端,可以下载网页并且打印到串口监视器。
  • HTTP Client Console: 建造一个简单的客户端,可以下载网页并且用控制台通过WIFI打印到串口监视器。
  • Mailbox Read Messages: 用REST API通过一个网页发送文本信息到。
  • Process: 示范怎么用Process运行 Linux 命令。
  • Remote Due Blink: 示范怎么远程上传程序到DUE开发板上。
  • Shell Commands: 用Process 来运行 shell 命令。
  • SpacebrewYun: 在Arduino IDE软件的例子上看更多关于 Spacebrew 文档信息。
  • Temboo: 在Arduino IDE软件的例子上看更多关于 Temboo 文档信息。
  • Temperature Web Panel: 当浏览者要求时,粘贴传感数据到网页上。
  • Time Check: 从网络的时间服务器获得时间,并且打印到串口监视器。
  • WiFi Status: 运行一个预配置的脚本,报告返回当前wifi网络的强度。
  • Yun First Config: 用串口监视器不费力地连接你的云产品到wifi网络,并且在上面回答一些简单的问题。
  • Yun Serial Terminal: 通过串口监视器进入Linux终端。

您必须 登录 才能发表评论!