logo elektroda
logo elektroda
X
logo elektroda

Creating and using your own WiFi library in the Arduino IDE: code and instructions

opaaa 942 3
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20683825
    opaaa
    Level 4  
    Hi. This is my first approach to the Arduino IDE. I found such code on the internet
    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;
    in the *.ino file
    Quote:
    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.
    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?
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • #2 20683849
    JacekCz
    Level 42  
    opaaa wrote:
    maybe someone can suggest where to look for some information on how to create these types of libraries?


    I guess I would emphasise general C++ knowledge, a set of good "how to create libraries" patterns. It's hard to convey that in a single post.

    Once you have more "battle-fire" in PC versions of C++, you have a better chance.
    I vice versa: without, you have little chance of a nice, well-styled library (which is why most "Arduino libraries" are so lousy)

    Added after 4 [minutes]:

    BTW: local MyWiFiLib object wifiLib; in setup() - makes no sense in good programming, it will be destroyed immediately (unless there are piggyback effects e.g. in global variables) .
    Another, is the name of the class. It has to be named like one piece of "something", but not one piece of a library.
    These are simple things, as one already has experience in C++ on a PC, but impossible to convey "in short soldier words".
  • ADVERTISEMENT
  • #3 20683870
    DJ_KLIMA
    Level 24  
    Creating libraries for Arduino is the same as for C++ and this should be followed.
  • #4 20684311
    JacekCz
    Level 42  
    DJ_KLIMA wrote:
    Creating libraries for Arduino is the same as for C++ and should be followed.



    Exactly. 95% of the principles are the same, do it right, and add 5% of arduino specific knowledge.

    In the normal world of programming, a library developer needs to know the language fluently, without stammer, and in good style. This task is not given to beginners.
ADVERTISEMENT