Using the Current Time from Atheros AR9331

This seems like a pretty basic question but it's been giving me some difficulty. I am using my Arduino Yun to turn a light on ( via a relay ) between specific times. I want to use the Atheros AR9331 ( linux component ) to supply the current time and then check to see if it's within the window where the light should be on ( for example, from 9:00 - 17:00 ).

I have been looking at this Time Check tutorial http://arduino.cc/en/Tutorial/TimeCheck for guidance, but it's working with strings and integers. I'm not really sure how to properly use this to compare the current values returned to the time values that I want to check against.

If I receive the values:

hour = 14 min = 06 sec = 54

How do I determine if this is currently between the hours of 9:00 - 17:00?

Is there a way to get an actual datetime value back and use that in a comparison in the arduino sketch?

Thanks, -Eric

If you want to do this on the AtMega side, use a long and calculate:

long stamp=24L*60*60*hour+60L*min+sec; // note the 'L's ...

… and do the same with your start and end times; if beg <= start && start <= end, don’t switch on the lamp.

kind regards,

Jos

The simplest solution is possible because your start time (9:00) and end time (17:00) are whole hours. For that particular example you just need this:

if ((hour >= 9) && (hour < 17)
   // turn on lamp
else
   // turn off lamp

That will keep the light on any time from 9:00:00 through 16:59:59. It will turn on right at 9:00:00, and turn off just as the time switches to 17:00:00 (assuming you are reading the time and making the comparison often enough.)

But I’m guessing you want a more general solution, one that can switch at other times and not just exactly on the hour.

JosAH has a good idea, but I’m not sure why he is multiplying the hour term by 24? Drop the 24 part of it, and you have a solution that works down to one second precision (lets you set a threshold time of 9:14:27, for example.)

But I’m going to guess that you don’t need that much precision. If you just want to switch at a certain hour and minute (with seconds always 00) you can just do this:

int makeTime(int hour, int minute)
{
   return min + (hour * 60);
}

An integer is good enough here, as the maximum value is 59 + (23 * 60) or 1439. There is no need for a long (which takes more storage and processing time.)

To use this function, convert the current time and threshold times, and then do a simple range check:

int start = makeTime(9, 0);
int end = makeTime(17, 0);

int current = makeTime(hour, min);

if ((current >= start) && (current < end))
   // light on
else
   // light off

This code will still turn on at 9:00:00 and turn off at 17:00:00. But it could just as easily turn on at 8:30:00 and off at 17:05:00. It will always switch as the seconds change to/from zero.

Of course, it’s not necessary to calculate the start and end times every time, those values only need to be updated when they are changed. So it’s OK to have them as globals. Only the current time value needs to be recomputed every time you read the time from the Linux side.

Very nice and thank you for the clear explanation and detailed code example. This is very helpful and instructional.

-Eric

Plan B:

Use cron from Linux,

from Arduino IDE, File->Examples->Bridge->Bridge upload it to Yun.

http://arduino.cc/en/Tutorial/Bridge

nano /mnt/sda1/on.sh

#!/bin/ash
/usr/bin/curl "http://127.0.0.1/arduino/digital/13/1"

chmod 755 /mnt/sda1/on.sh
nano /mnt/sda1/off.sh

#!/bin/ash
/usr/bin/curl "http://127.0.0.1/arduino/digital/13/0"

chmod 755 /mnt/sda1/off.sh

Test turn on LED (Red)

/mnt/sda1/on.sh

Test turn off LED (Red)

/mnt/sda1/off.sh
crontab -e

5 5 * * * /mnt/sda1/on.sh
15 17 * * * /mnt/sda1/off.sh

above example command will execute at 5:05AM and 5:15PM daily.

http://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/

You could easily setup schedule service @yearly, @monthly, @weekly, @daily, @hourly.

sonnyyu: Plan B:

Use cron from Linux,

Ooh... An interesting idea! The crontab syntax is a bit fiddly, but very powerful. This could be a great solution for those cases where the timing requirements are complex, but don't change very often. A big advantage here is that it eliminates a lot of bridge traffic required to poll the time constantly, freeing up bandwidth for other uses. The downside is the required code complexity to manage crontab if the timing requirements must be easily adjustable.

drroco: ... The goal of my project is to provide supplemental light in my chicken coop so that my hens continue to lay year round. In order to do this, they need around 14 hours of light per day. By controlling the light with the Arduino, I can avoid having to manually adjust a timer as the days get longer and shorter throughout the year. ...

http://aa.usno.navy.mil/data/docs/RS_OneYear.php

QUEENS, NEW YORK                           
Astronomical Applications Dept.
Location: W073 49, N40 46                          
Rise and Set for the Sun for 2015                   
U. S. Naval Observatory                                                                                                             
Washington, DC  20392-5420                                                             
Eastern Standard Time          

Jan.

Day Rise  Set  
     h m  h m  
01  0719 1638  
02  0719 1639  
03  0720 1640  
04  0720 1641  
05  0720 1642
...

import Rise and Set for the Sun for 2015 data into mysql or sqlite database

Use python sqlite to retrieve data from database:

http://zetcode.com/db/sqlitepythontutorial/

Install python-crontab:

opkg update
opkg install python-openssl #adds ssl support to python
opkg install distribute #it contains the easy_install command line tool (this can take some time)
easy_install python-crontab
nano /mnt/sda1/setcron.py

#!/usr/bin/python
from crontab import CronTab
tab = CronTab(user='root')
cmd = '/mnt/sda1/on.sh'
# You can even set a comment for this command
cron_job = tab.new(cmd, comment='LED')
cron_job.setall('5 5 * * *')
#writes content to crontab
tab.write()
print tab.render()

chmod 755 /mnt/sda1/setcron.py


/mnt/sda1/setcron.py

The code remove all the Cron with comment='LED':

nano /mnt/sda1/removecron.py

#!/usr/bin/python
from crontab import CronTab
tab = CronTab(user='root')
tab.remove_all(comment='LED')
tab.write()
print tab.render()

chmod 755 /mnt/sda1/removecron.py

 /mnt/sda1/removecron.py

Wrote python script:

  1. retrieve sunrise, sunset time from database ( Naval, US)
  2. remove all crontab with comment=‘LED’
  3. set up crontab with comment=‘LED’ with sunrise, sunset time and 14 hours in total .

Run this python script by crontab at early morning say 2:00 AM every day.