OK try this, it is not exactly what you want but close and maybe you can take it from here. I am not sure I see why you want the times displayed so I used the published time from the cloud instead of time from the core.
There are rate limits as to how fast you can publish and one per second is a good place to be.
First the HTML:
<!DOCTYPE HTML>
<html>
<body>
<span id="currentTime"></span><br>
<span id="RELAY1"></span><br>
<span id="RELAY2"></span><br>
<br><br>
<button onclick="start()">Connect</button>
<script type="text/javascript">
function start() {
document.getElementById("currentTime").innerHTML = "Waiting for data...";
var deviceID = "53ff6f065075535155281687";
var accessToken = "1...0";
var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);
eventSource.addEventListener('open', function(e) {
console.log("Opened!"); },false);
eventSource.addEventListener('error', function(e) {
console.log("Errored!"); },false);
eventSource.addEventListener('RELAYTIME', function(e) {
var parsedData = JSON.parse(e.data);
var tempSpan = document.getElementById("currentTime");
tempSpan.style.fontSize = "28px";
tempSpan.innerHTML = "Current time: " + parsedData.data;
}, false);
eventSource.addEventListener('RELAY1', function(e) {
var parsedData = JSON.parse(e.data);
var tempSpan = document.getElementById("RELAY1");
tempSpan.style.fontSize = "28px";
tempSpan.innerHTML = "Alkalinty Status: " + parsedData.data + " as of " + parsedData.published_at;
}, false);
eventSource.addEventListener('RELAY2', function(e) {
var parsedData = JSON.parse(e.data);
var tempSpan = document.getElementById("RELAY2");
tempSpan.style.fontSize = "28px";
tempSpan.innerHTML = "Calcium Status: " + parsedData.data + " as of " + parsedData.published_at;
}, false);
}
</script>
</body>
</html>
And now the core firmware:
// This #include statement was automatically added by the Spark IDE.
#include "SparkTime.h"
UDP UDPClient;
SparkTime rtc;
char publishString[40];
int RELAY1 = D0;
int RELAY2 = D1;
int RELAY3 = D2;
int RELAY4 = D3;
void setup()
{
//Initilize the relay control pins as output
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
// Initialize all relays to an OFF state
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);
rtc.begin(&UDPClient, "pool.ntp.org");
rtc.setTimeZone(-5); // gmt offset
rtc.setUseDST(true);
}
void loop() {
unsigned long currentTime = rtc.now();
static unsigned long stopTime = 0;
static unsigned long lastTime = 0;
if (currentTime > lastTime) { // only check every second
lastTime = currentTime;
sprintf(publishString,"%u:%u:%u",rtc.hour(currentTime),rtc.minute(currentTime),rtc.second(currentTime));
Spark.publish("RELAYTIME",publishString);
if (rtc.hour(currentTime)==0 && rtc.minute(currentTime)==17 && rtc.second(currentTime)==00) {
digitalWrite(RELAY1,HIGH);
Spark.publish("RELAY1","ON");
stopTime = currentTime + (2); //one minute and ten seconds
}
if (currentTime >= stopTime) {
digitalWrite(RELAY1,LOW);
Spark.publish("RELAY1","OFF");
}
if (rtc.hour(currentTime)==0 && rtc.minute(currentTime)==17 && rtc.second(currentTime)==03) {
digitalWrite(RELAY2,HIGH);
Spark.publish("RELAY2","ON");
stopTime = currentTime + (2); //one minute and ten seconds
}
if (currentTime >= stopTime) {
digitalWrite(RELAY2,LOW);
Spark.publish("RELAY2","OFF");
}
delay(100);
}