HTTPClient works but

Hello i have a problem with my code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
WiFiClient client;
 const char* ssid = "WTC";
 const char* password = "****";

 int device1_pin = D0;
 int device2_pin = D1;
 int device3_pin = D2;
 int device4_pin = D3;
 int device5_pin = D4;
 int device6_pin = D5;

 int device_status1;
 int device_status2;
 int device_status3;
 int device_status4;
 int device_status5;
 int device_status6;
 
void setup () {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting..");
  }
  
  pinMode(device1_pin,OUTPUT);
  pinMode(device2_pin,OUTPUT);
  pinMode(device3_pin,OUTPUT);
  pinMode(device4_pin,OUTPUT);
  pinMode(device5_pin,OUTPUT);
  pinMode(device6_pin,OUTPUT);
}
 
void loop() {
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
    WiFiClientSecure client;
    
client.setInsecure();
    HTTPClient http;  //Declare an object of class HTTPClient
    /*----------------------------------------------*/

    http.begin(client,"https://*******/data/device_status.php?did=1");  //Specify request destination
    int httpCode1 = http.GET();                                          //Send the request
    if (httpCode1 > 0) { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      device_status1 = payload.toInt();
    //Serial.print(httpCode1);
    Serial.println();
    Serial.print(payload);
    Serial.println();
      Serial.print("Device 1 Status=");
      Serial.print(device_status1); //Print the response payload
      Serial.println();
      delay(1500);
    }
    
    /*----------------------------------------------*/
    
    http.begin(client,"****datta/device_status.php?did=2");  //Specify request destination
    int httpCode2 = http.GET();                                          //Send the request
    if (httpCode2 > 0) { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      device_status2 = payload.toInt();
      Serial.println();
      Serial.print(payload);
      Serial.println();
      Serial.print("Device 2 Status=");
      Serial.print(device_status2); //Print the response payload
      Serial.println();
      delay(1500);
    }

    /*----------------------------------------------*/
    
    http.begin(client,"https://********/data/device_status.php?did=3");  //Specify request destination
    int httpCode3 = http.GET();                                          //Send the request
    if (httpCode3 > 0) { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      device_status3 = payload.toInt();
      Serial.println();
      Serial.print(payload);
      Serial.println();
      Serial.print("Device 3 Status=");
    Serial.print(device_status3); //Print the response payload
     Serial.println();
     delay(1500);
    }

    /*----------------------------------------------*/
    
    http.begin(client,"https://*****/data/device_status.php?did=4");  //Specify request destination
    int httpCode4 = http.GET();                                          //Send the request
    if (httpCode4 > 0) { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      device_status4 = payload.toInt();
      Serial.println();
      Serial.print(payload);
      Serial.println();
      Serial.print("Device 4 Status=");
     Serial.print(device_status4); //Print the response payload
     Serial.println();
     delay(1500);
    }
    
        http.begin(client,"https://*******/data/device_status.php?did=5");  //Specify request destination
    int httpCode5 = http.GET();                                          //Send the request
    if (httpCode5 > 0) { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      device_status5 = payload.toInt();
      Serial.println();
      Serial.print(payload);
      Serial.println();
     Serial.print("Device 5 Status=");
     Serial.print(device_status5); //Print the response payload
     Serial.println();
     delay(1500);
    }
    
        http.begin(client,"https://*******/data/device_status.php?did=6");  //Specify request destination
    int httpCode6 = http.GET();                                          //Send the request
    if (httpCode6 > 0) { //Check the returning code
      String payload = http.getString();   //Get the request response payload
      device_status6 = payload.toInt();
      Serial.println();
      Serial.print(payload);
      Serial.println();
      Serial.print("Device 6 Status=");
      Serial.print(device_status6); //Print the response payload
     Serial.println();
     delay(1500);

    }
    
    http.end();   //Close connection
  }
  delay(5500);
  if(device_status1 == 1){
     digitalWrite(device1_pin,LOW);
     Serial.println("Device 1 ON");  
  }else if(device_status1 == 0){
     digitalWrite(device1_pin,HIGH);
     Serial.println("Device 1 OFF");
  }
  
  if(device_status2 == 1){
     digitalWrite(device2_pin,LOW); 
     Serial.println("Device 2 ON"); 
  }else if(device_status2 == 0){
     digitalWrite(device2_pin,HIGH);
     Serial.println("Device 2 OFF");
  }
  
  if(device_status3 == 1){
     digitalWrite(device3_pin,LOW);
     Serial.println("Device 3 ON");  
  }else if(device_status3 == 0){
     digitalWrite(device3_pin,HIGH);
     Serial.println("Device 3 OFF");
  }
  
  if(device_status4 == 1){
     digitalWrite(device4_pin,LOW);  
     Serial.println("Device 4 ON");
  }else if(device_status4 == 0){
     digitalWrite(device4_pin,HIGH);
     Serial.println("Device 4 OFF");
  }
  
    if(device_status5 == 1){
     digitalWrite(device5_pin,LOW);  
     Serial.println("Device 5 ON");
  }else if(device_status5 == 0){
     digitalWrite(device5_pin,HIGH);
     Serial.println("Device 5 OFF");
  }
  
    if(device_status6 == 1){
     digitalWrite(device6_pin,LOW);  
     Serial.println("Device 6 ON");
  }else if(device_status6 == 0){
     digitalWrite(device6_pin,HIGH);
     Serial.println("Device 6 OFF");
  }
  delay(15000);
}

Evething is ok, Serial.print(payload) write 1 or 0 from my web page, but device_status1 is still 0 :frowning:

Where I make a mistake?

Welcome to the forum

Your topic was MOVED to its current forum category as it is more suitable than the original as it is not an Introductory Tutorial

Please post a complete sketch

I updated the code...

Thanks, and thanks for using code tags in your first post. Many don't

Can you show us the output you get in Serial Monitor?

It sounds like the line:
device_status1 = payload.toInt();
is failing to convert "1" to the value 1. Maybe there are other characters in the 'payload' that prevents it from being recognized as a number.

Output from Serial Monitor is 1 or 0 without ""

What I would do next is print the value of payload.length() to make sure that 1 is the only character in the String. Non-printing characters might cause problems for payload.toInt().

When I added Serial.print (payload.length ()); writes the number: 7

So there is something in the payload other than the single character '1'. Maybe that is causing the conversion of 'payload' to an integer to fail and return zero.

Maybe displaying the payload in hexadecimal would be informative.

  for (int i = 0; i < payload.length(); i++)
  {
    Serial.print((byte)payload[i], HEX);
    Serial.print(' ');
  }
  Serial.println();

Now when switch is on writes: EF BB BF EF BB BF 31 and when is off writes: EF BB BF EF BB BF 30

That's a Unicode UTF8 "Byte Order Mark":

Oooo, and how to get out of this problem?

Lots of possibilities. You could figure out why the PHP script is putting those extra characters at the beginning of the payload and fix that. You could find a way to recognize the payload without using ".toInt()".

Thanks, I'll try to fix PHP

I solved the problem by changing the coding of PHP files.
Thanks John :wink:

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