Lesson 28: Retrieve Smartphone Date and Time

 

Written by Jonathan Sim

You can find this lesson and more in the Arduino IDE (File -> Examples -> Andee). If you are unable to find them, you will need to install the Andee Library for Arduino IDE.

Works on iOS and Android
In this lesson, I'll show you two methods that you can use to retrieve the date and time from your smartphone.

Method 1 retrieves date and time as a string.

Method 2 retrieves date and time as separate integer variables so that you can easily use them for other purposes.

Lesson28.png


Contents

Top of the Code

#include <SPI.h>
#include <Andee.h>

// We'll create two objects to demonstrate these two ways.
AndeeHelper methodOne;
AndeeHelper methodTwo;

char methodOneString[30];
char methodTwoString[30]; 
int day;
int month;
int year;
int hour; 
int minute;
int second;

setup()

void setup()
{
  Andee.begin();  // Setup communication between Annikken Andee and Arduino
  Andee.clear();  // Clear the screen of any previous displays
  setInitialData(); // Define object types and their appearance
}

setInitialData()

void setInitialData()
{
  methodOne.setId(0);
  methodOne.setType(DATA_OUT);
  methodOne.setLocation(0,0,FULL);
  methodOne.setTitle("Method One");
  
  methodTwo.setId(1);
  methodTwo.setType(DATA_OUT);
  methodTwo.setLocation(1,0,FULL);
  methodTwo.setTitle("Method Two");
}

loop()

void loop()
{
  /////// Method One /////////
  // Useful only if you want to display the date/time and nothing more.
  // Refer to http://resources.annikken.com/index.php?title=How_to_retrieve_the_smartphone%27s_date/time_as_a_string
  // for formatting options
  Andee.printDeviceTime("%A, %d %B %Y, %I:%M:%S %p", methodOneString);
  methodOne.setData(methodOneString);
  methodOne.update();
  
  /////// Method Two /////////
  // Useful if you need to check the current time to execute a scheduled task
  // Retrieve date and store in variables: day, month, and year
  Andee.getDeviceDate(&day, &month, &year);
  // Retrieve time and store in variables: hour, minute, second
  Andee.getDeviceTime(&hour, &minute, &second);
  sprintf(methodTwoString, "%d/%d/%d, %02d:%02d:%02d", day, month, year, hour, minute, second);
  methodTwo.setData(methodTwoString);
  methodTwo.update();
  
  delay(500); // Always leave a short delay for Bluetooth communication
}
comments powered by Disqus