Last active
November 19, 2021 07:25
-
-
Save CelliesProjects/cb31165ad0ac311132e4cc1054c409c3 to your computer and use it in GitHub Desktop.
Millisecond clock take2 with fps counter for ESP32 with 64x128 SSD1306 OLED display.
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
/*********************************************** | |
* Millisecond clock by Cellie | |
* needs a 128x64 SSD1306 OLED on pins 19 and 23 | |
**********************************************/ | |
#include "WiFi.h" | |
#include "SSD1306.h" //https://github.com/squix78/esp8266-oled-ssd1306 | |
// i2c pin definitions for oled | |
#define I2C_SCL_PIN 19 | |
#define I2C_SDA_PIN 23 | |
const char * networkssid = ".........."; | |
const char * networkpsk = ".........."; | |
const char * NTPpoolAdress = "nl.pool.ntp.org"; | |
const char * defaultTimezone = "CET-1CEST,M3.5.0/2,M10.5.0/3"; /* http://www.remotemonitoringsystems.ca/time-zone-abbreviations.php */ | |
SSD1306 OLED( 0x3c, I2C_SDA_PIN, I2C_SCL_PIN ); | |
void setup() { | |
Serial.begin(115200); | |
//setup oled | |
OLED.init(); | |
OLED.clear(); | |
OLED.setTextAlignment( TEXT_ALIGN_CENTER ); | |
OLED.setFont( ArialMT_Plain_16 ); | |
OLED.drawString( 64, 10, F( "Millisecond clock" ) ); | |
OLED.setFont( ArialMT_Plain_10 ); | |
OLED.drawString( 64, 30, F( "Setting up WiFi and time." ) ); | |
OLED.display(); | |
OLED.setFont( ArialMT_Plain_16 ); | |
// setup WiFi | |
WiFi.mode( WIFI_STA ); | |
Serial.println( "Starting wifi" ); | |
WiFi.begin( networkssid, networkpsk ); | |
while ( WiFi.status() != WL_CONNECTED ) | |
{ | |
delay(500); | |
Serial.print( "." ); | |
} | |
Serial.println(); | |
Serial.println( "WiFi connected" ); | |
//setup ntp time | |
Serial.print( "Getting time from " ); Serial.println( NTPpoolAdress ); | |
configTzTime( defaultTimezone, NTPpoolAdress ); | |
Serial.print( "NTP ready. " ); | |
struct tm timeinfo; | |
if ( !getLocalTime( &timeinfo ) ) | |
{ | |
Serial.println( "Failed to obtain time" ); | |
} | |
else | |
{ | |
Serial.println( asctime( &timeinfo ) ); | |
} | |
} | |
time_t nowtime; | |
struct timeval tv; | |
struct tm *nowtm; | |
char buf[15]; | |
int previousSecond; | |
int frameCounter, fpsLastSecond; | |
void loop() { | |
// put your main code here, to run repeatedly: | |
gettimeofday( &tv, NULL ); | |
OLED.clear(); | |
nowtime = tv.tv_sec; | |
nowtm = localtime(&nowtime); | |
if ( nowtm->tm_sec > previousSecond ) | |
{ | |
previousSecond = nowtm->tm_sec; | |
fpsLastSecond = frameCounter; | |
frameCounter = 0; | |
} | |
snprintf( buf, sizeof(buf), "%d" ":" "%02d" ":" "%02d" "." "%03d", nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec, tv.tv_usec / 1000 ); | |
OLED.drawString( 64, 10, buf ); | |
snprintf( buf, sizeof(buf), "%d fps", fpsLastSecond); | |
OLED.drawString( 64, 30, buf ); | |
OLED.display(); | |
frameCounter++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment