Real Time Clock (RTC) Question on initial setup and Javascript

I am working on my YUN and Xbee project, but running into a question on whether it is a necessary or viable option to use a RTC for anything on this platform since the YUN is wifi proficient.

So for example, if I do use a RTC and want to configure the very initial settings for time, can I just use the Javascript Date function:

<p id="mon"></p>

<script>
var d = new Date();
document.getElementById("mon").innerHTML = d.getMonth(); //day, year, hour ,minutes, etc
</script>

1 script block for each value needed----

and load the “document.getElementById(“mon”).innerHTML = d.getMonth();” in the proper section that RTC library uses to set the month, etc.

Here is some code borrowed from:

// The following codes transmits the data to the RTC
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekday));
  Wire.write(decToBcd(monthday));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.write(byte(0));
  Wire.endTransmission();
  // Ends transmission of data
}

The reason I’m looking at this from the Javascript side is so I can easily input the values into the code as listed above and update periodically if needed.

==================================================
Now from a more practical standpoint (ie, less code, hopefully), if I have internet access via my YUN and I want to say set a 12 hour virtual timer for lights, for instance, and I know I want to set my virtual timer for Turning on at 8 am, I could poll the Javascript time function periodically checking for 8 am. Then using REST commands could download that data to the YUN.

Once the 8 am hour is reached I would set a flag and my 12 hour timer would start without actually using a real timer function, and at the same time activate my function to power on the lights relay via my Xbee, then setup a new poll now to check and see if it is 8 pm when my lights should turn off–when it is 8 pm, then reset the flag, Xbee turns the relay off, and go on, ad infinitum.

A significant problem I see with a pure internet based timer only is if I lose internet access and have no RTC backup-- the polling would still continue, but no actual changes in status could occur until the internet access is resumed. In a scenario such as that, it could be profitable to include a $20 RTC module to my YUN as a failsafe and not have to worry about that problem. Polling could take place using one or the other type of Time indicator, either RTC or web based. I could also then poll the RTC periodically as above to check for the 8 o’clock times. It would accomplish the same goals I believe, but now with built in redundancies as noted.

Any thoughtful feedback appreciated.
Thank you,

houdinihar

A computer uses often a NTP (time server) to get the time, and adjusts the computer time to that. If you open a html page from the Yun on a computer, the JavaScript is executed on the computer and the JavaScript uses the time from the computer.

I don't know if the Yun gets the time with NTP. Many routers and embedded systems have the option to get the time with NTP.

If the Yun is turned on, and it has no acces to internet, it has not time. Only for that situation you could use an RTC.

Do you want to run the code for an alarm on the Arduino part of the Yun or on the wifi/linux/OpenWRT part ? For the arduino part, there is a library : http://www.pjrc.com/teensy/td_libs_TimeAlarms.html For the wifi/linux/OpenWRT part, there is perhaps a packet, or you can write something in python or so.

Do you want to run the code for an alarm on the Arduino part of the Yun or on the wifi/linux/OpenWRT part ?

I am looking for the Arduino side initially, which will then communicate on the wifi side with my Xbees though after the parameters have been assessed.

and the more I think about it, the more I favor having dual Time checks, both RTC and web based. There is more code overhead, but I feel the cost is worth it.

houdinihar

Please buy a good RTC module from a good supplier. For example an DS3231 or else a DS1307. Many cheap RTC modules don't work very well.

You connect that to the pins, and run a library on the Arduino side.

It is possible to send a command to the wifi/linux/OpenWRT side to set the date and time there. I can not test it right now, I'm having troubles with firmware on my Yun (not even a real Yun, but a Dragino shield).

I have been working on some javascript coding to practice with as testing, so far with good results. I am awaiting the arrival of 2 Ds3231 at this time.

<p id="sec"></p>
<p id="min"></p>
<p id="hrs"></p>


<script>	

var myVar= setInterval(function(){alertFunc()}, 5000);	
var stopFlag = 0;

function alertFunc() {    
var d = new Date();

document.getElementById("sec").innerHTML = d.getSeconds();
document.getElementById("min").innerHTML = d.getMinutes();
document.getElementById("hrs").innerHTML = d.getHours();

var h = document.getElementById("hrs").innerHTML = d.getHours();


<!-- Below if statement is merely a place holder at this time for my -->
<!-- Arduino code, which will be geared for any timer type of action, such as lighting -->
<!-- This particular piece would merely be used to send a notice to the Arduino to turn on -->
<!-- a pin via REST commands to activate the relay for the light -->

if (h == 9 && stopFlag == 0)
   {window.alert("It is now 9 am");
   stopFlag += 1;
   }
   else if ( h != 9 && stopFlag >= 1)
   {   stopFlag = 0;}
      
 
}

houdinihar