-->
Page 1 of 1

NTP Client library

PostPosted: Fri Dec 18, 2015 8:12 am
by gmag11
I am trying to develop a NTP client library for ESP8266/Arduino to make adding NTP sync an easyer task.

Basically, I've thought about a contructor as
Code: Select allNTPClient(String host, int interval)
and a
Code: Select allNTPClient.begin()
function to register sync function.

My problem is with that registering. I have a public function:
Code: Select alltime_t ntpClient::getNtpTime()
that connects to NTP server, decodes the response and returns a time_t variable.

Then, inside
Code: Select allboolean ntpClient::begin()
function I try to run
Code: Select allsetSyncProvider(ntpClient::getNtpTime)
I've also tried to use
Code: Select allsetSyncProvider(this->getNtpTime)
I get a compile error here:

Code: Select allntpClient.cpp:In member function 'boolean ntpClient::begin()
ntpClient.cpp:79:39: error: cannot convert 'ntpClient::getNtpTime' from type 'time_t (ntpClient::)() {aka long int (ntpClient::)()}' to type 'getExternalTime {aka long int (*)()}
  setSyncProvider(ntpClient*:getNtpTime)
Error compiling project sources


What could I do to allow registering this funtion as sync provider to hide ntp internals to my scketches?

Regards

Re: NTP Client library

PostPosted: Thu Feb 11, 2016 4:05 pm
by Dipl Ing
I forgot to remove "setTime(ntp.getNtpTime());" from experimental variant, but it is completely useless
sorry
Code: Select all#include <NTPClient.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>

NTPClient ntp("hu.pool.ntp.org");

time_t kozvetito() {  //kozvetito means middleman in english
  return ntp.getNtpTime();
}

void setup(){
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  WiFi.waitForConnectResult();
  setSyncInterval(1440);
  setSyncProvider(kozvetito);
}

void loop() {
  Serial.println(String(hour())+":"+String(minute())+":"+String(second()));
  delay(1000);
}