String Comparsion:Arduino C.

我正在开发基于Web的家庭自动化系统,所以我的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  #include  #include  #include  #include  #include  #include  #include  #include  #include  #include  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语句什么都不返回。 我已经尝试了几种其他比较字符串的方法,但它不起作用。 我想知道是否有关于弦乐的遗漏。

 r = (response.substring(165,174)); 

我使用了错误的索引,它应该从167开始。这意味着有空格或“\ n”s导致字符串与给定值不匹配。

尝试

 if(r.equals("loneOn")) { Serial.println("Light 1 is on"); } 

http://arduino.cc/en/Reference/StringEquals