Arduino从网页获取数据

时间:2014-01-04 15:43:06

标签: arduino

我正在尝试将我的Arduino uno +以太网屏蔽连接到php脚本,该脚本从数据库获取值,然后发回,然后显示在串行监视器上。它工作,它成功连接,我得到了发回的值,但我在串行监视器上显示它时遇到问题。它应该只显示服务器发送的内容,但不显示。任何人都可以帮忙吗?

串行输出:它应该只输出“值”,但是有些数字不应该存在。如果我把它输出到液晶显示器,我就不能让它们出现。

connecting...
connected
HTTP/1.1 200 OK
Server: cloudflare-nginx
Date: Sat, 04 Jan 2014 15:36:51 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Set-Cookie: __cfduid=dcef101052b82760c1a2de019e6b076141388849811461; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.linku.biz; HttpOnly
X-Powered-By: PHP/5.3.27
CF-RAY: e7901b6dec606e2

4
The 
5
Value
0


disconnecting.

PHP脚本

<?php
echo 'The Value';
?>

Arduino脚本

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };


char server[] = "www.linku.biz";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /arduino.php HTTP/1.1");
    client.println("Host: www.linku.biz");
    client.println("Connection: close");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

2 个答案:

答案 0 :(得分:3)

不知道这是否有帮助,但请注意这些数字是读出的可用字符数。 4“The”和5“value”

我希望client.available在收到这些字符之后返回4和5的值。他们如何转换为ASCI“4”和“5”并打印出来,我不知道。

答案 1 :(得分:0)

我通过这段代码解析数据

String readString;

   //gets byte from ethernet buffer
    readString += client.read(); //places captured byte in 

 //parse readString for request
    index = readString.indexOf("text"); //finds location of first "text"
    data_want = readString.substring(index+some char, index+some char); //captures data String

    Serial.println(data_wand);