Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Trigger a function based on time of day


  • Please log in to reply
6 replies to this topic

#1 MaikolPlan

MaikolPlan

    New Member

  • Members
  • Pip
  • 2 posts

Posted 11 September 2012 - 01:43 PM

Hi: I'm new to Netduino world and so far all my little experiments have gone well thanks to the great community in this forum. Now I have a question for you people. I need that my Netduino Plus can send an XML file at a given time of the day. My idea is that for example at 4 p.m there is a trigger that makes Netduino call one of the existing functions in my code, that sends the XML data to my server. I don't know how to create this trigger that calls the existing function. I have read about event handlers and delegates but doesn't know if they can serve my purpose. I code on C#. Thanks in advance. Mike

#2 65tux

65tux

    Advanced Member

  • Members
  • PipPipPip
  • 38 posts

Posted 11 September 2012 - 03:43 PM

Freaky - I'm working right now on doing almost the EXACT same thing. All I am planning to do is in the Main do a while(true) with sleeps to check to see if current time >= configured time, then spawn a thread to do my transactions. Will post code when I get it working.

#3 65tux

65tux

    Advanced Member

  • Members
  • PipPipPip
  • 38 posts

Posted 12 September 2012 - 06:06 PM

Hopefully this works - I stripped a bunch of my other code out because I actually have an array list with multiple time of day events, but this should make sense.

            TimeSpan ts;
			DateTime ShiftStart = DateTime.Today + new TimeSpan(16, 0, 0);  //schedule first event at 16:00:00 today
            while (true)
            {
                DateTime now = DateTime.Now;
                ts = shft.ShiftStart.Subtract(now);
				if (ts.Ticks < 0 )
				{
					//current time greather than event time, fire
					 Thread t = new Thread(MyWorker);
					t.Start();
					//set event time to tomorrow
					ShiftStart = ShiftStart + new TimeSpan(1, 0, 0, 0);
				}
			   Thread.Sleep(10000);
			}


#4 MaikolPlan

MaikolPlan

    New Member

  • Members
  • Pip
  • 2 posts

Posted 13 September 2012 - 01:54 AM

Thanks 65tux! I'll give your code a try. Also I would like to know if someone has accomplish to trigger this with event handlers. thanks,

#5 Patrick

Patrick

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationTampa

Posted 15 September 2012 - 02:24 PM

I don't believe DateTime.Now() actually returns the current time as it does on the non-micro version of the .Net framework. The Netduino simply doesn't remember the system time when powered off (no battery) so you get a default value. You need to somehow set the system time when powered up - maybe from a time server - then that code should work. I've thought about this a bit but have never worked through a solution but it seems likely that someone would have. Sorry I can't be more help.

#6 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 15 September 2012 - 09:03 PM

I don't believe DateTime.Now() actually returns the current time as it does on the non-micro version of the .Net framework. The Netduino simply doesn't remember the system time when powered off (no battery) so you get a default value.

You need to somehow set the system time when powered up - maybe from a time server - then that code should work. I've thought about this a bit but have never worked through a solution but it seems likely that someone would have. Sorry I can't be more help.


You can do it via Time server or you can add a RTC Module (Real Time Clock). Set it once and forget it.

#7 Kem

Kem

    Member

  • Members
  • PipPip
  • 17 posts

Posted 16 September 2012 - 01:49 PM

What about setting a System.Threading.Timer to go at x milliseconds from now?

using System;
using Microsoft.SPOT;
using System.Threading;

namespace DoThingAtEleven
{
    public class Program
    {
        public static void Main()
        {
            Debug.Print(DateTime.Now.ToString("HH:mm:ss"));

            DateTime needEventAt = DateTime.Today + new TimeSpan(23, 00, 0);
            long ticksToEleven = needEventAt.Ticks - DateTime.Now.Ticks;
            int millisecondsToEleven = (int)(ticksToEleven / TimeSpan.TicksPerMillisecond);
            Timer triggerAtEleven = new Timer(new TimerCallback(DoThingAtEleven), null, millisecondsToEleven, Timeout.Infinite);

            Thread.Sleep(Timeout.Infinite);
        }

        private static void DoThingAtEleven(object state)
        {
            Debug.Print("It's time!");
            Debug.Print(DateTime.Now.ToString("HH:mm:ss");
        }
    }
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.