WiFiUDP udp;
+unsigned long ntp_lastset = 0;
+unsigned long ntp_lasttry = 0;
+
+
/* compose and send an NTP time request packet */
void ntp_send()
{
if (ntpServerIP == INADDR_NONE) {
- WiFi.hostByName(NTP_SERVER, ntpServerIP);
- Serial.print("Got NTP server " NTP_SERVER " address ");
- Serial.println(ntpServerIP);
+ if (WiFi.hostByName(NTP_SERVER, ntpServerIP) == 1) {
+ if (ntpServerIP == IPAddress(1,0,0,0)) {
+ Serial.println("DNS lookup failed for " NTP_SERVER " try again later.");
+ ntpServerIP = INADDR_NONE;
+ return;
+ }
+ Serial.print("Got NTP server " NTP_SERVER " address ");
+ Serial.println(ntpServerIP);
+ } else {
+ Serial.println("DNS lookup of " NTP_SERVER " failed.");
+ return;
+ }
}
-
+ ntp_lasttry = millis();
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
const unsigned long seventyYears = 2208988800UL;
time_t unixtime = secsSince1900 - seventyYears;
+ ntp_lastset = millis();
Serial.print("NTP update unixtime=");
Serial.println(unixtime);
return unixtime;
return answer;
}
+// add an entry to the log
void logEntry(time_t when, uint32_t card)
{
unsigned char entry[8];
f.close();
}
+// produce a copy of the log file
String printLog(int html, int last)
{
String out;
// some serial, for debug
Serial.begin(115200);
- // The lock mechanism, set HIGH to turn on and connect ground to output pin
+ // The lock mechanism
pinMode(MOSFET, OUTPUT);
digitalWrite(MOSFET, LOCK_CLOSE);
// if we have no config, enter config mode
WiFiManager wfm;
+ // Only wait in config mode for 3 minutes max
+ wfm.setConfigPortalTimeout(180);
+ // Try to connect to the old Ap for this long
+ wfm.setConnectTimeout(60);
+ // okay, lets try and connect...
wfm.autoConnect(MANAGER_AP);
+ Serial.println("Entering normal doorlock mode.");
+
// we have config, enable web server
server.on( "/", handleRoot );
server.on( "/reset", handleReset );
SPIFFS.begin();
// init wiegand keyfob reader
- Serial.println("Starting Wiegand test reader");
+ Serial.println("Configuring Wiegand keyfob reader");
wg.begin(WD0, WD0, WD1, WD1);
// setup button debounce for the release switch
release_button.setup(ERELEASE, 20, InputDebounce::PIM_EXT_PULL_DOWN_RES);
+ Serial.println("Requesting time from network");
// listener port for replies from NTP
udp.begin(localPort);
setSyncProvider(ntp_fetch);
+
+ Serial.println("Hackspace doorlock v1. READY");
}
unsigned long locktime = 0;
}
}
+ // has ntp failed, do we need to try again?
+ if (ntp_lastset == 0 && ntp_lasttry + 300000 < millis()) {
+ Serial.println("Ask Time service to try again");
+ setSyncProvider(ntp_fetch);
+ }
}