0

I run this code and get the massage:

Compilation error: call to 'HTTPClient::begin' declared with attribute error: obsolete API, use ::begin(WiFiClient, url)

what can I do?

//----------------------------------------Include the NodeMCU ESP8266 Library
//----------------------------------------see here: https://www.youtube.com/watch?v=8jMr94B8iN0 to add NodeMCU ESP8266 library and board
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>

//----------------------------------------

#define ON_Board_LED 2  //--> Defining an On Board LED (GPIO2 = D4), used for indicators when the process of connecting to a wifi router

#define LED_D8 15 //--> Defines an LED Pin. D8 = GPIO15



//---------------------------------------SSID and Password of your WiFi router.
const char* ssid = "*wifi name*"; //--> Your wifi name or SSID.
const char* password = "*Wfif password*"; //--> Your wifi password.
//----------------------------------------

//----------------------------------------Web Server address / IPv4
// If using IPv4, press Windows key + R then type cmd, then type ipconfig (If using Windows OS).
const char *host = "https://localhost/";
//----------------------------------------

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(500);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password); //--> Connect to your WiFi router
  Serial.println("");
    
  pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output
  digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board

  pinMode(LED_D8,OUTPUT); //--> LED port Direction output
  digitalWrite(LED_D8, LOW); //--> Turn off Led

  //----------------------------------------Wait for connection
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    //----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
    digitalWrite(ON_Board_LED, LOW);
    delay(250);
    digitalWrite(ON_Board_LED, HIGH);
    delay(250);
    //----------------------------------------
  }
  //----------------------------------------
  digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
  //----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
  Serial.println("");
  Serial.print("Successfully connected to : ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  //----------------------------------------
}

void loop() {
  // put your main code here, to run repeatedly:
  HTTPClient http; //--> Declare object of class HTTPClient
  WiFiClient WiFiClient;

  //----------------------------------------Getting Data from MySQL Database
  String GetAddress, LinkGet, getData;
  int id = 0; //--> ID in Database
  GetAddress = "rafael/GetData.php"; 
  LinkGet = host + GetAddress; //--> Make a Specify request destination
  getData = "ID=" + String(id);
  Serial.println("----------------Connect to Server-----------------");
  Serial.println("Get LED Status from Server or Database");
  Serial.print("Request Link : ");
  Serial.println(LinkGet);
  http.begin("LinkGet");
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header
  int httpCodeGet = http.POST(getData); //--> Send the request
  String payloadGet = http.getString(); //--> Get the response payload from server
  Serial.print("Response Code : "); //--> If Response Code = 200 means Successful connection, if -1 means connection failed. For more information see here : https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  Serial.println(httpCodeGet); //--> Print HTTP return code
  Serial.print("Returned data from Server : ");
  Serial.println(payloadGet); //--> Print request response payload

  if (payloadGet == "1") {
    digitalWrite(LED_D8, HIGH); //--> Turn off Led
  }
  if (payloadGet == "0") {
    digitalWrite(LED_D8, LOW); //--> Turn off Led
  }
  //----------------------------------------
  
  Serial.println("----------------Closing Connection----------------");
  http.end(); //--> Close connection
  Serial.println();
  Serial.println("Please wait 5 seconds for the next connection.");
  Serial.println();
  delay(5000); //--> GET Data at every 5 seconds
}

I want my esp8266 to connect to my database(MySql) and get a value to turn on/off a led light.

The esp im using is the NodeMCU epp8266.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198

1 Answers1

0

Apart from the actual issue that causes the compile error there are several other things wrong with this code. I fixed the most obvious ones. See the explanations for the callouts below the code.

// <1>
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
//#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
//#include <ESP8266WebServer.h>
//#include <ESP8266HTTPClient.h>
//#include <WiFiClientSecureBearSSL.h>

#define ON_Board_LED 2
#define LED_D8 15

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

// <2>
// <3>
const char* host = "http://192.168.1.1/";

// <4>
String GetAddress, LinkGet, getData;
int id = 0;

// <5>
HTTPClient http; 
WiFiClient client;

// <6>
void connectWiFi() {
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    digitalWrite(ON_Board_LED, LOW);
    delay(250);
    digitalWrite(ON_Board_LED, HIGH);
    delay(250);
  }
  digitalWrite(ON_Board_LED, HIGH);
  Serial.println("");
  Serial.print("Successfully connected to : ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println();  
}

void setup() {
  Serial.begin(115200);
  delay(500);

  // <7>
  GetAddress = "rafael/GetData.php";
  LinkGet = host + GetAddress;
  getData = "ID=" + String(id);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  pinMode(ON_Board_LED, OUTPUT);
  digitalWrite(ON_Board_LED, HIGH);

  pinMode(LED_D8, OUTPUT);
  digitalWrite(LED_D8, LOW);
}

void loop() {
  
  // <8>
//  // put your main code here, to run repeatedly:
//  HTTPClient http; //--> Declare object of class HTTPClient
//  WiFiClient WiFiClient;

  // <9>
  if (WiFi.status() != WL_CONNECTED) {
    connectWiFi();  
  }

  Serial.println("----------------Connect to Server-----------------");
  Serial.println("Get LED Status from Server or Database");
  Serial.print("Request Link : ");
  Serial.println(LinkGet);

  // <10>
  http.begin(client, LinkGet);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  int httpCodeGet = http.POST(getData);
  String payloadGet = http.getString();
  
  Serial.print("Response Code : ");
  Serial.println(httpCodeGet);
  Serial.print("Returned data from Server : ");
  Serial.println(payloadGet);

  if (payloadGet == "1") {
    digitalWrite(LED_D8, HIGH);
  }
  if (payloadGet == "0") {
    digitalWrite(LED_D8, LOW);
  }

  Serial.println("----------------Closing Connection----------------");
  http.end();
  Serial.println();
  Serial.println("Please wait 5 seconds for the next connection.");
  Serial.println();
  delay(5000);
}
  1. Clean up the includes such that only those remain which you actually need.
  2. With this sketch your host cannot be (and likely is not) reachable using SSL/TLS encryption (https -> http).
  3. localhost is wrong as this would point to the ESP8266 itself (loopback). You need the PC IP address or possibly its hostname here.
  4. Declare global variables globally i.e. outside loop().
  5. Declare and initialize the HTTPClient and WiFiClient globally rather than inside the loop(). See my answer here https://stackoverflow.com/a/59701884/131929 for more.
  6. Put the WiFi-connecting code in a separate function - you'll need it later.
  7. Initialize the global variables in setup() rather than loop(); goes along with <4>.
  8. Don't instantiate those over and over again; goes along with <5>.
  9. Verify the device is still connected to WiFi every time loop() is invoked and re-connect if not.
  10. Compile error: http.begin() takes a WiFiClient plus that actual HTTP address. This is what the compile error message tells you.
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198