Arduino中的字符串比较 [英] String comparsion in Arduino

查看:1094
本文介绍了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中的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆