diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b06e864 --- /dev/null +++ b/.gitignore @@ -0,0 +1,212 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ + +# Visual Studio 2015 cache/options directory +.vs/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +## TODO: Comment the next line if you want to checkin your +## web deploy settings but do note that will include unencrypted +## passwords +#*.pubxml + +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# LightSwitch generated files +GeneratedArtifacts/ +_Pvt_Extensions/ +ModelManifest.xml diff --git a/NtpClient.ino b/NtpClient.ino new file mode 100644 index 0000000..9d8a2dd --- /dev/null +++ b/NtpClient.ino @@ -0,0 +1,32 @@ +/* + Name: NtpClient.ino + Created: 21/12/2015 16:26:34 + Author: gmag1 + Editor: http://www.visualmicro.com +*/ + + +//#include +#include +#include +#include "NtpClientLib.h" + +struct strConfig { + String ssid; + String password; +} config; + +ntpClient ntp; + +// the setup function runs once when you press reset or power the board +void setup() { + config.ssid = "SSID"; // Your SSID + config.password = "PASSWD"; //Your WiFi Password + WiFi.begin(config.ssid.c_str(), config.password.c_str()); + ntp = ntpClient(123,"pool.ntp.org") +} + +// the loop function runs over and over again until power down or reset +void loop() { + +} diff --git a/NtpClient.sln b/NtpClient.sln new file mode 100644 index 0000000..d1d5850 --- /dev/null +++ b/NtpClient.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NtpClient", "NtpClient.vcxproj", "{C5F80730-F44F-4478-BDAE-6634EFC2CA88}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Debug|x86.ActiveCfg = Debug|Win32 + {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Debug|x86.Build.0 = Debug|Win32 + {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Release|x86.ActiveCfg = Release|Win32 + {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/NtpClient.vcxproj b/NtpClient.vcxproj new file mode 100644 index 0000000..e6c01cc --- /dev/null +++ b/NtpClient.vcxproj @@ -0,0 +1,94 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {C5F80730-F44F-4478-BDAE-6634EFC2CA88} + NtpClient + NtpClient + + + + Application + true + v140 + MultiByte + + + Application + false + v140 + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + true + C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\hardware\esp8266\1.6.5-1160-gef26c5f\cores\esp8266;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\hardware\esp8266\1.6.5-1160-gef26c5f\variants\generic;G:\C\Users\gmag1\Dropbox\Arduino\NtpClient\NtpClient;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\hardware\esp8266\1.6.5-1160-gef26c5f\libraries\ESP8266WiFi\src;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\hardware\esp8266\1.6.5-1160-gef26c5f\libraries\ESP8266WiFi\src\utility;G:\C\Users\gmag1\Dropbox\Arduino\NtpClient\NtpClient\src\_micro-api\libraries\NtpClientLib\src;G:\C\Users\gmag1\Dropbox\Arduino\NtpClient\NtpClient\src\_micro-api\libraries\NtpClientLib\src\utility;C:\Users\gmag1\Documents\Arduino\libraries\Time2;C:\Users\gmag1\Documents\Arduino\libraries\Time2\utility;C:\Program Files (x86)\Arduino\libraries;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\hardware\esp8266\1.6.5-1160-gef26c5f\libraries;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\uyvh4bl3.fvx\Micro Platforms\default\debuggers;C:\Users\gmag1\Documents\Arduino\libraries;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\hardware\esp8266\1.6.5-1160-gef26c5f/tools/sdk//include;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\xtensa-lx106-elf;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\lib\gcc\xtensa-lx106-elf\4.8.2\include;C:\Users\gmag1\AppData\Local\arduino15\packages\esp8266\hardware\esp8266\1.6.5-1160-gef26c5f\tools\sdk\include;%(AdditionalIncludeDirectories) + G:\C\Users\gmag1\Dropbox\Arduino\NtpClient\NtpClient\__vm\.NtpClient.vsarduino.h;%(ForcedIncludeFiles) + true + __ESP8266_ESp8266__;__ESP8266_ESP8266__;__ets__;ICACHE_FLASH;F_CPU=80000000L;ARDUINO=166;ARDUINO_ESP8266_ESP01;ARDUINO_ARCH_ESP8266;__cplusplus;%(PreprocessorDefinitions) + + + true + + + + + Level3 + MaxSpeed + true + true + true + + + true + true + true + + + + + CppCode + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NtpClient.vcxproj.filters b/NtpClient.vcxproj.filters new file mode 100644 index 0000000..88ac231 --- /dev/null +++ b/NtpClient.vcxproj.filters @@ -0,0 +1,34 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/__vm/.NtpClient.vsarduino.h b/__vm/.NtpClient.vsarduino.h new file mode 100644 index 0000000..576f731 --- /dev/null +++ b/__vm/.NtpClient.vsarduino.h @@ -0,0 +1,104 @@ +/* + Editor: http://www.visualmicro.com + visual micro and the arduino ide ignore this code during compilation. this code is automatically maintained by visualmicro, manual changes to this file will be overwritten + the contents of the Visual Micro sketch sub folder can be deleted prior to publishing a project + all non-arduino files created by visual micro and all visual studio project or solution files can be freely deleted and are not required to compile a sketch (do not delete your own code!). + note: debugger breakpoints are stored in '.sln' or '.asln' files, knowledge of last uploaded breakpoints is stored in the upload.vmps.xml file. Both files are required to continue a previous debug session without needing to compile and upload again + + Hardware: Generic ESP8266 Module, Platform=esp8266, Package=esp8266 +*/ + +#ifndef _VSARDUINO_H_ +#define _VSARDUINO_H_ +#define __ESP8266_ESp8266__ +#define __ESP8266_ESP8266__ +#define __ets__ +#define ICACHE_FLASH +#define F_CPU 80000000L +#define ARDUINO 166 +#define ARDUINO_ESP8266_ESP01 +#define ARDUINO_ARCH_ESP8266 +#define __cplusplus +#define __ARM__ +#define __inline__ +#define __asm__(x) +#define __extension__ +#define __ATTR_PURE__ +#define __ATTR_CONST__ +#define __inline__ +#define __asm__ +#define __volatile__ + +#define __builtin_va_list +typedef int __gnuc_va_list; + +#define __ICCARM__ +#define __ASM +#define __INLINE +#define __attribute__(noinline) + +#define _STD_BEGIN +#define EMIT +#define WARNING +#define _Lockit +#define __CLR_OR_THIS_CALL +#define C4005 + +typedef int uint8_t; +#define __ARMCC_VERSION 400678 +#define PROGMEM +#define string_literal + +#define prog_void +#define PGM_VOID_P int + +#define _GLIBCXX_CONSTEXPR ([=] () {int _a = (1), _b = (2); return _a > _b ? _a : _b; }()) + + +typedef int _read; +typedef int _seek; +typedef int _write; +typedef int _close; +typedef int __cleanup; + +#define inline +#define __builtin_clz +#define __CHAR_BIT__ +#define _EXFUN() +#define __builtin_labs + +//MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015) +//MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013) +//MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012) +//MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010) +//#if (_MSC_VER == 1600) +// #undef __cplusplus +//#endif + +typedef unsigned char byte; +extern "C" void __cxa_pure_virtual() {;} + + + +#include +#include +#undef F +#define F(string_literal) ((const PROGMEM char *)(string_literal)) +#undef PSTR +#define PSTR(string_literal) ((const PROGMEM char *)(string_literal)) +#undef cli +#define cli() +#define pgm_read_byte(address_short) +#define pgm_read_word(address_short) +#define pgm_read_word2(address_short) +#define digitalPinToPort(P) +#define digitalPinToBitMask(P) +#define digitalPinToTimer(P) +#define analogInPinToBit(P) +#define portOutputRegister(P) +#define portInputRegister(P) +#define portModeRegister(P) +#include <..\NtpClient\NtpClient.ino> +#include +#include +#endif diff --git a/src/_micro-api/libraries/NtpClientLib/library.properties b/src/_micro-api/libraries/NtpClientLib/library.properties new file mode 100644 index 0000000..77c5836 --- /dev/null +++ b/src/_micro-api/libraries/NtpClientLib/library.properties @@ -0,0 +1,9 @@ +name=NtpClientLib +version=1.0.0 +author=gmag1 +maintainer=gmag1 +sentence=NtpClient Library +paragraph= +category=Uncategorized +url=https://github/NtpClient +architectures=* \ No newline at end of file diff --git a/src/_micro-api/libraries/NtpClientLib/src/NtpClientLib.cpp b/src/_micro-api/libraries/NtpClientLib/src/NtpClientLib.cpp new file mode 100644 index 0000000..fa4c146 --- /dev/null +++ b/src/_micro-api/libraries/NtpClientLib/src/NtpClientLib.cpp @@ -0,0 +1,220 @@ +/* + Name: NtpClientLib.cpp + Created: 21/12/2015 16:26:34 + Author: gmag11@gmail.com + Editor: http://www.visualmicro.com +*/ + +#include "NtpClientLib.h" + +time_t ntpClient::getNtpTime() { + if (WiFi.status() == WL_CONNECTED) { +#ifdef DEBUG + Serial.println("Starting UDP"); +#endif + _udp.begin(_udpPort); +#ifdef DEBUG + Serial.print("Local port: "); + Serial.println(_udp.localPort()); +#endif + while (_udp.parsePacket() > 0); // discard any previously received packets + Serial.print("NTP Server hostname: "); + uint8_t dnsResult = WiFi.hostByName(_ntpServerName, _timeServerIP); + Serial.print(dnsResult); + Serial.print(" "); + Serial.println(_timeServerIP); + Serial.println("-- Wifi Connected. Waiting for sync"); +#ifdef DEBUG + Serial.println("-- Transmit NTP Request"); +#endif + if (dnsResult != 0) { + sendNTPpacket(_timeServerIP); + uint32_t beginWait = millis(); + while (millis() - beginWait < 1500) { + int size = _udp.parsePacket(); + if (size >= NTP_PACKET_SIZE) { +#ifdef DEBUG + Serial.println("-- Receive NTP Response"); +#endif + _udp.read(_ntpPacketBuffer, NTP_PACKET_SIZE); // read packet into the buffer + time_t timeValue = decodeNtpMessage(_ntpPacketBuffer); + setSyncInterval(/*NTP_SYNC_PERIOD*/86000); + Serial.println("Sync Period set low"); + _udp.stop(); + + return timeValue; + } + } + Serial.println("-- No NTP Response :-("); + _udp.stop(); + + return 0; // return 0 if unable to get the time + } + else { + Serial.println("-- Invalid address :-(("); + _udp.stop(); + + return 0; // return 0 if unable to get the time + } + } + else { +#ifdef DEBUG + Serial.println("-- NTP Error. Not connected"); +#endif // DEBUG + return 0; + } +} + +ntpClient::ntpClient(int udpPort, String ntpServerName) { + _udpPort = udpPort; + memset(_ntpServerName, 0, NTP_SERVER_NAME_SIZE); + memset(_ntpPacketBuffer, 0, NTP_PACKET_SIZE); + //_ntpServerName = ntpServerName.c_str(); + ntpServerName.toCharArray(_ntpServerName, NTP_SERVER_NAME_SIZE); + Serial.print("ntpClient instance created: "); + Serial.println(_ntpServerName); + _interval = DEFAULT_NTP_INTERVAL; + _timeZone = DEFAULT_NTP_TIMEZONE; +} + +boolean ntpClient::begin() { + setSyncProvider(ntpClient::getNtpTime); //NOT WORKING, FAIL TO COMPILE + //setSyncInterval(_interval); //TODO +} + +boolean ntpClient::stop() { + setSyncProvider((time_t)0); + return true; +} + +time_t ntpClient::decodeNtpMessage(byte *messageBuffer) { + unsigned long secsSince1900; + // convert four bytes starting at location 40 to a long integer + secsSince1900 = (unsigned long)messageBuffer[40] << 24; + secsSince1900 |= (unsigned long)messageBuffer[41] << 16; + secsSince1900 |= (unsigned long)messageBuffer[42] << 8; + secsSince1900 |= (unsigned long)messageBuffer[43]; + +#define SEVENTY_YEARS 2208988800UL + return secsSince1900 - SEVENTY_YEARS + _timeZone * SECS_PER_HOUR; +} + +void ntpClient::nullSyncProvider() { + +} + +/*time_t ntpClient::_getNtpTime() { +return this->getNtpTime(); +}*/ + +/*String ntpClient::getTimeString() { +String timeStr = ""; +timeStr += String(hour()); +timeStr += ":"; +timeStr += printDigits(minute()); +timeStr += ":"; +timeStr += printDigits(second()); +timeStr += " "; +timeStr += printDigits(day()); +timeStr += "/"; +timeStr += printDigits(month()); +timeStr += "/"; +timeStr += String(year()); + +return timeStr; +}*/ + +String ntpClient::printDigits(int digits) { + // utility for digital clock display: prints preceding colon and leading 0 + String digStr = ""; + + if (digits < 10) + digStr += '0'; + digStr += String(digits); + + return digStr; +} + + +boolean ntpClient::setUdpPort(int port) +{ + if (port > 0 || port < 65535) { + _udpPort = port; + return true; + } + else + return false; +} + +int ntpClient::getUdpPort() +{ + return _udpPort; +} + +boolean ntpClient::setInterval(int interval) +{ + if (interval > 15) { + if (_interval != interval) { + _interval = interval; + setSyncInterval(_interval); + } + return true; + } + else + return false; +} + +int ntpClient::getInterval() +{ + return _interval; +} + +boolean ntpClient::setNtpServerName(String ntpServerName) { + memset(_ntpServerName, 0, NTP_SERVER_NAME_SIZE); + ntpServerName.toCharArray(_ntpServerName, NTP_SERVER_NAME_SIZE); + return true; +} + +String ntpClient::getNtpServerName() +{ + return String(_ntpServerName); +} + +boolean ntpClient::setTimeZone(int timeZone) +{ + if (timeZone >= -13 || timeZone <= 13) { + _timeZone = timeZone; + return true; + } + else + return false; +} + +int ntpClient::getTimeZone() +{ + return _timeZone; +} + +// send an NTP request to the time server at the given address +boolean ntpClient::sendNTPpacket(IPAddress &address) { + // set all bytes in the buffer to 0 + memset(_ntpPacketBuffer, 0, NTP_PACKET_SIZE); + // Initialize values needed to form NTP request + // (see URL above for details on the packets) + _ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode + _ntpPacketBuffer[1] = 0; // Stratum, or type of clock + _ntpPacketBuffer[2] = 6; // Polling Interval + _ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision + // 8 bytes of zero for Root Delay & Root Dispersion + _ntpPacketBuffer[12] = 49; + _ntpPacketBuffer[13] = 0x4E; + _ntpPacketBuffer[14] = 49; + _ntpPacketBuffer[15] = 52; + // all NTP fields have been given values, now + // you can send a packet requesting a timestamp: + _udp.beginPacket(address, 123); //NTP requests are to port 123 + _udp.write(_ntpPacketBuffer, NTP_PACKET_SIZE); + _udp.endPacket(); + return true; +} + diff --git a/src/_micro-api/libraries/NtpClientLib/src/NtpClientLib.h b/src/_micro-api/libraries/NtpClientLib/src/NtpClientLib.h new file mode 100644 index 0000000..dbba018 --- /dev/null +++ b/src/_micro-api/libraries/NtpClientLib/src/NtpClientLib.h @@ -0,0 +1,95 @@ +/* + Name: NtpClientLib.h + Created: 21/12/2015 16:26:34 + Author: gmag11@gmail.com + Editor: http://www.visualmicro.com +*/ + +#ifndef _NtpClientLib_h +#define _NtpClientLib_h + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "arduino.h" +#else + #include "WProgram.h" +#endif + +#include +#include +#include + +//#include + +#define DEFAULT_NTP_SERVER "pool.ntp.org" +#define DEFAULT_NTP_PORT 123 +#define DEFAULT_NTP_INTERVAL 600 +#define DEFAULT_NTP_TIMEZONE 1 + +#define NTP_SERVER_NAME_SIZE 60 + +const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message + + +class ntpClient { + +public: + ntpClient() { + ntpClient(DEFAULT_NTP_PORT, DEFAULT_NTP_SERVER); + } + + ntpClient(int udpPort) { + ntpClient(udpPort, DEFAULT_NTP_SERVER); + } + + ntpClient(String ntpServerName) { + ntpClient(DEFAULT_NTP_PORT, ntpServerName); + } + + ntpClient(int udpPort, String ntpServerName); + + //~ntpClient (); + + boolean begin(); //Starts NTP Sync + + boolean stop(); //Stops NTP Sync + + time_t getNtpTime(); //Starts a NTP time request to server. Returns a time in UNIX time format + + //String getTimeString(); + + boolean setUdpPort(int port); + int getUdpPort(); + + boolean setInterval(int interval); + int getInterval(); + + boolean setNtpServerName(String ntpServerName); + String getNtpServerName(); + + boolean setTimeZone(int timeZone); + int getTimeZone(); + +protected: + + boolean sendNTPpacket(IPAddress &address); + time_t decodeNtpMessage(byte *messageBuffer); + void nullSyncProvider(); + //time_t _getNtpTime(); + String printDigits(int digits); + +private: + + int _udpPort; + char _ntpServerName[NTP_SERVER_NAME_SIZE]; + IPAddress _timeServerIP; + + int _timeZone; + WiFiUDP _udp; + byte _ntpPacketBuffer[NTP_PACKET_SIZE]; + int _interval; + + +}; + +#endif +