WeMOS: D1 R1 mini OLED 0.96 126x64 NTP

From OnnoWiki
Jump to navigation Jump to search
/*
  Pin	ESP8266
  Vin	5V
  GND	GND
  SCL	GPIO 5 (D1) 
  SDA	GPIO 4 (D2)
 */
/*********
  Complete project details at https://randomnerdtutorials.com
  
  This is an example for our Monochrome OLEDs based on SSD1306 drivers. Pick one up today in the adafruit shop! ------> http://www.adafruit.com/category/63_98
  This example is for a 128x32 pixel display using I2C to communicate 3 pins are required to interface (two I2C and one reset).
  Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
  Written by Limor Fried/Ladyada for Adafruit Industries, with contributions from the open source community. BSD license, check license.txt for more information All text above, and the splash screen below must be included in any redistribution. 
*********/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.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)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const char *ssid     = "o1";
const char *password = "Dzaq1993!";
const long utcOffsetInSeconds = 25200;
//Week Days
String weekDays[7]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

//Month names
String months[12]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
int hh, mm, ss;
String chh;
String cmm;
char cWaktu[6]; 



void setup() {
  Serial.begin(115200);

  // 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
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();  

  WiFi.begin(ssid, password); 
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
  }
  timeClient.begin();

  // Clear the buffer
  display.clearDisplay();
}

void loop() {
  timeClient.update();
  time_t epochTime = timeClient.getEpochTime();
  
  String weekDay = weekDays[timeClient.getDay()];
  struct tm *ptm = gmtime ((time_t *)&epochTime); 
  int monthDay = ptm->tm_mday;
  int currentMonth = ptm->tm_mon+1;
  String currentMonthName = months[currentMonth-1];
  int currentYear = ptm->tm_year+1900;
  String currentDate = weekDay +", " + String(monthDay) + " " + currentMonthName + " " + String(currentYear)  ;
  currentDate.toCharArray(cWaktu,20);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(20, 0);
  display.println( cWaktu );

  hh = timeClient.getHours();
  mm = timeClient.getMinutes(); 
  ss = timeClient.getSeconds();
  if ( hh>9 ) chh = String( hh );
  else chh = String( " " )  + String( hh );
  if ( mm>9 ) cmm = String( mm );
  else cmm = String( "0" ) + String( mm );
  String temp_str = chh + String( ":" ) + cmm;
  temp_str.toCharArray(cWaktu,6);
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(0, 35);
  display.println( cWaktu );
  display.display();
  delay( (60-ss)*1000 );
}