Skip to content

Commit f688c3d

Browse files
authored
Merge pull request #12 from lucadentella/provide_full_example
Example added
2 parents f8c3e15 + c9c6fc2 commit f688c3d

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// TOTP DEMO, v1.0
2+
//
3+
// Requires a WiFi-capable board (for example esp32)
4+
// and NTPClient library: https://github.com/arduino-libraries/NTPClient
5+
//
6+
// Change the wifi settings and enter your hmacKey
7+
//
8+
// To generate the hmacKey and initialize the smartphone app
9+
// you can use my tool: http://www.lucadentella.it/OTP
10+
//
11+
// Tested with Arduino 1.8.12, NTPClient 3.2.0 and esp32 1.0.4
12+
13+
14+
#include <WiFi.h>
15+
#include <NTPClient.h>
16+
#include <TOTP.h>
17+
18+
// change the following settings according to your WiFi network
19+
char ssid[] = "mySSID";
20+
char password[] = "myPASSWORD";
21+
22+
// enter your hmacKey (10 digits)
23+
uint8_t hmacKey[] = {0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x6b, 0x65, 0x79, 0x30};
24+
25+
WiFiUDP ntpUDP;
26+
NTPClient timeClient(ntpUDP);
27+
TOTP totp = TOTP(hmacKey, 10);
28+
29+
String totpCode = String("");
30+
31+
void setup() {
32+
33+
Serial.begin(9600);
34+
while (!Serial);
35+
36+
Serial.println("TOTP demo");
37+
Serial.println();
38+
39+
// connect to the WiFi network
40+
WiFi.begin(ssid, password);
41+
while (WiFi.status() != WL_CONNECTED) {
42+
delay(1000);
43+
Serial.println("Establishing connection to WiFi...");
44+
}
45+
Serial.print("Connected to WiFi with IP: ");
46+
Serial.println(WiFi.localIP());
47+
Serial.println();
48+
49+
// start the NTP client
50+
timeClient.begin();
51+
Serial.println("NTP client started");
52+
Serial.println();
53+
}
54+
55+
void loop() {
56+
57+
// update the time
58+
timeClient.update();
59+
60+
// generate the TOTP code and, if different from the previous one, print to screen
61+
String newCode = String(totp.getCode(timeClient.getEpochTime()));
62+
if(totpCode!= newCode) {
63+
totpCode = String(newCode);
64+
Serial.print("TOTP code: ");
65+
Serial.println(newCode);
66+
}
67+
}

0 commit comments

Comments
 (0)