Difference between revisions of "WeMOS: D1 R1 mini OLED 0.91 128x32 NTP"

From OnnoWiki
Jump to navigation Jump to search
(Created page with " /********* Complete project details at https://randomnerdtutorials.com This is an example for our Monochrome OLEDs based on SSD1306 drivers. Pick one up today in th...")
 
 
(One intermediate revision by the same user not shown)
Line 26: Line 26:
 
  const char *ssid    = "o3";
 
  const char *ssid    = "o3";
 
  const char *password = "Dzaq1993!";
 
  const char *password = "Dzaq1993!";
  const long utcOffsetInSeconds = 25200;  
+
  const long utcOffsetInSeconds = 25200;
 
   
 
   
 
  // Define NTP Client to get time
 
  // Define NTP Client to get time
Line 32: Line 32:
 
  NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
 
  NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
 
  int hh, mm, ss;
 
  int hh, mm, ss;
 +
String chh;
 +
String cmm;
 
  char cWaktu[6];
 
  char cWaktu[6];
 
+
 
  void setup() {
 
  void setup() {
 
   Serial.begin(115200);
 
   Serial.begin(115200);
Line 41: Line 43:
 
     Serial.println(F("SSD1306 allocation failed"));
 
     Serial.println(F("SSD1306 allocation failed"));
 
     for(;;); // Don't proceed, loop forever
 
     for(;;); // Don't proceed, loop forever
   }  
+
   }
 
   
 
   
 
   // Show initial display buffer contents on the screen --
 
   // Show initial display buffer contents on the screen --
Line 62: Line 64:
 
   mm = timeClient.getMinutes();  
 
   mm = timeClient.getMinutes();  
 
   
 
   
   if ( hh>9 ) {
+
   if ( hh>9 ) chh = String( hh );
    String temp_str = String( hh ) + String( ":") + String( mm );
+
  else chh = String( " " ) + String( hh );
    temp_str.toCharArray(cWaktu,6);
+
  if ( mm>9 ) cmm = String( mm );
   } else {
+
   else cmm = String( "0" ) + String( mm );
    String temp_str = " " + String( hh ) + String( ":" ) + String( mm );
+
    temp_str.toCharArray(cWaktu,6);
+
  String temp_str = chh + String( ":" ) + cmm;
  }
+
  temp_str.toCharArray(cWaktu,6);
 
    
 
    
 
   display.clearDisplay();
 
   display.clearDisplay();
Line 76: Line 78:
 
   display.println( cWaktu );
 
   display.println( cWaktu );
 
   display.display();
 
   display.display();
   delay(60000);
+
   delay(30000);
 
  }
 
  }

Latest revision as of 01:41, 28 March 2023

/*********
  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 32 // 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     = "o3";
const char *password = "Dzaq1993!";
const long utcOffsetInSeconds = 25200;

// 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();
  hh = timeClient.getHours();
  mm = timeClient.getMinutes(); 

  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.clearDisplay();
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println( cWaktu );
  display.display();
  delay(30000);
}