我只是试着让TXT文件包含使用以下代码抛出Arduino以太网屏蔽
原始代码http://arduino.cc/en/Tutorial/WebClientRepeating
/*
Repeating Web client
This sketch connects to a a web server and makes a request
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
This example uses DNS, by assigning the Ethernet client with a MAC address,
IP address, and DNS address.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 19 Apr 2012
by Tom Igoe
http://arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192,168,1,177);
// fill in your Domain Name Server address here:
IPAddress myDns(192,168,1,1);
// initialize the library instance:
EthernetClient client;
char server[] = "192.168.1.100";
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned int postingInterval = 60*1000; // delay between updates, in milliseconds
void setup() {
// start serial port:
Serial.begin(9600);
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac, ip, myDns);
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET /latest.txt HTTP/1.1");
client.println("Host: 192.168.1.100");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}
我在串行监视器上的所有结果
My IP address: 192.168.1.177
connecting...
HTTP/1.1 200 OK
Date: Tue, 16 Sep 2014 14:17:12 GMT
Server: Apache/2.4.9 (Win64) PHP/5.5.12
Last-Modified: Tue, 16 Sep 2014 12:42:19 GMT
ETag: "6-5032e150962be"
Accept-Ranges: bytes
Content-Length: 6
Connection: close
Content-Type: text/plain
123321
disconnecting.
我想使用这样的数据if(strcmp(test,"123321")==0) Serial.println( "OK");
但是当它有http标题我不能,所以我想只得到文本
答案 0 :(得分:0)
将client.println("GET /latest.txt HTTP/1.1");
更改为client.println("GET /latest.txt");
答案 1 :(得分:0)
我的代码完全相似。您需要先将字符串保存在字符串中。所以你的read语句在哪里,你需要一个字符串追加运算符并将“if”更改为“while”,类似
while(client.available())
{
c = client.read();
response = response + c;
}
确保在void loop()外面声明字符串“response”。这将在“响应”中保存服务器的整个响应。之后,您可以应用子字符串函数从“响应”中提取所需的文本。访问Arduino的官方网页,了解“substring”的详细信息。 就我而言,它就像是:
r = (response.substring(165,174));
要找到标题结束和原始响应开始的位置,将会有点痛苦,我建议使用试错法。继续尝试任意字符串索引。每次打印“r”,看看你有多接近。或者你可以计算字符,包括空格和从HTTP 1.1到123321开头的“\ n”。 因此变量“r”将携带您提供给字符串“response”的response.substring(,)的文本b / w索引位置。之后你可以使用“r”来表达你喜欢的目的。
看看我的代码。我希望它有所帮助。 String Comparsion: Arduino C