ESP32: WiFi NTP TM1650 7segmen serial

From OnnoWiki
Jump to navigation Jump to search
/*********
 
   I2C Device ESP32
   SDA        SDA (default is GPIO 21)
   SCL        SCL (default is GPIO 22)
   GND        GND
   VCC        usually 3.3V or 5V
   
  Get Time NTP
*********/

#include <Wire.h>
#include <TM1650.h>
#include <NTPClient.h>
#include <WiFi.h>
#include <WiFiUdp.h> 

// Replace with your network credentials
const char* ssid     = "o1";
const char* password = "Dzaq1993!";
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", 
"Wednesday", "Thursday", "Friday", "Saturday"};
const long utcOffsetInSeconds = 25200;
int hh, mm, ss;
char cWaktu[5];

TM1650 d;

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "id.pool.ntp.org", utcOffsetInSeconds); 

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

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

void loop() {
  timeClient.update();
  hh = timeClient.getHours();
  mm = timeClient.getMinutes();
  ss = timeClient.getSeconds();
  Serial.print(daysOfTheWeek[timeClient.getDay()]);
  Serial.print(", ");
  Serial.print(hh);
  Serial.print(":");
  Serial.print(mm);
  Serial.print(":");
  Serial.println(ss);
  // Serial.println(timeClient.getFormattedTime());
  // delay(1000); 

  if ( hh > 9 ) {
      String temp_str = String( hh*100 + mm );
      temp_str.toCharArray(cWaktu,5);
  } else {
      String temp_str = " " + String( hh*10 + mm );
      temp_str.toCharArray(cWaktu,5);
  } 

  d.displayString( cWaktu ); // menampilkan waktu
  delay(500); // tunggu selama 1 detik 

  for (int i = 0; i<20; i++) {
    for (int j = 0; j<4; j++) {
       d.setDot(j,true);
       delay(200);
    }
    for (int j = 0; j<4; j++) {
       d.setDot(j,false);
       delay(200);
    }
  }
 
}