Created
March 22, 2020 15:52
-
-
Save shamasis/4a2a3e1fe3c75a88432478615a4893a6 to your computer and use it in GitHub Desktop.
TOTP Generator for ESP32 OLED module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <NTPClient.h> | |
// dependencies | |
#include "TOTP++.h" | |
#include "SSD1306.h" | |
// replace with your network credentials | |
const char* ssid = "Redacted"; | |
const char* password = "Redacted"; | |
// replace with your timezone in seconds | |
const int tz = 19800; | |
// instance for the OLED. Addr, SDA, SCL | |
SSD1306 display(0x3c, 5, 4); | |
// led pin | |
uint8_t ledPin = 16; | |
// TOTP CONFIG (update the key to match yours) | |
TOTP totp = TOTP(); | |
string key = "JBSWY3DPEHPK3PXP"; | |
char code[6]; // stores the current key in memory | |
// Define NTP Client to get time | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP); | |
// Variables to save date and time | |
String formattedDate; | |
void wifi_setup() { | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
digitalWrite(ledPin, LOW); | |
delay(250); | |
digitalWrite(ledPin, HIGH); | |
delay(250); | |
Serial.print("."); | |
} | |
// Print local IP address and start web server | |
Serial.print("WiFi connected to: "); | |
Serial.println(WiFi.localIP()); | |
digitalWrite(ledPin, HIGH); // led off | |
} | |
void display_setup() { | |
display.init(); | |
display.flipScreenVertically(); | |
} | |
void setup() { | |
// indicate boot using LED | |
pinMode(ledPin, OUTPUT); | |
// Initialize Serial Monitor | |
Serial.begin(115200); | |
// setup wifi | |
wifi_setup(); | |
// setup display | |
display_setup(); | |
// Initialize a NTPClient to get time | |
timeClient.begin(); | |
timeClient.setTimeOffset(tz); // GMT+5:30 = 19800 | |
digitalWrite(ledPin, HIGH); | |
} | |
void loop() { | |
while (!timeClient.update()) { | |
timeClient.forceUpdate(); | |
} | |
long GMT = timeClient.getEpochTime() - tz; | |
char *newCode = totp.getCode(key, 30, GMT); | |
if (strcmp(code, newCode) != 0 && GMT != 0) { | |
strcpy(code, newCode); | |
} | |
display.clear(); // clear the display | |
display.setFont(ArialMT_Plain_10); | |
display.setTextAlignment(TEXT_ALIGN_LEFT); | |
display.drawString(2, 2, "Dan Hersam Token"); | |
display.setFont(ArialMT_Plain_24); | |
display.setTextAlignment(TEXT_ALIGN_CENTER); | |
display.drawString(64, 20, code); | |
display.setFont(ArialMT_Plain_10); | |
display.setTextAlignment(TEXT_ALIGN_RIGHT); | |
display.drawString(126, 52, timeClient.getFormattedTime() + " GMT +530"); | |
display.display(); // display whatever is in the buffer | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, shamasis.
would you share "TOTP++" dependencies?