Getting just the time from unix time

Hi all,

As part of a project I want to drive a motor at specific times every day of the year at sunrise and sunset. However, I don't know how to get the time by itself, ignoring the date, from unix time. Is there some sort of command that allows me to do this?

So far I can only get the motor to run at 2 specific times.
Any help would be greatly appreciated.

This is the code I have so far:

#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_MotorShield.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x57);


// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

RTC_DS3231 rtc;
uint32_t sunrise = 1593575280;
uint32_t sunset = 1593634860;
int counter = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis;

 
void setup ()
{

  Serial.begin(9600);

  Serial.println("Starting RTC");
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    // while (1);
  }
  Serial.println("RTC started");
  Serial.println("Starting AMS");
  AFMS.begin();
  Serial.println(" AMS started, setup routine finished");
  myMotor->setSpeed(10);
}
void doorUp () {
  //delay(2000);
  myMotor->step(50, FORWARD, SINGLE);
  //delay(5000);
  myMotor->step(200, FORWARD, SINGLE);
}
void doorDown () {
  myMotor->step(200, BACKWARD, SINGLE);
  //delay(5000);
  //delay(2000)
  myMotor->step(50, BACKWARD, SINGLE);
}

void loop ()
{ 
  delay(1000);
  currentMillis = millis();

  if (currentMillis - previousMillis >= 1000) {

    previousMillis = currentMillis;

    Serial.print("Loop has run ");
    Serial.print(counter);
    Serial.println(" times");
    counter = counter + 1;

    DateTime now = rtc.now();
    Serial.println(now.unixtime());

    if (now.unixtime() > sunrise and sunrise < (sunrise + 1)) {
      doorUp ();
  }
  if (now.unixtime() > sunset and sunset < (sunset + 1)) {
      doorDown ();
  }

  }

}

Here is an example how I would do that (extracted and modified from a working program)

time_t getSeconds(uint8_t hour, uint8_t min, uint8_t sec)
{
    return (hour * 3600UL) + (min * 60UL) + sec;
}

...
time_t sunrise = getSeconds(3, 48, 0);
time_t sunset = getSeconds(20, 21, 0);
...
time_t now = rtc.get();
time_t todaySeconds = getSeconds(hour(now), minute(now), second(now));

if (todaySeconds > sunrise && todaySeconds < sunset)
...

Side note about your code, these conditions:

sunrise < (sunrise + 1)
...
sunset < (sunset + 1)

will obviously always be true...

To get just the time as a string from a unix timestamp you could use toString() from RTClib.h

#include <RTClib.h>

RTC_DS1307 rtc;

void setup () {
  Serial.begin(57600);
}

void loop() {

  DateTime now = rtc.now();
  char format[] = "hh:mm:ss";          // or "hh:mm"
  char tm = now.toString(format);
  Serial.println(tm);

}

Alternatively you could also use now.hour() and now.minute() which return hour and minute as integers.

Plasterboard:
Hi all,

As part of a project I want to drive a motor at specific times every day of the year at sunrise and sunset. However, I don't know how to get the time by itself, ignoring the date, from unix time. Is there some sort of command that allows me to do this?

There are 86400 seconds in a day. Use the modulo operator % to discard the days and keep only the excess seconds.

https://www.arduino.cc/en/pmwiki.php?n=Reference/Modulo

Thanks for the advice, everyone.

You could use the Solar Position library to trigger sunrise and sunset events directly:

float currentElev = Timbuktu.getSolarPosition(someEpochTime).elevation;
if (deviceState == OFF and currentElev > 0.0) {
  deviceState = ON;
  [... turn on the device ]
  {
else if (deviceState == ON and currentElev < 0.0) {
 deviceState = OFF;
  [... turn off the device ]
  }

It can easily be synced to any time source.