Main Site Documentation

Inaccurate time&date with DateTime


#1

Hello,

I am facing an odd issue with DateTime class in NMF, while I was working on my code to test one of the sensors, I tried to display the sensor readings alongside the time of reading as a time stamp, anyway I tried to run the code twice and every time I got a fixed date and time!!! (06/01/2011 00:00:00)!!!

I thought my system has a problem with CPU cycle, I have created a non NMF project (console application) and the time was correct and accurate!! So nothing wrong with my OS!

this is an example of my original code just to test to display the current time&date:


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace GadgeteerApp1
{
    public partial class Program
    {
        
        void ProgramStarted()
        {
            Debug.Print("Program Started "+ DateTime.Today.ToString());
        }
    }
}


It sounds a simple issue but I couldn’t tell where is exactly the problem in order to be solved!!

BTW I am using FEZ Spider mainboard and Visual C# 2010 Express.

Thank you in advance,


#2

and I believe on boot you have to read the RTC and. set the os clock


#3

@ Mike @ andre.m - I am a little bit lost here, would you please explain further to me?

Thanks :slight_smile:


#4

https://www.ghielectronics.com/docs/29/realtime-clock


#5

Another way is if you have a network connection you can read a NTP server and set the time when you first boot the device. I also reset the time at midnight just to stay in sync. I use this and it works great but you do have to have a network connection.


#6

Thanks a lot @ andre.m on this clarification, it helps me a lot!

However, I am using WiFi RS21 module that means I do have internet connection, so I would like to go with @ Cowboy 's suggestion. I have found several NTP clients code!! but none of them has worked with me!! First I run my WiFi connection then I tried to run NTP client to get the current date and time, that’s my logic! am I doing it right?

This is part from the code


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Time;
using GHI.Premium.Net;
using System.Net.Sockets;
using Gadgeteer.Networking;
 using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.Net;
 
namespace GadgeteerWiFi
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        string ssid = "BTHub3-23546";     
        string PassKey = "142578IOPJNF";    
        void ProgramStarted()
        {
            wifi_RS21.UseDHCP();
            WiFiNetworkInfo info = new WiFiNetworkInfo();
            info.SSID = ssid;
            info.SecMode = SecurityMode.WEP;
            info.networkType = NetworkType.AccessPoint;
            wifi_RS21.Interface.Join(info, PassKey);           
            if (!wifi_RS21.Interface.IsOpen)
                wifi_RS21.Interface.Open();
            if (!wifi_RS21.Interface.NetworkInterface.IsDhcpEnabled)
                wifi_RS21.Interface.NetworkInterface.EnableDhcp();
            Debug.Print("Program Started and it's connected and " + wifi_RS21.IsNetworkConnected.ToString());
            SetTime();
        }

        void SetTime()
        {
            Debug.Print("setting time");

            TimeService.SystemTimeChanged += new SystemTimeChangedEventHandler(TimeService_SystemTimeChanged);
            TimeService.TimeSyncFailed += new TimeSyncFailedEventHandler(TimeService_TimeSyncFailed);

            var settings = new TimeServiceSettings();
            settings.ForceSyncAtWakeUp = true;

            // refresh time is in seconds   
            settings.RefreshTime = 1800;

            settings.PrimaryServer = GetTimeServerAddress();

            TimeService.Settings = settings;
            TimeService.SetTimeZoneOffset(0);
            TimeService.Start();
        }

        byte[] GetTimeServerAddress()
        {
            IPAddress[] address = Dns.GetHostEntry("time.windows.com").AddressList;
            if (address != null && address.Length > 0)
            {
                return address[0].GetAddressBytes();
            }
            throw new ApplicationException("Could not get time server address");
        }

        void TimeService_TimeSyncFailed(object sender, TimeSyncFailedEventArgs e)
        {
            Debug.Print("error synchronizing time with NTP server: " + e.ErrorCode);
        }

        void TimeService_SystemTimeChanged(object sender, SystemTimeChangedEventArgs e)
        {
            Debug.Print("network time received. Current Date Time is " + DateTime.Now.ToString());
        }   
 
    }
}


#7

@ andre.m I applied your suggestion regarding networkup and networkdown method, and I nearly have tried every code on internet regrading setting up WiFi connection with Gadgeteer, I have no luck with this module since every code I tried it ended up with a (Network down) status!

I will post it in another thread to avoid confusion with this thread’s title!

But according to your suggestion I have to manually connect a coin-cell battery in socket Z via Extender module, right? Do I have to look for specific resources regarding that?

Thanks in advance,


#8

Hi @ andre.m these are three codes I have used so far to just connect with WiFi - I spent the whole day trying :frowning:

that means connect Pin5 and GND only with the extender, but how I can do this with the battery?? Do I need specific battery holder? any suggestion from your side is great :slight_smile:

and what about the code to setup time after I connected the RTC battery?

Thanks in advance,


#9

That was Mike not me.


#10

Thanks a lot, I will try this out :slight_smile:

Cheers,


#11

Thanks a lot, I will try this out Smiley

Cheers,


#12

I used one of these:
https://www.ghielectronics.com/catalog/product/319

and cr2032 and socket.


#13

@ andre.m

Kindly what do you mean by this specific module, can I use Extender?? Or there is another option?


#14

Do I need to use such a module with FEZ mainbaord
http://www.ebay.co.uk/itm/SD2405-Real-Time-clock-Module-Arduino-Gadgeteer-Compatible-/130958935877?_trksid=p3284.m263&_trkparms=algo%3DSIC%26its%3DI%26itu%3DUCI%2BIA%2BUA%2BFICS%2BUFI%26otn%3D21%26pmod%3D141027491842%26ps%3D54

Can’t just use extender + rc2032 battery and its holder?