Hi. This is my first approach to the Arduino IDE. I found such code on the internet
File con_wifi.ino
File MyWiFiLib.h
File MyWiFiLib.cpp
This program is supposed to connect the ESP8266 to the wifi network and it works perfectly. The problem is that I don't understand how the libraries in the arduino work. For this code I found such a description of the line in the *.ino file
File con_wifi.ino
#include "MyWiFiLib.h"
void setup() {
Serial.begin(115200);
delay(1000);
MyWiFiLib wifiLib;
const char* ssid = "wifi"; // Zdefiniuj swoją nazwę sieci WiFi
const char* password = "abc123abc123"; // Zdefiniuj swoje hasło do sieci WiFi
wifiLib.connectToWiFi(ssid, password);
// Dalsze operacje
}
void loop() {
// Działania w pętli
}
File MyWiFiLib.h
#ifndef MyWiFiLib_h
#define MyWiFiLib_h
#include <Arduino.h>
#include <ESP8266WiFi.h>
class MyWiFiLib {
public:
MyWiFiLib();
void connectToWiFi(const char* ssid, const char* password);
};
#endif
File MyWiFiLib.cpp
#include "MyWiFiLib.h"
MyWiFiLib::MyWiFiLib() {
// Konstruktor, jeśli potrzebujesz
}
void MyWiFiLib::connectToWiFi(const char* ssid, const char* password) {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP());
WiFi.hostname("D1mini");
Serial.println(WiFi.getHostname());
}
This program is supposed to connect the ESP8266 to the wifi network and it works perfectly. The problem is that I don't understand how the libraries in the arduino work. For this code I found such a description of the line
MyWiFiLib wifiLib;
Quote:but it doesn't tell me too much, maybe someone can suggest where to look for some information on how to create this type of library?The line MyWiFiLib wifiLib; creates an instance of the MyWiFiLib class object named wifiLib. This is an initialisation of the MyWiFiLib library object that was previously defined in the "MyWiFiLib.h" header file.
After creating an instance of the wifiLib object, its functions and methods, such as connectToWiFi(), can be used to perform operations inside this library. In this case, the connectToWiFi() function is called on the wifiLib object with the given ssid and password arguments, which will attempt to connect the ESP8266 module to a WiFi network with the given name (SSID) and password (password).
Using the wifiLib object allows easy use of the MyWiFiLib library functions, which helps organise the code and allows these functions to be used in different places in the program when needed.