Created Sun, 28 Jun 2015 13:34:09 +0000 by Robbedoes
Sun, 28 Jun 2015 13:34:09 +0000
Hi, I am trying to get a WF32 board to work as NTP Client. I don't seem to be able to change the various examples into a working solution. Would anyone have working code available? Thanks! Rob
Fri, 01 Jan 2016 08:29:29 +0000
Hi, Yup I am exactly in the same boat. Do not understand network protocol and trying to get my RTC set from NTP on WF32 every time it boots. Using the deWebIOServer example as a starting point. This is going to be a surveillance system, one for may dethatched garage and the other for my cabin 5 hour away.
Luc
Mon, 04 Jan 2016 22:34:27 +0000
Hi,
Here is the start of my code. I was taken from several locations including Arduino libraries. It compiles and runs but never gets a response. I am using the ChipKit WF32 board with the DWIFIcK libraries.
This is my code so far, but like I said, "I get no response."
/************************************************************************/
/* */
/* UDPEchoClient */
/* */
/* A chipKIT DNETcK with DWIFIcK UDP Client application to */
/* demonstrate how to use the UdpClient Class. */
/* This is to set the Real Time Clock. */
/* */
/************************************************************************/
/* Author: Keith Vogel */
/* Copyright 2011, Digilent Inc. */
/* Modified: Luc Theoret */
/* Date: Jan 2016 */
/************************************************************************/
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/************************************************************************/
/* */
/* */
/************************************************************************/
/* Revision History: */
/* */
/* 12/21/2011(KeithV): Created */
/* 2/3/2012(KeithV): Updated for WiFi */
/* 11/13/2012(KeithV): Modified to be generic for all HW libraries */
/* */
/************************************************************************/
//******************************************************************************************
//******************************************************************************************
//***************************** SET YOUR CONFIGURATION *************************************
//******************************************************************************************
//******************************************************************************************
/************************************************************************/
/* */
/* Include ONLY 1 hardware library that matches */
/* the network hardware you are using */
/* */
/* Refer to the hardware library header file */
/* for supported boards and hardware configurations */
/* */
/************************************************************************/
// #include <WiFiShieldOrPmodWiFi.h> // This is for the MRF24WBxx on a pmodWiFi or WiFiShield
#include <WiFiShieldOrPmodWiFi_G.h> // This is for the MRF24WGxx on a pmodWiFi or WiFiShield
/************************************************************************/
/* Required libraries, Do NOT comment out */
/************************************************************************/
#include <DNETcK.h>
#include <DWIFIcK.h>
/************************************************************************/
/* */
/* SET THESE VALUES FOR YOUR NETWORK */
/* */
/************************************************************************/
char * szIPServer = "192.168.1.180";
unsigned short portServer = DNETcK::iPersonalPorts44 + 400; // port 44400
// Specify the SSID
const char * szSsid = "TELUS5606";
// select 1 for the security you want, or none for no security
#define USE_WPA2_PASSPHRASE
//#define USE_WPA2_KEY
//#define USE_WEP40
//#define USE_WEP104
//#define USE_WF_CONFIG_H
// modify the security key to what you have.
#if defined(USE_WPA2_PASSPHRASE)
const char * szPassPhrase = "002fba57fd";
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, szPassPhrase, &status)
#elif defined(USE_WPA2_KEY)
DWIFIcK::WPA2KEY key = { 0x27, 0x2C, 0x89, 0xCC, 0xE9, 0x56, 0x31, 0x1E,
0x3B, 0xAD, 0x79, 0xF7, 0x1D, 0xC4, 0xB9, 0x05,
0x7A, 0x34, 0x4C, 0x3E, 0xB5, 0xFA, 0x38, 0xC2,
0x0F, 0x0A, 0xB0, 0x90, 0xDC, 0x62, 0xAD, 0x58 };
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, key, &status)
#elif defined(USE_WEP40)
const int iWEPKey = 0;
DWIFIcK::WEP40KEY keySet = { 0xBE, 0xC9, 0x58, 0x06, 0x97, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00 }; // Key 3
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, keySet, iWEPKey, &status)
#elif defined(USE_WEP104)
const int iWEPKey = 0;
DWIFIcK::WEP104KEY keySet = { 0x3E, 0xCD, 0x30, 0xB2, 0x55, 0x2D, 0x3C, 0x50, 0x52, 0x71, 0xE8, 0x83, 0x91, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Key 3
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, keySet, iWEPKey, &status)
#elif defined(USE_WF_CONFIG_H)
#define WiFiConnectMacro() DWIFIcK::connect(0, &status)
#else // no security - OPEN
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, &status)
#endif
//******************************************************************************************
//******************************************************************************************
//***************************** END OF CONFIGURATION ***************************************
//******************************************************************************************
//******************************************************************************************
typedef enum
{
NONE = 0,
WRITE,
READ,
CLOSE,
DONE,
} STATE;
STATE state = WRITE;
unsigned tStart = 0;
unsigned tWait = 2000;
// must have a datagram cache
byte rgbDatagramCache[48];
UdpClient udpClient(rgbDatagramCache, sizeof(rgbDatagramCache));
// our sketch datagram buffer
byte rgbRead[48];
int cbRead = 0;
// this is for udpClient.writeDatagram to write
byte rgbWriteDatagram[48];
int cbWriteDatagram = sizeof(rgbWriteDatagram);
unsigned long highWord = 0;
unsigned long lowWord = 0;
unsigned long secsSince1900 = 0;
int tCount = 0;
/*** void setup()
*
* Parameters:
* None
*
* Return Values:
* None
*
* Description:
*
* Arduino setup function.
*
* Initialize the Serial Monitor, and initializes the
* connection to the UDPEchoServer
* Use DHCP to get the IP, mask, and gateway
* by default we connect to port 44400
*
* ------------------------------------------------------------ */
void setup() {
pinMode(13, OUTPUT);
DNETcK::STATUS status;
int conID = DWIFIcK::INVALID_CONNECTION_ID;
Serial.begin(9600);
Serial.println("WiFiUDP_NTP_RTC 1.0");
Serial.println("Digilent, Copyright 2012");
Serial.println("");
if((conID = WiFiConnectMacro()) != DWIFIcK::INVALID_CONNECTION_ID)
{
Serial.print("Connection Created, ConID = ");
Serial.println(conID, DEC);
state = WRITE;
}
else
{
Serial.print("Unable to connection, status: ");
Serial.println(status, DEC);
state = CLOSE;
}
// use DHCP to get our IP and network addresses
DNETcK::begin();
// make a connection to our echo server
udpClient.setEndPoint("192.168.1.64", 123);
setupNTPpacket();
}
/*** void loop()
*
* Parameters:
* None
*
* Return Values:
* None
*
* Description:
*
* Arduino loop function.
*
* We are using the default timeout values for the DNETck and UdpClient class
* which usually is enough time for the Udp functions to complete on their first call.
*
* This code will write a sting to the server and have the server echo it back
* Remember, UDP is unreliable, so the server may not echo back if the datagram is lost
*
* ------------------------------------------------------------ */
void loop() {
// int cbRead = 0;
switch(state)
{
// write out the strings
case WRITE:
if(udpClient.isEndPointResolved())
{
Serial.println("Writing out Datagram");
udpClient.writeDatagram(rgbWriteDatagram, cbWriteDatagram);
Serial.println("Waiting to see if a datagram comes back:");
state = READ;
tStart = (unsigned) millis();
}
break;
// look for the echo back
case READ:
delay(1000);
// see if we got anything to read
Serial.println("Reading... ");
if((cbRead = udpClient.available()) > 0)
{
cbRead = cbRead < sizeof(rgbRead) ? cbRead : sizeof(rgbRead);
cbRead = udpClient.readDatagram(rgbRead, cbRead);
Serial.print( "Printing content: " );
for(int i=0; i < cbRead; i++)
{
Serial.print((char) rgbRead[i]);
}
Serial.println( " ");
digitalWrite(13, HIGH);
// give us some more time to wait for stuff to come back
// tStart = (unsigned) millis();
state = CLOSE;
}
// give us some time to get everything echo'ed back
// or if the datagram is never echoed back
else if( (((unsigned) millis()) - tStart) > tWait )
{
// give us some more time to wait for stuff to come back
Serial.println("Give us more time." );
tStart = (unsigned) millis();
tCount ++;
if (tCount > 1)
{
Serial.println("Done waiting, Try again in a minute");
Serial.println("");
delay(600000L);
state = WRITE;
tCount = 0;
}
}
break;
// done, so close up the tcpClient
case CLOSE:
udpClient.close();
Serial.println("Closing udpClient, Done with sketch.");
state = DONE;
highWord = word(rgbRead[40], rgbRead[41]);
lowWord = word(rgbRead[42], rgbRead[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
secsSince1900 = highWord << 16 | lowWord;
Serial.print( "NTP Time: ");
Serial.println( secsSince1900 );
Serial.print( lowWord );
Serial.println( highWord );
break;
case DONE:
// Serial.println( "Done" );
break;
// default:
// break;
}
// keep the stack alive each pass through the loop()
DNETcK::periodicTasks();
}
void setupNTPpacket(void)
{
// set all bytes in the buffer to 0
memset(rgbWriteDatagram, 0, 48);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
rgbWriteDatagram[0] = 0b11100011; // LI, Version, Mode
rgbWriteDatagram[1] = 3; // Stratum, or type of clock
rgbWriteDatagram[2] = 6; // Polling Interval
rgbWriteDatagram[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
rgbWriteDatagram[12] = 49;
rgbWriteDatagram[13] = 0x4E;
rgbWriteDatagram[14] = 49;
rgbWriteDatagram[15] = 52;
}
Luc
Wed, 20 Jan 2016 22:29:06 +0000
Hi, I've been so frustrated with not being able to connect to an NTP server that I found Francesco Potortì code on I think Arduino forum. It gets the the time and date from any webserver. Here is my modifications to the code to work with the WiFiTCPEchoClient.
/*
* © Francesco Potortì 2013 - GPLv3
* modified by:
* Luc Theoret - Jan 2016 - GPLv3
*
* Send an HTTP packet and wait for the response, return the Unix time
*/
unsigned long webUnixTime( char * iServer, unsigned short iPort )
{
unsigned long times = 0;
time_t myUTCTime;
struct tm *myTime;
// Just choose any reasonably busy web server, the load is really low
// Serial.print( "Connecting to: " );
// Serial.println( iServer );
tcpClient.connect( iServer, iPort );
// Serial.println( "Is Connecting: ");
while( !tcpClient.isConnected() )
{
delay(10);
}
// if ( tcpClient.isConnected() )
// {
// Serial.println( "Connected:" );
// Make an HTTP 1.1 request which is missing a Host: header
// compliant servers are required to answer with an error that includes
// a Date: header.
tcpClient.print(F("GET / HTTP/1.1 \r\n\r\n"));
byte buf[1024]; // temporary buffer for characters
String sBuf = "";
String Temp1 = "";
int count = 0;
int offset = 0;
int cbBuf = sizeof( buf ) - 1;
int locptr = 0;
int pass = 0;
// wait for reply
while ( (tcpClient.available() < 1) && (locptr < 1000) )
{
locptr++;
delay( 10 );
}
if((cbBuf = tcpClient.available()) > 0)
{
cbBuf = tcpClient.readStream( buf, cbBuf );
buf[cbBuf] = '\0';
// Serial.println( cbBuf );
for( count = 0; count < cbBuf; count++ )
{
// Serial.print( (char) buf[count] );
// Temp1 = buf[count];
Temp1 = sBuf;
sBuf = Temp1 + (char) buf[count];
}
// Serial.println( " \n " );
count = 0;
do
{
if (( (char) buf[count] == 'D') && ( (char) buf[count + 1] == (int) 'a') && ( (char) buf[count + 2] == (int) 't') && ( (char) buf[count + 3] == (int) 'e'))
{
pass = 1;
// Serial.println( "Found 'Date'" );
}
else
{
count++;
// Serial.print( count );
// Serial.print( " char " );
// Serial.println( (char) buf[count] );
}
}
while( (pass == 0) && (count < cbBuf) );
// Serial.print( "Date offset: " );
// Serial.println( count );
// Parse Date
offset = count + 11;
Temp1 = "";
Temp1 += (char) buf[offset];
sBuf = Temp1 + (char) buf[offset + 1];
unsigned days = sBuf.toInt();
// Serial.print( "Date: " );
// Serial.println( days );
if (days < 10)
{
count--;
}
// Get Month
offset = count + 14;
int daysInPrevMonths;
int months;
switch (buf[offset])
{
case 'F':
daysInPrevMonths = 31;
months = 2;
break; // Feb
case 'S':
daysInPrevMonths = 243;
months = 9;
break; // Sep
case 'O':
daysInPrevMonths = 273;
months = 10;
break; // Oct
case 'N':
daysInPrevMonths = 304;
months = 11;
break; // Nov
case 'D':
daysInPrevMonths = 334;
months = 12;
break; // Dec
default:
if (buf[offset] == 'J' && buf[offset + 1] == 'a')
{
daysInPrevMonths = 0;
months = 1; // Jan
}
else if (buf[offset] == 'A' && buf[offset + 1] == 'p')
{
daysInPrevMonths = 90;
months = 4; // Apr
}
else switch (buf[offset + 2])
{
case 'r':
daysInPrevMonths = 59;
months = 3;
break; // Mar
case 'y':
daysInPrevMonths = 120;
months = 5;
break; // May
case 'n':
daysInPrevMonths = 151;
months = 6;
break; // Jun
case 'l':
daysInPrevMonths = 181;
months = 7;
break; // Jul
default: // add a default label here to avoid compiler warning
case 'g':
daysInPrevMonths = 212;
months = 8;
break; // Aug
}
}
// Serial.print( "Month: " );
// Serial.print( (char) buf[offset] );
// Serial.print( (char) buf[offset + 1] );
// Serial.println( (char) buf[offset + 2] );
// Get Year
offset = count + 18;
sBuf = "";
for( int counter = offset; counter < offset + 4; counter++ )
{
Temp1 = sBuf;
sBuf = Temp1 + (char) buf[counter];
}
int years = sBuf.toInt();
// Serial.print( "Year: " );
// Serial.println( years );
// Get Hour
offset = count + 23;
Temp1 = "";
Temp1 += (char) buf[offset];
sBuf = Temp1 + (char) buf[offset + 1];
int hours = sBuf.toInt();
// Serial.print( sBuf );
// Serial.print( " Hour: " );
// Serial.println( hours );
// Get Min
offset = count + 26;
Temp1 = "";
Temp1 += (char) buf[offset];
sBuf = Temp1 + (char) buf[offset + 1];
int minutes = sBuf.toInt();
// Serial.print( sBuf );
// Serial.print( " Minutes: " );
// Serial.println( minutes );
// Get Sec
offset = count + 29;
Temp1 = "";
Temp1 += (char) buf[offset];
sBuf = Temp1 + (char) buf[offset + 1];
int seconds = sBuf.toInt();
// Serial.print( sBuf );
// Serial.print( " Seconds: " );
// Serial.println( seconds );
// This code will not work after February 2100
// because it does not account for 2100 not being a leap year and because
// we use the day variable as accumulator, which would overflow in 2149
days += (years - 1970) * 365; // days from 1970 to the whole past year
days += (years - 1969) >> 2; // plus one day per leap year
days += daysInPrevMonths; // plus days for previous months this year
if (daysInPrevMonths >= 59 // if we are past February
&& ((years & 3) == 0)) // and this is a leap year
days += 1; // add one day
// Remove today, add hours, minutes and seconds this month
times = (((days) * 24 + hours) * 60 + minutes) * 60 + seconds;
}
// }
// Close TCP Client
// Serial.println("Closing port");
tcpClient.discardReadBuffer();
tcpClient.close();
return times;
}
And here is the calling code
/************************************************************************/
/* */
/* TCPEchoClient */
/* */
/* A chipKIT DNETcK TCP Client application to */
/* demonstrate how to use the TcpClient Class. */
/* This can be used in conjuction with TCPEchoServer */
/* */
/************************************************************************/
/* Author: Keith Vogel */
/* Copyright 2011, Digilent Inc. */
/************************************************************************/
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/************************************************************************/
/* */
/* */
/************************************************************************/
/* Revision History: */
/* */
/* 12/19/2011(KeithV): Created */
/* 2/7/2012(KeithV): Updated for WiFi */
/* 11/13/2012(KeithV): Modified to be generic for all HW libraries */
/* */
/************************************************************************/
//******************************************************************************************
//******************************************************************************************
//***************************** SET YOUR CONFIGURATION *************************************
//******************************************************************************************
//******************************************************************************************
/************************************************************************/
/* */
/* Include ONLY 1 hardware library that matches */
/* the network hardware you are using */
/* */
/* Refer to the hardware library header file */
/* for supported boards and hardware configurations */
/* */
/************************************************************************/
//#include <WiFiShieldOrPmodWiFi.h> // This is for the MRF24WBxx on a pmodWiFi or WiFiShield
#include <WiFiShieldOrPmodWiFi_G.h> // This is for the MRF24WGxx on a pmodWiFi or WiFiShield
/************************************************************************/
/* Required libraries, Do NOT comment out */
/************************************************************************/
#include <DNETcK.h>
#include <DWIFIcK.h>
/************************************************************************/
/* */
/* SET THESE VALUES FOR YOUR NETWORK */
/* */
/************************************************************************/
char * szIPServer = "g.cn"; // server to connect to
unsigned short portServer = DNETcK::iPersonalPorts44 + 300; // port 44300
// Specify the SSID
const char * szSsid = "TELUS5606";
// select 1 for the security you want, or none for no security
#define USE_WPA2_PASSPHRASE
//#define USE_WPA2_KEY
//#define USE_WEP40
//#define USE_WEP104
//#define USE_WF_CONFIG_H
// modify the security key to what you have.
#if defined(USE_WPA2_PASSPHRASE)
const char * szPassPhrase = "002fba57fd";
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, szPassPhrase, &status)
#elif defined(USE_WPA2_KEY)
DWIFIcK::WPA2KEY key = {
0x27, 0x2C, 0x89, 0xCC, 0xE9, 0x56, 0x31, 0x1E,
0x3B, 0xAD, 0x79, 0xF7, 0x1D, 0xC4, 0xB9, 0x05,
0x7A, 0x34, 0x4C, 0x3E, 0xB5, 0xFA, 0x38, 0xC2,
0x0F, 0x0A, 0xB0, 0x90, 0xDC, 0x62, 0xAD, 0x58 };
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, key, &status)
#elif defined(USE_WEP40)
const int iWEPKey = 0;
DWIFIcK::WEP40KEY keySet = {
0xBE, 0xC9, 0x58, 0x06, 0x97, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00 }; // Key 3
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, keySet, iWEPKey, &status)
#elif defined(USE_WEP104)
const int iWEPKey = 0;
DWIFIcK::WEP104KEY keySet = {
0x3E, 0xCD, 0x30, 0xB2, 0x55, 0x2D, 0x3C, 0x50, 0x52, 0x71, 0xE8, 0x83, 0x91, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Key 3
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, keySet, iWEPKey, &status)
#elif defined(USE_WF_CONFIG_H)
#define WiFiConnectMacro() DWIFIcK::connect(0, &status)
#else // no security - OPEN
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, &status)
#endif
//******************************************************************************************
//******************************************************************************************
//***************************** END OF CONFIGURATION ***************************************
//******************************************************************************************
//******************************************************************************************
typedef enum
{
NONE = 0,
WRITE,
READ,
CLOSE,
DONE,
}
STATE;
STATE state = WRITE;
char * isMyIP;
unsigned tStart = 0;
unsigned tWait = 5000;
TcpClient tcpClient;
byte rgbRead[1024];
int cbRead = 0;
// this is for Print.write to print
byte rgbWrite[] = {
'*','W','r','o','t','e',' ','f','r','o','m',' ','p','r','i','n','t','.','w','r','i','t','e','*','\n'};
int cbWrite = sizeof(rgbWrite);
// this is for tcpClient.writeStream to print
byte rgbWriteStream[] = {
'*','W','r','o','t','e',' ','f','r','o','m',' ','t','c','p','C','l','i','e','n','t','.','w','r','i','t','e','S','t','r','e','a','m','*','\n'};
int cbWriteStream = sizeof(rgbWriteStream);
/*** void setup()
*
* Parameters:
* None
*
* Return Values:
* None
*
* Description:
*
* Arduino setup function.
*
* Initialize the Serial Monitor, and initializes the
* connection to the TCPEchoServer
* Use DHCP to get the IP, mask, and gateway
* by default we connect to port 44300
*
* ------------------------------------------------------------ */
void setup() {
// to hold Unix Time
unsigned long timeUnix;
unsigned long addTimeUnix;
DNETcK::STATUS status;
int conID = DWIFIcK::INVALID_CONNECTION_ID;
Serial.begin(9600);
Serial.println("WiFiTCPEchoClient 1.0");
Serial.println("Digilent, Copyright 2012");
Serial.println("");
if((conID = WiFiConnectMacro()) != DWIFIcK::INVALID_CONNECTION_ID)
{
Serial.print("Connection Created, ConID = ");
Serial.println(conID, DEC);
state = WRITE;
}
else
{
Serial.print("Unable to connection, status: ");
Serial.println(status, DEC);
state = CLOSE;
}
// use DHCP to get our IP and network addresses
DNETcK::begin();
// Get server time in Unix format
timeUnix = webUnixTime(szIPServer, 80);
Serial.print( "GMT Time = " );
Serial.println( timeUnix );
addTimeUnix = timeUnix + ((-7) * 3600L);
Serial.print( "Local Time = " );
Serial.print( addTimeUnix );
Serial.println( "" );
setIntRTC( addTimeUnix );
DNETcK::end();
// DNETcK::begin();
// make a connection to our echo server
// tcpClient.connect(szIPServer, portServer);
}
/*** void loop()
*
* Parameters:
* None
*
* Return Values:
* None
*
* Description:
*
* Arduino loop function.
*
* We are using the default timeout values for the DNETcK and TcpClient class
* which usually is enough time for the Tcp functions to complete on their first call.
*
* This code will write some stings to the server and have the server echo it back
*
* ------------------------------------------------------------ */
void loop() {
}
and if you like here is the decoding code
#include <pic32_RTC.h>
/*-------------------------------------------------------
* Luc Theoret
* Jan 2016
*
* Function to read secounds since Jan 1, 1970 and set the
* internal time clock.
*
* Parameters: Pass secound since Jan 1, 1970
* Returns nothing
*
*/
void setIntRTC( unsigned long UnixTime )
{
unsigned long secYear = 31556926;
// unsigned long secMonth = 2629743;
unsigned long SecDay = 86400;
// unsigned long SecHour = 3600;
// unsigned long secMin = 60;
unsigned long workyear;
int worktime;
int workdays;
int leapyears;
int years;
int months;
int days;
int hours;
int minutes;
int seconds;
pic32_RTC myRTC;
// time_t myUnixTime;
// struct tm *myTime;
// Serial.println( "\nSet Time variables" );
years = (UnixTime / secYear) + 1970;
leapyears = ((years - 1969) >> 2);
workyear = UnixTime - ( leapyears * SecDay ) - ((years - 1970) * SecDay * 365);
workdays = workyear / SecDay;
if( workdays < 32 )
{
months = 1;
days = workdays;
}
else if( workdays < 61 )
{
if ((workdays == 60) && ((years & 3) == 0))
{
months = 2;
days = 29;
}
else if (workdays == 60)
{
months = 3;
days = 1;
workdays--;
}
else
{
months = 2;
days = workdays - 31;
}
}
else if( workdays < 91 )
{
months = 3;
days = workdays - 59;
}
else if( workdays < 121 )
{
months = 4;
days = workdays - 90;
}
else if( workdays < 152 )
{
months = 5;
days = workdays - 120;
}
else if( workdays < 182 )
{
months = 6;
days = workdays - 151;
}
else if( workdays < 213 )
{
months = 7;
days = workdays - 181;
}
else if( workdays < 244 )
{
months = 8;
days = workdays - 212;
}
else if( workdays < 274 )
{
months = 9;
days = workdays - 243;
}
else if( workdays < 305 )
{
months = 10;
days = workdays - 273;
}
else if( workdays < 335 )
{
months = 11;
days = workdays - 304;
}
else if( workdays < 366 )
{
months = 4;
days = workdays - 334;
}
else
{
Serial.println( "\n ERROR with date!!\n" );
}
// Serial.println( "\nSet Time: " );
// Serial.println( "Year, Month, Day" );
// Serial.print( years, DEC );
// Serial.print( ", " );
// Serial.print( months, DEC );
// Serial.print( ", " );
// Serial.println( days, DEC );
worktime = workyear - (workdays * SecDay);
hours = worktime / 3600;
minutes = ((worktime - (hours * 3600)) / 60);
seconds = worktime - (hours * 3600) - (minutes * 60);
// Serial.println( "\nHH:MM:SS" );
// Serial.print( hours );
// Serial.print( ":" );
// Serial.print( minutes );
// Serial.print( ":" );
// Serial.println( seconds );
myRTC.set(years, months, days, hours, minutes, seconds);
Serial.print(myRTC.WhoAmI());
Serial.print("\n");
myRTC.get();
Serial.print(myRTC.dayWeek());
Serial.print("\t");
Serial.print(myRTC.date());
Serial.print("\t");
Serial.print(myRTC.time());
Serial.print("\n");
}
If someone would help me with the time.h library. I would like to add timezone and DST support.
Luc