Int httpCode = http.GET(); return -1 arduino wemos d1

hello

I want get a value from function php da.php

<?php echo 10; ?>

and the arduino code is:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "MAURO"; //replace with your own wifi ssid
const char* password = "R000@2021"; //replace with your own wifi ssid password
const char* host = "192.168.0.100";

WiFiClient client;

void setup() {

Serial.begin(115200);
delay(10); // We start by connecting to a WiFi network Serial.println();
Serial.println();
WiFi.mode(WIFI_OFF);
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

}

void loop(){
HTTPClient http;
http.begin(client, "http://192.168.0.100/pr1/da.php");
int httpCode = http.GET();
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
http.end();
delay(5000);
}

but the line httpCode get -1 and payload recover empty.

please help

-1 is returned if WiFiClient.connect failed. so the IP or port 80 don't work.

A negative value from http.GET() means that the GET request either didn't get to the server or there was a problem getting a response. A positive number is the HTTP status from the response (200=Success, 404=Not Found, 500=ServerError...)

Does the URL work when you use a browser?

hello,

yes work the url

Are you sure the ESP8266 and the server are on the same LAN?

hello friend,

yes, in same lan, I just realized the error

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