本文介绍了Arduino 中的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于 Web 的家庭自动化系统,因此我的 Arduino 向服务器发送请求并在串行监视器中获得以下响应以及loneOn",这是由于 Serial.println(r); 语句.

I am working on web based Home Automation System, so my Arduino sends a request to the server and gets the following response in serial monitor, along with "loneOn", which is due to Serial.println(r); statement.

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,我需要确定它到底是哪个.但这不是现在的重点,我在比较字符串时遇到了麻烦.(响应也会在串行监视器中循环,但同样,这不是重点.)

In another case the response from the server will have loneOff, instead of loneOn, I need to decide which one it exactly is. But that's not the point right now, I am having trouble comparing the Strings. (Also the response would loop in the serial monitor, but again, that's not the point.)

这是Arduino的代码:

This is the code for 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 语句不返回任何内容.我尝试了其他几种比较字符串的方法,但它不起作用.我想知道关于字符串是否有任何我遗漏的地方.

Doesn't work, I mean here I am comparing the String 'r' with what its real value is i.e "loneOn", which is printed exactly as it is in the serial monitor, but the if statement returns nothing. I have tried several other methods of comparing Strings but it doesn't work. I wanted to know if there's anything I am missing about the Strings.

推荐答案

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

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

I was using the wrong index, it was supposed to begin at 167. Which means there were blank spaces or "\n"s that were causing the string not to match with the given value.

这篇关于Arduino 中的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:33