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.
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.
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());
}
}
}
@ 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?
Hi @ andre.m these are three codes I have used so far to just connect with WiFi - I spent the whole day trying
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
and what about the code to setup time after I connected the RTC battery?