# Using Http.getstring() and get wrong data (esp8266)

**URL:** <https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889>\
**Category:** Programming\
**Created:** [December 9, 2021, 9:58am UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889 "2021-12-09T09:58:53Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericmatevosian](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@ericmatevosian](https://forum.arduino.cc/u/ericmatevosian)\
**Post date:** [December 9, 2021, 9:58am UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/1 "2021-12-09T09:58:53Z")

</div>

Hi everyone!  
I'm quite new to esp8266 and HTTP .  
there is a simple iot server which is writted in php. and I installed it on my subdomain.

when I enter the url : "[http://iot.rizelectron.ir/read.php?u=matevosian.eric@gmail.com&p=12345678&r=field\_1](http://iot.rizelectron.ir/read.php?u=matevosian.eric@gmail.com&p=12345678&r=field_1)" in my chrome browser I get what I expect\>\>\> ﻿##DATA-IS:(on)##

but when I try to get this data through my esp8266, I get wrong and irrelevant data with meaningless characters.  
It has been sometimes that I get correct data but after a short time it stated to get wrong data.  
is there any idea? is this code correct?  
I use ESP8266 E01  
this is my code:

```cpp
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

#ifndef STASSID
#define STASSID "your-modem-ssid"
#define STAPSK "your-modem-pass"
#endif

const char* ssid = "Rizelectron";
const char* password = "esteghlaliran";

void setup() {
 pinMode(0,OUTPUT);
 pinMode(2,OUTPUT);
 digitalWrite(0,LOW);
 digitalWrite(2,HIGH);
 Serial.begin(115200);
 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 delay(500);
 Serial.print("wifi connected\n");
}

void loop() {
   WiFiClient client;
   HTTPClient http;
   if (http.begin(client, "http://iot.rizelectron.ir/read.php?u=matevosian.eric@gmail.com&p=12345678&r=field_1")) {  
     int httpCode = http.GET();
     if (httpCode > 0) {
       if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
         String payload = http.getString();
         delay(200);
         Serial.print("Http code: ");
         Serial.println(httpCode);
         Serial.print("\nPayload: ");
         Serial.println(payload);
         if(payload.indexOf("##DATA-IS:(off)##")>0){
           //digitalWrite(0,LOW);
           Serial.print("OFF");
           digitalWrite(2,HIGH);
           http.begin(client, "http://iot.rizelectron.ir/write.php?u=﻿matevosian.eric@gmail.com&p=12345678&f=state_1&v=off");
           http.GET();
         }
         if(payload.indexOf("##DATA-IS:(on)##")>0){
           //digitalWrite(0,HIGH);
           Serial.print("ON");
           digitalWrite(2,LOW);
           http.begin(client, "http://iot.rizelectron.ir/write.php?u=﻿matevosian.eric@gmail.com&p=12345678&f=state_1&v=on");
           http.GET();
         }
         
       }
     }
     http.end();
   }
   delay(10000);
}

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [December 9, 2021, 3:11pm UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/2 "2021-12-09T15:11:10Z")

</div>

> [@ericmatevosian](#):
>
> ```arduino
> Serial.print("Http code: ");
> Serial.println(httpCode);
> Serial.print("\nPayload: ");
> Serial.println(payload);
> 
> ```

What is showing on Serial Monitor? It looks like it should be:

```arduino
Http code: 200

Payload: ##DATA-IS:(on)##

```

What do you get instead?

---

<div class="post-metadata">

**Author:** ![ericmatevosian](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@ericmatevosian](https://forum.arduino.cc/u/ericmatevosian)\
**Post date:** [December 9, 2021, 9:23pm UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/3 "2021-12-09T21:23:44Z")

</div>

Thank you John for your response 🙏

Yea it should be ##DATA-IS:(on)## but I get  
irrelevant characters. Something like this :

Http code: 200

Payload: ?♤¿4□□□\*|a4□■

I don't know what is the problem. Is it possible that the module have problem itself? Cause I thought the problem is in the code..

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [December 9, 2021, 11:32pm UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/4 "2021-12-09T23:32:27Z")

</div>

> [@ericmatevosian](#):
>
> `Payload: ?♤¿4□□□*|a4□■`

It looks like you are following the BasicHttpClient example fairly closely so my guess is that there is something coming from the website that doesn't make sense to the ESP8266 libraries. Maybe looking at the headers would help. Try putting this in between printing "Http code" and "payload".

```arduino
  for (int i=0; i < http.headers(); i++)
  {
    Serial.print(http.headerName(i));
    Serial.print(": "
    Serial.println(http.header(i));
  }

```

---

<div class="post-metadata">

**Author:** ![ericmatevosian](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@ericmatevosian](https://forum.arduino.cc/u/ericmatevosian)\
**Post date:** [December 10, 2021, 6:28am UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/5 "2021-12-10T06:28:09Z")

</div>

Thank you John, I added the code you mentioned but I got nothing between http.code and payload, so I printed http.headers() and saw that it returns 0 , so this is why the for loop is not executed. what does it mean ?

nothing happen when added this between printing "Http code" and "payload" 👇

```cpp
for (int i=0; i < http.headers(); i++)
  {
    Serial.print(http.headerName(i));
    Serial.print(": ");
    Serial.println(http.header(i));
  }

```

---

<div class="post-metadata">

**Author:** ![ericmatevosian](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@ericmatevosian](https://forum.arduino.cc/u/ericmatevosian)\
**Post date:** [December 10, 2021, 10:26am UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/6 "2021-12-10T10:26:53Z")

</div>

things are getting a little bit confusing...  
I don't know why but the same code works now (it's dangerous to don't know the reason) and get the payload below:

> ##DATA-IS:(off)##

but another problem raises now! the term "off" in the mentioned string is actually the value of a variable in the php file, which is updated trough a button in the webserver. when I change the value to "on" the ESP8266 still receives ﻿##DATA-IS:(off)## instead of ﻿##DATA-IS:(on)##.. but when I check the URL through a browser like Chrome it shows ﻿##DATA-IS:(on)##..  
at this moment check this [URL](http://iot.rizelectron.ir/read.php?u=matevosian.eric@gmail.com&p=12345678&r=field_1) and you will see it returns the value ﻿##DATA-IS:(on)##

what is the possibilities ??

---

<div class="post-metadata">

**Author:** ![homefoodi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/homefoodi/32/710181_2.png) [@homefoodi](https://forum.arduino.cc/u/homefoodi)\
**Post date:** [December 10, 2021, 10:31am UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/7 "2021-12-10T10:31:29Z")

</div>

Nice, you have provided good explanation

---

<div class="post-metadata">

**Author:** ![KASSIMSAMJI](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kassimsamji/32/261751_2.png) [@KASSIMSAMJI](https://forum.arduino.cc/u/KASSIMSAMJI)\
**Post date:** [December 10, 2021, 10:50am UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/8 "2021-12-10T10:50:03Z")

</div>

> [@ericmatevosian](#):
>
> what is the possibilities ??

caching issues ?

you might want to try [POST](https://techtutorialsx.com/2016/07/21/esp8266-post-requests/) ?

---

<div class="post-metadata">

**Author:** ![ericmatevosian](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@ericmatevosian](https://forum.arduino.cc/u/ericmatevosian)\
**Post date:** [December 10, 2021, 11:01am UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/9 "2021-12-10T11:01:20Z")

</div>

Thank you for your reply, would you please explain a little more?

I want to get the correct value of the url response.. but it always returns wrong using mentioned Arduino code in ESP8266.. I've also tried some python code to check the url and it works fine there...

---

<div class="post-metadata">

**Author:** ![ericmatevosian](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@ericmatevosian](https://forum.arduino.cc/u/ericmatevosian)\
**Post date:** [December 10, 2021, 4:20pm UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/10 "2021-12-10T16:20:33Z")

</div>

I tried to find out why the value of http.headers() is equal to zero! I could not make it!  
I think all of the mentioned problems above comes from here... http.headers() = 0

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [December 10, 2021, 5:26pm UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/11 "2021-12-10T17:26:30Z")

</div>

> [@ericmatevosian](#):
>
> when I change the value to "on" the ESP8266 still receives ﻿##DATA-IS:(off)##

My guess is that you were getting the contents of some buffer that formerly contained garbage and now contains "##DATA-IS:(off)##". If you reset the ESP8266 does it still receive "##DATA-IS:(off)##"?

---

<div class="post-metadata">

**Author:** ![ericmatevosian](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@ericmatevosian](https://forum.arduino.cc/u/ericmatevosian)\
**Post date:** [December 11, 2021, 4:19am UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/12 "2021-12-11T04:19:47Z")

</div>

Thank you John, the module reset don't help, and still get ##DATA-IS(off)##, but some times after long time (few hours) if I change the state to "ON" and after that turn on the module it gets ##DATA-IS(on)##.. and here we go.. it starts to show ##DATA-IS(on)## and don't change to off...

Confusing 😪

---

<div class="post-metadata">

**Author:** ![KASSIMSAMJI](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kassimsamji/32/261751_2.png) [@KASSIMSAMJI](https://forum.arduino.cc/u/KASSIMSAMJI)\
**Post date:** [December 11, 2021, 12:21pm UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/13 "2021-12-11T12:21:00Z")

</div>

possibly a cashing issue from server to a specific client ?, yeah It happens most of the time with GET request

change your PHP to receive POST and follow the guide on the link I did post to change your ESP script to using POST

---

<div class="post-metadata">

**Author:** ![ericmatevosian](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@ericmatevosian](https://forum.arduino.cc/u/ericmatevosian)\
**Post date:** [December 12, 2021, 1:23pm UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/14 "2021-12-12T13:23:39Z")

</div>

thank you so much! It solved my problem 😍 I didn't know that it's possible to receive URL payload by POST method.. I've read a little more about POST and GET methods, we can send and receive data with HTTP protocol by both of them.. right??

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 10, 2022, 1:23pm UTC](https://forum.arduino.cc/t/using-http-getstring-and-get-wrong-data-esp8266/933889/15 "2022-06-10T13:23:54Z")

</div>

Closed on June 10, 2022, 1:23pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
