Skip to content

WiFi connection should not be included in the library #8

@kcranky

Description

@kcranky

Though admittedly it's by no means authoritative, two of the Pragmatic Programming Principles is orthogonality and decoupling - basically to separate functionality where possible. While including connection to the WiFi library is useful because it can hide things away from a user perspective, the users are programmers and so would likely prefer to initialize WiFi outside of the module as it's better practice.

What changes?
The simplest way to implement this is to pull out the toggl.init() function, and in a regular project initialize WiFi as one usually would.

The biggest change would be to the example code provided in the library, but they could be refactored (maybe) as follows:

#include <Toggl.h>

// Ensure we import the correct Wifi library
# if defined (ESP8266)
#include <ESP8266WiFi.h>
# elif defined(ESP32)
#include "WiFi.h"
# else
#error "Please define ESP processor, ESP32 or ESP8266"
# endif

// Set WiFi Username and Password
const char* SSID = "";
const char* PASS = "";

// If you're using an ESP8266, uncomment this
//void wifiSetup(){
//
//  WiFi.begin(String(SSID), String(PASS));
//
//  while(WiFi.status() != WL_CONNECTED){
//    delay(100);
//  }
//}

//If you're using an ESP32, uncomment this 
//void wifiSetup(){
//  WiFi.begin(SSID, PASS);
//  while(WiFi.status() != WL_CONNECTED){
//    delay(100);
//  }
//}

//  Toggl Information
void setup(){
...
  wifiSetup();
...
}

Or

  • just put the code that's in the wifiSetup() methods in the primary setup() method
  • Just leave a comment instructing the user to add the WifI code (though this is more elegant it's maybe less "polite" to beginners)

Other Notes
Ideally the library would handle connection in an agnostic way (i.e not do a if ((WiFi.status() == WL_CONNECTED)) check but rather just check if there is any sort of connection. This could be done by pulling the WiFi check into a separate method, perhaps hasConnection(), which changes depending on what connection method the user decides to implement. This is another issue though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions