I get an http error -1 on esp8266 with the following code. Connection to Wifi is working fine. but http request leads to an erorr. I tried with GET and POST as well but same error. http request works well in the browser on the other hand.
Can you please help to analyze this issue?
#include <ESP8266WiFi.h>
//#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <UrlEncode.h>
const char* ssid = "XXXXX";
const char* password = "XXXXXX";
// +international_country_code + phone number
String phoneNumber = "XXXXXXXXXXXXX";
String apiKey = "XXXXXXX";
void sendMessage(String message){
// Data to send with HTTP POST
String GetRequest = "http://api.callmebot.com/whatsapp.php?phone=XXXXXXXXXXXX&text=Hurra%3A%20Hello%20from%20ESP8266%21&apikey=XXXX8";
WiFiClient client;
HTTPClient http;
http.begin(client, GetRequest);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send HTTP POST request
int httpResponseCode = http.GET();
if (httpResponseCode == 200){
Serial.print("Message sent successfully");
}
else{
Serial.println("Error sending the message");
Serial.print(url);
Serial.println(httpReqestData );
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
// Send Message to WhatsAPP
sendMessage("Hurra: Hello from ESP8266!");
}
void loop() {
}