• 从网络得到数据--Arduino+以太网


    ethernet_shield_client

    昨天我们讨论了如何使用Arduino以太网插板建立服务器,并通过网络控制Arduino的引脚。今天我们来看看用插板做为客户端来从一个网页上得到信息并返回报告。我几个月前用的这个方法,当时我做了一个Nixie Twitter follower的计数器被称为Twixie。

    以太网插板可用来访问任何非密码保护的网站,但你要得到信息返回是比较难的一部分。对于Twixie,我创建了一个特殊的php页面,查询twitter API和显示twiter的计数。这让我不必要去告诉Arduino去查找什么,查无数行的HTML寻找一个数字。在我们的示例中会变得更加简单。我创建了一个PHP文件,仅输出一个随机字符串。我这样做是因为要让每个人都设置一个API账户,不需要证明这个概念就可以开始。但是这个想法是,你可以将PHP文件(任何网络可访问文件)编写并显示出你所需要的东西。

    在客户端模式中,以太网插板可以访问网页并将它所读到的信息返回。但是每一次读取一个字节读完整个网页内容。所以它就像大海捞针在大页面上。即使我们正在阅读的页面只包含我们需要的信息,有额外的信息在一开始就被送到arduino。你永远不会看到它,实际上是一个web服务器发送额外的信息作为一个“头”,告诉浏览器有关页面的各种信息(这是不同于HTML标签的)。

    如此,我们需要一种方式来告诉Arduino什么是垃圾,什么是有用的东西。我们可以用<>来包括信息。当Arduino开始读取网页的时候,我们会告诉它忽略所有指导看到"<"。从这点上我们告诉Arduino记录每个下面的字符知道我们看到了结束字符">"。此时,Arduino有所需的一切,断开服务器然后返回报告找到的数据。

    以太网插板库不支持DNS这意味着我们不能直接访问网页,我们需要通过一个IP地址来访问网页。例如,bildr的IP地址是174.123.231.247,你可以访问bildr比如http://174.123.231.247/ ~ bildr / -不是每个服务器允许你这样做,但是通常可以IPADDRESS/~ACCOUNT_USERNAME – 所以你可以看到我在这创建的PHP文件 http://174.123.231.247/~bildr/examples/ethernet/

    arduino-ethernet-client

    代码

    直至Arduino1.0,都支持DHCP,所以可以插入大多数的网络让它工作。

    NOW 1.0 COMPATIBLE

    //ARDUINO 1.0+ ONLY
    //ARDUINO 1.0+ ONLY
    #include <Ethernet.h>
    #include <SPI.h>

    ////////////////////////////////////////////////////////////////////////
    //CONFIGURE
    ////////////////////////////////////////////////////////////////////////
    byte server[] = { 174,123,231,247 }; //ip Address of the server you will connect to

    //The location to go to on the server
    //make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
    String location = "/~bildr/examples/ethernet/ HTTP/1.0";
    // if need to change the MAC address (Very Rare)
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    ////////////////////////////////////////////////////////////////////////

    EthernetClient client;

    char inString[32]; // string for incoming serial data
    int stringPos = 0; // string index counter
    boolean startRead = false; // is reading?

    void setup(){
    Ethernet.begin(mac);
    Serial.begin(9600);
    }

    void loop(){
    String pageValue = connectAndRead(); //connect to the server and read the output

    Serial.println(pageValue); //print out the findings.

    delay(5000); //wait 5 seconds before connecting again
    }

    String connectAndRead(){
    //connect to the server

    Serial.println("connecting...");

    //port 80 is typical of a www page
    if (client.connect(server, 80)) {
    Serial.println("connected");
    client.print("GET ");
    client.println(location);
    client.println();

    //Connected - Read the page
    return readPage(); //go and read the output

    }else{
    return "connection failed";
    }

    }

    String readPage(){
    //read the page, and capture & return everything between '<' and '>'

    stringPos = 0;
    memset( &inString, 0, 32 ); //clear inString memory

    while(true){

    if (client.available()) {
    char c = client.read();

    if (c == '<' ) { //'<' is our begining character
    startRead = true; //Ready to start reading the part
    }else if(startRead){

    if(c != '>'){ //'>' is our ending character
    inString[stringPos] = c;
    stringPos ++;
    }else{
    //got what we need here! We can disconnect now
    startRead = false;
    client.stop();
    client.flush();
    Serial.println("disconnecting.");
    return inString;

    }

    }
    }

    }

    }

    PHP例程文件。创建一个随机的字符串比如<1Hc2f>

    <?php
    //the arduino will store anything between '<' and '>'
    //So if the output was <1kjhghk5> - the arduino would read 1kjhghk5
    //Just generates a random alphanumeric string
    $what_the_arduino_reads = '1'.base_convert(rand(10000,9999999), 10, 36);

    echo '<'.$what_the_arduino_reads.'>';
    ?>

    翻译自:http://bildr.org/2011/06/arduino-ethernet-client/

    感谢阅读!

    更多与我们联系:

    WIZnet邮箱:wiznetbj@wiznet.co.kr


  • 相关阅读:
    Oracle instr() 字符查找函数
    Oracle 中触发器增加存储过程commit问题
    Oracle 记录下jdbc thin client module名称
    sqoop job 实现自动增量导入
    Linux LVM--三种Logic Volume
    Kafka ISR and AR HW 、 LEO
    Kafka Rebalance机制分析
    Kafka 基础操作
    Kafka 通过python简单的生产消费实现
    Kafka为什么速度那么快?该怎么回答
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3243661.html
Copyright © 2020-2023  润新知