Controller script to respond time request by node

4 years 9 months ago #2278 by vickeyhort
I have tried a couple of hours, but failed to understand. here is my sketch

#include <SPI.h>
#include <MySensor.h>
#include <Time.h>
#include <Timezone.h>


MySensor gw;
boolean timeReceived = false;
unsigned long lastUpdate=0, lastRequest=0;
// Pakistan standard time (GMT+5)
TimeChangeRule myPKTS= {"PKTS", Second, Sun, Mar, 2, +300}; //Daylight time = UTC - 4 hours
Timezone myTZ(myPKTS, myPKTS);
time_t utc, local;

void setup()
{
gw.begin();

// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Clock", "1.0");

// Request time from controller.
gw.requestTime(receiveTime);
}

// This is called when a new time value was received
void receiveTime(unsigned long time) {
// Ok, set incoming time
setTime(time);
timeReceived = true;

}

void loop()
{
unsigned long now = millis();
gw.process();

// If no time has been received yet, request it every 10 second from controller
// When time has been received, request update every hour
if ((!timeReceived && (now-lastRequest) > (10UL*1000UL))
|| (timeReceived && (now-lastRequest) > (60UL*1000UL*60UL))) {
// Request time from controller.
Serial.println("requesting time");
gw.requestTime(receiveTime);
lastRequest = now;
}

// Print time every second
if (timeReceived && now-lastUpdate > 1000) {
Serial.print(hour());
Serial.print(" ");
Serial.print(minute());
Serial.print(" ");
Serial.print(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
lastUpdate = now;
}
}


How to include rest of the things?

Please Log in or Create an account to join the conversation.

Time to create page: 0.177 seconds

Forum latest

  • No posts to display.