我正在开发基于网络的家庭自动化系统,因此我的Arduino向服务器发送请求并在串行监视器中获得以下响应以及“loneOn”,这是由于Serial.println(r);
陈述。
HTTP/1.1 200 OK
Date: Mon, 13 Oct 2014 17:46:03 GMT
Server: Apache/2.4.4 (Win32) PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Length: 14
Content-Type: text/html
loneOn.ltwoOn.
loneOn
在另一种情况下,来自服务器的响应将具有 loneOff,而不是 loneOn,我需要确定它到底是哪一个。但这不是现在的重点,我在比较字符串时遇到了困难。 (响应也会在串行监视器中循环,但同样,这不是重点。)
这是 Arduino 的代码:
#include <TextFinder.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <String.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 192,168,137,1 } ;
IPAddress ip(192,168,1,100);
EthernetClient client;
String response = "";
String r = "";
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET /dir/ardu.php HTTP/1.1");
client.println("Host: localhost");
client.println();
}
else {
Serial.println("connection failed");
}
}
void loop(){
char c,s;
while(client.available())
{
c = client.read();
response = response + c;
}
Serial.println(response);
r = (response.substring(165,174));
Serial.println(r);
if (r == "loneOn")
Serial.println("Light 1 is on");
}
问题是:
Serial.println(r);
if (r == "loneOn")
Serial.println("Light 1 is on");
}
不起作用,我的意思是在这里我将字符串“r”与其实际值进行比较,即“loneOn”,它的打印方式与串行监视器中的完全相同,但 if 语句不返回任何内容。我尝试了其他几种比较字符串的方法,但它不起作用。我想知道关于弦乐是否有什么我遗漏的东西。