Skip to content

Instantly share code, notes, and snippets.

@shikarunochi
Created April 29, 2022 06:32
Show Gist options
  • Save shikarunochi/a0b02a582a565c51f0db6000b8253596 to your computer and use it in GitHub Desktop.
Save shikarunochi/a0b02a582a565c51f0db6000b8253596 to your computer and use it in GitHub Desktop.
#include <M5Atom.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include "time.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 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Wi-Fi SSID
char *ssid = "[WIFI SSID]";
// Wi-Fi Password
char *password = "[WIFI PASSWORD]";
#define JST 3600* 9
void setup() {
Wire.begin(21,25);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
delay(1000);
display.clearDisplay();
display.setRotation(2);
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
WiFi.disconnect( true, true );
delay(500);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
delay(500);
display.print(".");
display.display();
}
//configTime(JST, 0, "ntp.nict.jp", "time.google.com", "ntp.jst.mfeed.ad.jp");
configTime(JST, 0, "time.windows.com");
//Wi-Fi接続したままで一度 getLocalTime しておく必要があるらしい。
struct tm timeInfo;
getLocalTime(&timeInfo);
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
display.clearDisplay();
display.display();
}
int preSecond = 0;
unsigned long ss = 0;
unsigned long ssCount = 0;
void loop(){
ss = millis() / 10;
while(true){
int newSS = millis() / 10;
if(newSS != ss){
ssCount = ssCount + (newSS - ss);
if(ssCount > 99){
ssCount = 99; //ごまかし
}
ss = newSS;
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
display.println("Error!");
display.display();
}else{
display.clearDisplay();
display.setCursor(0,20);
display.printf(":%02d.%02d",timeinfo.tm_sec,ssCount);
display.display();
if(timeinfo.tm_sec != preSecond){
ssCount = 0;
preSecond = timeinfo.tm_sec;
}
}
}
delay(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment