Connect To Internet and get network time

Hi Folks,

I have been trying to get this to work for a few days now with no luck, im building a research device that I will be using to track the time people spend doing specific activities.

So I need to get the current time when I start the device. I have tried to find code samples for connecting to the network using the Ethernet module but doesn’t seem to work. I have updated my spider to the 4.2 SDK is that where the change is?

the code that I have been using to set up the network looks like this


 ethernet.Interface.Open();
GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet.Interface);
ethernet.Interface.NetworkInterface.EnableStaticIP("192.168.0.200", "255.255.255.0", "192.168.0.1");

as for the network time that Is code that I found in another thread ill link to that too if that will help

if anyone can help or you need further info I would be grateful

Cheers

cfoxleyevans

Hi,
I read in the forum that there are issues with NTP in Vers. 4.2.
http://www.tinyclr.com/forum/topic?id=9672&page=1#msg97737
Search in the forum under “NTP”.
Roland

Give this a try, it works in 4.1 on my Panda II.

namespace Ntp
{
    using System;
    using System.Threading;
    using GHIElectronics.NETMF.FEZ;
    using GHIElectronics.NETMF.Net;
    using GHIElectronics.NETMF.Net.NetworkInformation;
    using GHIElectronics.NETMF.Net.Sockets;
    using Microsoft.SPOT;
    using Microsoft.SPOT.Hardware;
    
	public class Program
    {
        public static void Main()
        {
            WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true);

            try
            {
                Dhcp.EnableDhcp(add mac, add hostname);
                Utility.SetLocalTime(Ntp.NtpClient.GetNetworkTime());
            }
            catch (Exception ex)
            {

            }

            while (true)
            {
                System.Threading.Thread.Sleep(500);
            }
        }
	}
	
    public class NtpClient
    {
        public NtpClient()
        {
        }

        public static DateTime GetNetworkTime()
        {
            return NtpClient.GetNetworkTime("time-a.nist.gov");
        }

        public static DateTime GetNetworkTime(string ntpServer)
        {
            IPAddress[] address = Dns.GetHostEntry(ntpServer).AddressList;
            if (address == null || (int)address.Length == 0)
            {
                throw new ArgumentException(string.Concat("Could not resolve ip address from '", ntpServer, "'."), "ntpServer");
            }
            else
            {
                IPEndPoint ep = new IPEndPoint(address[0], 123);
                return NtpClient.GetNetworkTime(ep);
            }
        }

        public static DateTime GetNetworkTime(IPEndPoint ep)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            s.Bind(ep);
            byte[] ntpData = new byte[48];
            ntpData[0] = 27;
            for (int i = 1; i < 48; i++)
            {
                ntpData[i] = 0;
            }
            s.SendTo(ntpData, ep);

            EndPoint remoteEndPoint = ep;

            s.ReceiveFrom(ntpData, ref remoteEndPoint);
            byte offsetTransmitTime = 40;
            long intpart = (long)0;
            long fractpart = (long)0;
            for (int i = 0; i <= 3; i++)
            {
                intpart = (long)256 * intpart + (long)ntpData[offsetTransmitTime + i];
            }
            for (int i = 4; i <= 7; i++)
            {
                fractpart = (long)256 * fractpart + (long)ntpData[offsetTransmitTime + i];
            }
            long milliseconds = intpart * (long)1000 + fractpart * (long)1000 / 4294967296L;
            s.Close();
            TimeSpan timeSpan = TimeSpan.FromTicks(milliseconds * (long)10000);
            DateTime dateTime = new DateTime(1900, 1, 1);
            dateTime = dateTime + timeSpan;
            
            TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);

            //Hardcode for PST
            //if (offsetAmount == TimeSpan.Zero)
            //{
            //    offsetAmount = new TimeSpan(-7, 0, 0);
            //}

            DateTime networkDateTime = dateTime + offsetAmount;
            return networkDateTime;
        }
    }
}

Hey Folks,

Wasent expecting to get a reply on this thread its been three months :slight_smile:

thanks for the replies but i it turns out the i needed to use DHCP to be able to used the spider on the university network… couldent get this to work on 4.2 not sure if it was a bug in my code or libs but anyway i flashed back to 4.1 and built the project using that :slight_smile:

I then fell on hard time because due to the way that the network was configured i could get an IP but i couldn’t get out of the network so i had to hack my own time service together by using a web server hosted inside the uni network.

Worked out in the end!!

While we’re here, updating an old thread, Kengo your code would not be relevant to anything other than a USBizi based device (Panda, Domino etc) with WIZ5100 Ethernet shield, and 4.1 framework; all the other devices use more advanced networking with the IP stack in the framework not like this which has the stack on the WIZ5100.

If GHI has been unable to find the problem, it is because they can not reproduce the problem, and nobody with the problem has made the effort to send them a set of equipment which displays the problem.

The G120 issue was fixed because someone sent them what they needed to recreate the problem.

repeated mentions of the problem is not going to motivate GHI. if you have the problem, speak privately with GHI and discuss how you can help them recreate the issue.

@ andre.m As always, we are always ready to help and fix any issues. But we obviously can’t fix what we do not see, no one can. You said before you are having problems with DHCP when connecting to your linux machine. How do we know the problem is not in your machine? Have you tried EMX with a router/switch? Did you have problems and if so , what is your router/switch part number?

@ Chris14 - What is your router model number? HAve you tried a different device?

Sorry about that… I was wrapped up on the network time and not the DHCP in 4.2.

@ andre.m - you got it. Replied to your post

Hi Folks

@ Gus didn’t mean to kick up a big fuss on an old thread :wink:

If it helps the set up i had at home was a FEZ Spider connected to a Virgin Media Super-Hubnot sure what the exact model number is im afraid. I was also using the J11D.
I was unable to get a DHCP lease when using this set up using the 4.2 but this worked fine first time with the 4.1.

The set up at university is much more complicated and i dont think it has much relevance to this situation so i wont try to explain (im not really privileged to know this kind of stuff anyway :P)

Hope this helps you if you want to try and track down the problem feel free to pm me if i can help further :slight_smile:

Cheers

Chris Foxley-Evans