Hi,
I am working on a project that is making use of the NTPClient library to get the current date. I am working on a NodeMCU ESP32, so have connected it to the WiFi. When testing the library out, I worked with one file and everything went well. I was able to extract and print the current date. Now, I am trying to implement the same thing into my project, however, I am running into an issue where the library is being defined multiple times. I can't seem to figure out where to place the definitions and initialisations. I have 5 files. Here is my project structure:
-
cros.ino
:
This file is the one I upload to the chip. It includes 3 other files.
#include "cros_core.h"
#include "cros_constants.h"
#include "cros_types.h"
I define a class that has been created in cros_core.h
:
CCrowboxCore g_crOSCore;
And within the setup()
function, I call:
g_crOSCore.Setup();
Which has the logic of the program.
-
cros_core.h
:
This file is defines everything and also sets up a class. The definitions I have included for example are:
#include <ESP32Servo.h>
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
In this file, I also then try to initialise the NTPClient, as I would have done if everything were in one file:
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
There is also a class set up here which looks something like:
class CCrowboxCore
{
public:
CCrowboxCore();
void Setup();
void Loop();
public:
void DebugPrint( const char *pString );
void ReportSystemError( cros_error_code_t errorCode );
//--------------------------------------
// Public Accessor methods
//--------------------------------------
cros_time_t GetUptimeSeconds();
bool IsABirdOnThePerch();
cros_time_t HowLongHasBirdBeenHere();
cros_time_t HowLongHasBirdBeenGone();
And more...
-
cros_core.cpp
:
In here, I have the code for the program. In thesetup()
function of this class, I begin the NTPClient:
timeClient.begin();
timeClient.setTimeOffset(3600);
I then make use of the timeClient in the class loop()
function.
The error I am getting from this is:
sketch\cros_core.cpp.o:(.bss.timeClient+0x0): multiple definition of `timeClient'
sketch\cros.ino.cpp.o:(.bss.timeClient+0x0): first defined here
sketch\cros_core.cpp.o:(.bss.ntpUDP+0x0): multiple definition of `ntpUDP'
sketch\cros.ino.cpp.o:(.bss.ntpUDP+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Where should I be defining and initialising:
#include <NTPClient.h>
#include <WiFiUdp.h>
And
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
Appreciate the help!