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

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 : "" 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:

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

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

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

void setup() {
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 Serial.print("wifi connected\n");

void loop() {
   WiFiClient client;
   HTTPClient http;
   if (http.begin(client, "")) {  
     int httpCode = http.GET();
     if (httpCode > 0) {
       if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
         String payload = http.getString();
         Serial.print("Http code: ");
         Serial.print("\nPayload: ");
           http.begin(client, "");
           http.begin(client, "");

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

Http code: 200

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

What do you get instead?

1 Like

Thank you John for your response :pray:

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..

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".

  for (int i=0; i < http.headers(); i++)
    Serial.print(": "
1 Like

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" :point_down:

for (int i=0; i < http.headers(); i++)
    Serial.print(": ");

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:


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 and you will see it returns the value ##DATA-IS:(on)##

what is the possibilities ??

Nice, you have provided good explanation

1 Like

caching issues ?

you might want to try POST ?

1 Like

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...

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

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)##"?

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 :sleepy:

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

1 Like

thank you so much! It solved my problem :heart_eyes: 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??

1 Like

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