String comparsion in Arduino












0















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.










share|improve this question

























  • Please indent your code.

    – Jabberwocky
    Oct 13 '14 at 18:02













  • Maybe there are unprintable trailing characters in r. A typical candidate would be n.

    – Jabberwocky
    Oct 13 '14 at 18:08











  • I used substring function on the server response, so from "loneOn.ltwoOn." only "loneOn" is extracted. I thought the same as you said, initially, so in the response from the server i added "." so there is no confusion. The Serial.println(r) statement prints it only as "loneOn", if there was even one more character it would be "." :)

    – Sarosh
    Oct 13 '14 at 18:13











  • @blacai Thanks.

    – Sarosh
    Oct 13 '14 at 18:28











  • Thanks @MichaelWalz there were actually a couple of "n"s, but they were at the beginning so i didn't notice.

    – Sarosh
    Oct 14 '14 at 18:53
















0















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.










share|improve this question

























  • Please indent your code.

    – Jabberwocky
    Oct 13 '14 at 18:02













  • Maybe there are unprintable trailing characters in r. A typical candidate would be n.

    – Jabberwocky
    Oct 13 '14 at 18:08











  • I used substring function on the server response, so from "loneOn.ltwoOn." only "loneOn" is extracted. I thought the same as you said, initially, so in the response from the server i added "." so there is no confusion. The Serial.println(r) statement prints it only as "loneOn", if there was even one more character it would be "." :)

    – Sarosh
    Oct 13 '14 at 18:13











  • @blacai Thanks.

    – Sarosh
    Oct 13 '14 at 18:28











  • Thanks @MichaelWalz there were actually a couple of "n"s, but they were at the beginning so i didn't notice.

    – Sarosh
    Oct 14 '14 at 18:53














0












0








0








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.










share|improve this question
















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.







string arduino comparison






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 0:35









gre_gor

4,12892631




4,12892631










asked Oct 13 '14 at 17:50









SaroshSarosh

236




236













  • Please indent your code.

    – Jabberwocky
    Oct 13 '14 at 18:02













  • Maybe there are unprintable trailing characters in r. A typical candidate would be n.

    – Jabberwocky
    Oct 13 '14 at 18:08











  • I used substring function on the server response, so from "loneOn.ltwoOn." only "loneOn" is extracted. I thought the same as you said, initially, so in the response from the server i added "." so there is no confusion. The Serial.println(r) statement prints it only as "loneOn", if there was even one more character it would be "." :)

    – Sarosh
    Oct 13 '14 at 18:13











  • @blacai Thanks.

    – Sarosh
    Oct 13 '14 at 18:28











  • Thanks @MichaelWalz there were actually a couple of "n"s, but they were at the beginning so i didn't notice.

    – Sarosh
    Oct 14 '14 at 18:53



















  • Please indent your code.

    – Jabberwocky
    Oct 13 '14 at 18:02













  • Maybe there are unprintable trailing characters in r. A typical candidate would be n.

    – Jabberwocky
    Oct 13 '14 at 18:08











  • I used substring function on the server response, so from "loneOn.ltwoOn." only "loneOn" is extracted. I thought the same as you said, initially, so in the response from the server i added "." so there is no confusion. The Serial.println(r) statement prints it only as "loneOn", if there was even one more character it would be "." :)

    – Sarosh
    Oct 13 '14 at 18:13











  • @blacai Thanks.

    – Sarosh
    Oct 13 '14 at 18:28











  • Thanks @MichaelWalz there were actually a couple of "n"s, but they were at the beginning so i didn't notice.

    – Sarosh
    Oct 14 '14 at 18:53

















Please indent your code.

– Jabberwocky
Oct 13 '14 at 18:02







Please indent your code.

– Jabberwocky
Oct 13 '14 at 18:02















Maybe there are unprintable trailing characters in r. A typical candidate would be n.

– Jabberwocky
Oct 13 '14 at 18:08





Maybe there are unprintable trailing characters in r. A typical candidate would be n.

– Jabberwocky
Oct 13 '14 at 18:08













I used substring function on the server response, so from "loneOn.ltwoOn." only "loneOn" is extracted. I thought the same as you said, initially, so in the response from the server i added "." so there is no confusion. The Serial.println(r) statement prints it only as "loneOn", if there was even one more character it would be "." :)

– Sarosh
Oct 13 '14 at 18:13





I used substring function on the server response, so from "loneOn.ltwoOn." only "loneOn" is extracted. I thought the same as you said, initially, so in the response from the server i added "." so there is no confusion. The Serial.println(r) statement prints it only as "loneOn", if there was even one more character it would be "." :)

– Sarosh
Oct 13 '14 at 18:13













@blacai Thanks.

– Sarosh
Oct 13 '14 at 18:28





@blacai Thanks.

– Sarosh
Oct 13 '14 at 18:28













Thanks @MichaelWalz there were actually a couple of "n"s, but they were at the beginning so i didn't notice.

– Sarosh
Oct 14 '14 at 18:53





Thanks @MichaelWalz there were actually a couple of "n"s, but they were at the beginning so i didn't notice.

– Sarosh
Oct 14 '14 at 18:53












2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer































    0














    Try



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


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






    share|improve this answer
























    • Actually, i found the problem. It was in the substring statement. r = (response.substring(165,174)); I was using the wrong index, it was supposed to begin at 167, its like what @Michael Walz said, what i missed was the fact that it was at the beginning of the string and not at the end. And previously i did try the method you proposed. Thanks for your response.

      – Sarosh
      Oct 14 '14 at 18:51











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26345912%2fstring-comparsion-in-arduino%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 14 '14 at 18:58









        SaroshSarosh

        236




        236

























            0














            Try



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


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






            share|improve this answer
























            • Actually, i found the problem. It was in the substring statement. r = (response.substring(165,174)); I was using the wrong index, it was supposed to begin at 167, its like what @Michael Walz said, what i missed was the fact that it was at the beginning of the string and not at the end. And previously i did try the method you proposed. Thanks for your response.

              – Sarosh
              Oct 14 '14 at 18:51
















            0














            Try



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


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






            share|improve this answer
























            • Actually, i found the problem. It was in the substring statement. r = (response.substring(165,174)); I was using the wrong index, it was supposed to begin at 167, its like what @Michael Walz said, what i missed was the fact that it was at the beginning of the string and not at the end. And previously i did try the method you proposed. Thanks for your response.

              – Sarosh
              Oct 14 '14 at 18:51














            0












            0








            0







            Try



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


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






            share|improve this answer













            Try



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


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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 14 '14 at 17:48









            PEARPEAR

            3171514




            3171514













            • Actually, i found the problem. It was in the substring statement. r = (response.substring(165,174)); I was using the wrong index, it was supposed to begin at 167, its like what @Michael Walz said, what i missed was the fact that it was at the beginning of the string and not at the end. And previously i did try the method you proposed. Thanks for your response.

              – Sarosh
              Oct 14 '14 at 18:51



















            • Actually, i found the problem. It was in the substring statement. r = (response.substring(165,174)); I was using the wrong index, it was supposed to begin at 167, its like what @Michael Walz said, what i missed was the fact that it was at the beginning of the string and not at the end. And previously i did try the method you proposed. Thanks for your response.

              – Sarosh
              Oct 14 '14 at 18:51

















            Actually, i found the problem. It was in the substring statement. r = (response.substring(165,174)); I was using the wrong index, it was supposed to begin at 167, its like what @Michael Walz said, what i missed was the fact that it was at the beginning of the string and not at the end. And previously i did try the method you proposed. Thanks for your response.

            – Sarosh
            Oct 14 '14 at 18:51





            Actually, i found the problem. It was in the substring statement. r = (response.substring(165,174)); I was using the wrong index, it was supposed to begin at 167, its like what @Michael Walz said, what i missed was the fact that it was at the beginning of the string and not at the end. And previously i did try the method you proposed. Thanks for your response.

            – Sarosh
            Oct 14 '14 at 18:51


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26345912%2fstring-comparsion-in-arduino%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks