首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino从网页获取数据

Arduino从网页获取数据
EN

Stack Overflow用户
提问于 2014-01-04 23:43:06
回答 2查看 13.7K关注 0票数 1

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

串行输出:它应该只输出“值”,但是有一些数字不应该在那里。如果我把这个输出到LCD显示器上,我就不能让它们显示数字了。

代码语言:javascript
复制
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脚本

代码语言:javascript
复制
<?php
echo 'The Value';
?>

Arduino脚本

代码语言:javascript
复制
#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);
  }
}
EN

回答 2

Stack Overflow用户

发布于 2014-01-05 08:41:01

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

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

票数 3
EN

Stack Overflow用户

发布于 2021-01-21 15:24:14

我通过下面的代码来解析数据

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20923003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档