字符串Comparsion:Arduino的Ç [英] String Comparsion: Arduino C

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

问题描述

我关于基于网络家庭自动化系统工作,所以我的Arduino发送一个请求给服务器,并得到在串行监控下列反应,以loneOn,这是由于Serial.println(r)的沿;声明。

  HTTP / 1.1 200 OK
日期:星期一,2014年10月13日17时46分03秒GMT
服务器:Apache / 2.4.4(Win32的)PHP / 5.4.16
的X已启动方式:PHP / 5.4.16
内容长度:14
内容类型:text / html的loneOn.ltwoOn。
loneOn

在另一种情况下,从服务器的响应将有loneOff,而不是loneOn,我需要决定哪一个它到底是。但是,这不是问题的关键,现在,我有比较字符串的麻烦。(另外,反应将在串口监视器循环,但同样,这不是问题的关键。)
这是code为Arduino的,

 的#include< TextFinder.h>
#包括LT&;&Dhcp.h GT;
#包括LT&;&Dns.h GT;
#包括LT&;&Ethernet.h GT;
#包括LT&;&EthernetClient.h GT;
#包括LT&;&EthernetServer.h GT;
#包括LT&;&EthernetUdp.h GT;
#包括LT&;&util.h GT;
#包括LT&;&STRING.H GT;
#包括LT&;&spi.h中GT;
#包括LT&;&Ethernet.h GT;
字节的MAC [] = {写0xDE,写入0xAD,0xBE,0xEF,0xFE时,0xED};
字节服务器[] = {192,168,137,1};
ip地址IP(192,168,1,100);
EthernetClient客户端;
串响应=;
串R =;无效设置(){
    Serial.begin(9600);
    而(!串行){
        ;
    }    如果(Ethernet.begin(MAC)== 0){
        Serial.println(无法使用DHCP来配置以太网);
        Ethernet.begin(MAC,IP);
    }    延迟(1000);
    Serial.println(连接...);    如果(client.connect(服务器,80)){
        Serial.println(连接);        client.println(GET /dir/ardu.php HTTP / 1.1);
        client.println(主机:本地主机);
        client.println();    }
    其他{
        Serial.println(连接失败);
    }
}空隙环(){
    焦C,S;    而(client.available())
    {
        C = client.read();
        响应=响应+ C;
    }    Serial.println(响应);
    R =(response.substring(165174));
    Serial.println(R);    如果(R ==loneOn)
        Serial.println(光1上);
}

问题是,

  Serial.println(R);
如果(R ==loneOn)
    Serial.println(光1上);
}

不工作,我的意思是,我与它的真正价值是什么即loneOn,而这正是印刷,因为它是在串行显示器比较字符串'R',但if语句返回任何内容。我曾尝试比较字符串的其他几个方法,但它不工作。我想知道如果有什么我缺少对字符串。


解决方案

  R =(response.substring(165174));

我用错了指数

,它应该开始于167.这意味着有空格或\\ nS认为是导致字符串不匹配与给定值。

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

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.) 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");
}

The problem is,

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

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)); 

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.

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

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