Timer for the Core

Hi, Spark core support team and everybody!

I am writing a code for making digital pin to be HIGH by timer.
I have written with HTML and php, but it does not work during the HTML file is not opened.
How can I make timer function which works well?
Could you kindly help me to write a sample?

Regards,
Daisuke

Hi @Daisuke

Have you seen @peekay123 's library for doing timers here:

I am sure he can offer advice too!

@bko, @peekay123
I could not understand how to use it…
Can I make it simply?

  1. User input time 15:00 on website
  2. The website output the time as text. (It should output Json? I still don’t know how)
  3. The core every 30sec access to the text.
  4. If the core detects watch from internet and the time in the text are same, make a pin to be HIGH

Daisuke, the Spark-Interval-Timer uses hardware timers for folks who want accurate, interrupt-driven timers.

In your case, you may want to use bko’s NTP Time library:

You could use the time strings to compare with your user-entered time to achieve what you are looking for. :smile:

Thanks. I am trying to make the core access to JASONP.
I am beginner, but I hope I can complete it.

// This #include statement was automatically added by the Spark IDE.
#include "SparkTime.h"

UDP UDPClient;
SparkTime rtc;

unsigned long currentTime;
unsigned long lastTime = 0UL;
String timeStr;

void setup() {
  rtc.begin(&UDPClient, "north-america.pool.ntp.org");
  rtc.setTimeZone(7); // gmt offset. I am using Malaysian time now
  Serial.begin(9600);
}

void loop() {
    currentTime = rtc.now();
    if (currentTime != lastTime) {
	// Just the time in 12 hour format

	
	timeStr = "";
	timeStr += rtc.AMPMString(currentTime); // modified to show AM/PM here for better visibility
	timeStr += rtc.hour12String(currentTime);
	timeStr += ":";
	timeStr += rtc.minuteString(currentTime);
	
	timeStr += " ";	
	
	Serial.print(timeStr);
      }
      lastTime = currentTime;

}

Hi @Daisuke

Have you seen this tutorial yet?

You could enter your time on a web page and then on a button click script have it POST to the core via a Spark function when you hit a “start” button on the web page.

Does “15:00” in your example above mean 15 minutes and zero seconds or does it mean 15 hours and zero minutes or does it mean at 15:00 real-time, which we would call 3:00 pm in the US?

Are you making an alarm clock?

Thank you @bko

I have not seen your tutorial. I will try it now.
my 15:00 means 15:00 real time.
Your “Real Time Clock Library for Spark” code shows time as 12 hour clock. So I use 12 hour clock on the code.

I am making water pump switch timer which turns on on time. the time is set on website.

Hi @Daisuke

With my library, you have your choice of 12 or 24 hour time. Both are available.

  uint8_t hour(uint32_t tnow);  // this always returns 24-hour time as an integer 0-23
  String hourString(uint32_t tnow);  // use this method for 24-hour time Strings
  String hour12String(uint32_t tnow);  // use this one 12-hour US time Strings

Thank you.
I start to read your tutrial.

I am finding to a way the Core access to JSONP on a server.
I want to make some digital pin to be HIGH when currentTime == time in JSONP.

Hi @Daisuke

It sounds like you want the core to do an HTTP GET request to server on the internet and parse the resulting JSONP string. You can do that! You will only have trouble if the response from the server that needs to be parsed is very long and you have memory problems on the core.

I would start with this library:

There is also an awesome JSON parser based on the JSMN library. It was used in the openweather project on another topic. You can find the library on my github.