|
| 1 | +/**The MIT License (MIT) |
| 2 | +
|
| 3 | +Copyright (c) 2015 by Daniel Eichhorn |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in all |
| 13 | +copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +SOFTWARE. |
| 22 | +
|
| 23 | +See more at http://blog.squix.ch |
| 24 | +*/ |
| 25 | + |
| 26 | +#include "TimeClient.h" |
| 27 | + |
| 28 | +TimeClient::TimeClient(int utcOffset) { |
| 29 | + myUtcOffset = utcOffset; |
| 30 | +} |
| 31 | + |
| 32 | +void TimeClient::updateTime() { |
| 33 | + WiFiClient client; |
| 34 | + const int httpPort = 80; |
| 35 | + if (!client.connect("www.google.com", httpPort)) { |
| 36 | + Serial.println("connection failed"); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // This will send the request to the server |
| 41 | + client.print(String("GET / HTTP/1.1\r\n") + |
| 42 | + String("Host: www.google.com\r\n") + |
| 43 | + String("Connection: close\r\n\r\n")); |
| 44 | + while(!client.available()) { |
| 45 | + delay(1000); |
| 46 | + } |
| 47 | + |
| 48 | + String line; |
| 49 | + |
| 50 | + int size = 0; |
| 51 | + client.setNoDelay(false); |
| 52 | + while(client.connected()) { |
| 53 | + while((size = client.available()) > 0) { |
| 54 | + line = client.readStringUntil('\n'); |
| 55 | + line.toUpperCase(); |
| 56 | + // example: |
| 57 | + // date: Thu, 19 Nov 2015 20:25:40 GMT |
| 58 | + if (line.startsWith("DATE: ")) { |
| 59 | + Serial.println(line.substring(23, 25) + ":" + line.substring(26, 28) + ":" +line.substring(29, 31)); |
| 60 | + int parsedHours = line.substring(23, 25).toInt(); |
| 61 | + int parsedMinutes = line.substring(26, 28).toInt(); |
| 62 | + int parsedSeconds = line.substring(29, 31).toInt(); |
| 63 | + |
| 64 | + localEpoc = (parsedHours * 60 * 60 + parsedMinutes * 60 + parsedSeconds); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +String TimeClient::getHours() { |
| 72 | + if (localEpoc == 0) { |
| 73 | + return "--"; |
| 74 | + } |
| 75 | + int hours = (getCurrentEpoch() % 86400L) / 3600 + myUtcOffset; |
| 76 | + if (hours < 10) { |
| 77 | + return "0" + String(hours); |
| 78 | + } |
| 79 | + return String(hours); // print the hour (86400 equals secs per day) |
| 80 | + |
| 81 | +} |
| 82 | +String TimeClient::getMinutes() { |
| 83 | + if (localEpoc == 0) { |
| 84 | + return "--"; |
| 85 | + } |
| 86 | + int minutes = ((getCurrentEpoch() % 3600) / 60); |
| 87 | + if (minutes < 10 ) { |
| 88 | + // In the first 10 minutes of each hour, we'll want a leading '0' |
| 89 | + return "0" + String(minutes); |
| 90 | + } |
| 91 | + return String(minutes); |
| 92 | +} |
| 93 | +String TimeClient::getSeconds() { |
| 94 | + if (localEpoc == 0) { |
| 95 | + return "--"; |
| 96 | + } |
| 97 | + int seconds = getCurrentEpoch() % 60; |
| 98 | + if ( seconds < 10 ) { |
| 99 | + // In the first 10 seconds of each minute, we'll want a leading '0' |
| 100 | + return "0" + String(seconds); |
| 101 | + } |
| 102 | + return String(seconds); |
| 103 | +} |
| 104 | + |
| 105 | +String TimeClient::getFormattedTime() { |
| 106 | + return getHours() + ":" + getMinutes() + ":" + getSeconds(); |
| 107 | +} |
| 108 | + |
| 109 | +long TimeClient::getCurrentEpoch() { |
| 110 | + return localEpoc + ((millis() - localMillisAtUpdate) / 1000); |
| 111 | +} |
| 112 | + |
0 commit comments