WeMOS: D1 R1 mini LCD I2C PCF8574 NTP tanpa Serial

From OnnoWiki
Jump to navigation Jump to search
/*
 * I2C Connector WeMOS D1 Mini Pro
   SDA - pin D2
   SCL - pin D1
*/

// This example shows various featues of the library for LCD with 16 chars and 2 lines.
// Library https://github.com/mathertel/LiquidCrystal_PCF8574

#include <Arduino.h>
#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

LiquidCrystal_PCF8574 lcd(0x27);  // set the LCD address to 0x27 for a 16 chars and 2 line display

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;
char cWaktu[6];

void setup() 
{
  Serial.begin(115200);
  Wire.begin();
  Wire.beginTransmission(0x27);
  Wire.begin();      //Join the bus as master
  lcd.begin(16, 2);  // initialize the lcd 

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

void loop() {
  timeClient.update();
  hh = timeClient.getHours();
  mm = timeClient.getMinutes(); 

  lcd.setBacklight(1);
  lcd.home();
  lcd.clear();
  lcd.noBlink();
  lcd.noCursor();
  lcd.setCursor(0,1);

  if ( hh>9 ) {
    String temp_str = String( hh ) + String( ":") + String( mm );
    temp_str.toCharArray(cWaktu,6);
  } else {
    String temp_str = " " + String( hh ) + String( ":" ) + String( mm );
    temp_str.toCharArray(cWaktu,6);
  }  

  lcd.print( cWaktu ); // menampilkan waktu
  delay(500); // tunggu selama 1 detik 
}