/*
To test, you need the Wokwi IoT Gateway, as explained here:
https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway
Then start the simulation, and open http://localhost:9080
in another browser tab.
Note that the IoT Gateway requires a Wokwi Club subscription.
To purchase a Wokwi Club subscription, go to https://wokwi.com/club
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Font
#include <Fonts/FreeMonoBold12pt7b.h>
// #include <Arduino.h>
#ifdef ESP32
// #include "../libraries/WiFi"
#include <WiFi.h>
// #include "../libraries/AsyncTCP"
#include <AsyncTCP.h>
#elif defined(ESP8266)
// #include "../libraries/ESP8266WiFi"
#include <ESP8266WiFi.h>
// #include "../libraries/ESPAsyncTCP"
#include <ESPAsyncTCP.h>
#endif
// #include "../libraries/ESPAsyncWebServer"
// #include <ESPAsyncWebServer.h>
// #include "index.h"
/* =========================
You can choose any font you want from:
FreeMono12pt7b.h FreeSansBoldOblique12pt7b.h
FreeMono18pt7b.h FreeSansBoldOblique18pt7b.h
FreeMono24pt7b.h FreeSansBoldOblique24pt7b.h
FreeMono9pt7b.h FreeSansBoldOblique9pt7b.h
FreeMonoBold12pt7b.h FreeSansOblique12pt7b.h
FreeMonoBold18pt7b.h FreeSansOblique18pt7b.h
FreeMonoBold24pt7b.h FreeSansOblique24pt7b.h
FreeMonoBold9pt7b.h FreeSansOblique9pt7b.h
FreeMonoBoldOblique12pt7b.h FreeSerif12pt7b.h
FreeMonoBoldOblique18pt7b.h FreeSerif18pt7b.h
FreeMonoBoldOblique24pt7b.h FreeSerif24pt7b.h
FreeMonoBoldOblique9pt7b.h FreeSerif9pt7b.h
FreeMonoOblique12pt7b.h FreeSerifBold12pt7b.h
FreeMonoOblique18pt7b.h FreeSerifBold18pt7b.h
FreeMonoOblique24pt7b.h FreeSerifBold24pt7b.h
FreeMonoOblique9pt7b.h FreeSerifBold9pt7b.h
FreeSans12pt7b.h FreeSerifBoldItalic12pt7b.h
FreeSans18pt7b.h FreeSerifBoldItalic18pt7b.h
FreeSans24pt7b.h FreeSerifBoldItalic24pt7b.h
FreeSans9pt7b.h FreeSerifBoldItalic9pt7b.h
FreeSansBold12pt7b.h FreeSerifItalic12pt7b.h
FreeSansBold18pt7b.h FreeSerifItalic18pt7b.h
FreeSansBold24pt7b.h FreeSerifItalic24pt7b.h
FreeSansBold9pt7b.h FreeSerifItalic9pt7b.h
============================ */
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
// Setup own Router AP connection
const char* ssid = "********";
const char* password = "********";
// Adafruit Object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Web Server
// Initializate the server
AsyncWebServer server(80);
int second = 0;
char ssecond[5];
int minute = 0;
char sminute[5];
char all[10];
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.setFont(&FreeMonoBold12pt7b);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
// Display text
textCenter("00:00");
}
void loop() {
second++;
if(second > 60){
second = 0;
minute ++;
}
// Convert 123 to string [buf]
itoa(second, ssecond, 10);
itoa(minute, sminute, 10);
strcat( all, minute );
strcat( all, second );
textCenter(all);
delay(100);
}
/* ===========================
Center the text
============================= */
void textCenter(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
display.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// display on horizontal and vertical center
display.clearDisplay(); // clear display
display.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
display.println(text); // text to display
display.display();
}