WeMOS: D1 R1 mini LCD I2C NTP Prayer Time Indonesia

From OnnoWiki
Jump to navigation Jump to search
/*
 * Calculate Prayer Time
 * 
 * Time & Date From NTP
 */
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "Arduino.h"
#include "uRTCLib.h"
// uRTCLib rtc;
uRTCLib rtc(0x68);
/*
 I2C LCD  ESP8266-Wenos D1 Mini
 GND          GND
 VCC          VIN
 SDA          D2
 SCL          D1
*/
#include <LiquidCrystal_I2C.h>

// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;

LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); 

const char *ssid     = "o1";
const char *password = "Dzaq1993!";
const long utcOffsetInSeconds = 25200;
char weekDays[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char months[12][12]  = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

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

int Y = 2023;
int M = 4;
int D = 4;
int DD;     // Day Now
int H = 16;
int m = 25;
int s = 0;
int Z = 7;  // Time Zone GMT+7 Jakarta
// # define PI           = 3.14159265359;
float LAT                = -6.16858549901122;
float LONG               = 106.865053367193;
float FAJR_ANGLE         = 20;
float ISHA_ANGLE         = 18; 
float SF                 = 1;      // Imam Safii
float DESCEND_CORRECTION = 2/60;
 
float FAJR;
float SUNRISE;
float ZUHR;
float ASR;
float MAGHRIB;
float ISHA;

void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, password); 
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }
  timeClient.begin();
  
  delay(500);
  calcPrayerTime(); 
  // Serial.println( "\n" );
  // PrintFloatToHour( FAJR );
  // PrintFloatToHour( SUNRISE );
  // PrintFloatToHour( ZUHR );
  // PrintFloatToHour( ASR );
  // PrintFloatToHour( MAGHRIB );
  // PrintFloatToHour( ISHA ); 

  URTCLIB_WIRE.begin();
  lcd.begin( 5, 4 );// initialize LCD
  lcd.init();
  // turn on LCD backlight                      
  lcd.backlight();
  lcd.clear(); 
}

void loop() {
  timeClient.update(); 
  
  time_t epochTime = timeClient.getEpochTime();
  String formattedTime = timeClient.getFormattedTime();
  int H = timeClient.getHours();
  int m = timeClient.getMinutes();
  int s = timeClient.getSeconds();
  String weekDay = weekDays[timeClient.getDay()];
  float mm = m;
  float HH = H;
  HH = HH + mm/60;
 
  //Get a time structure
  struct tm *ptm = gmtime ((time_t *)&epochTime); 
  int D = ptm->tm_mday;
  int M = ptm->tm_mon+1;
  String currentMonthName = months[M-1];
  int Y = ptm->tm_year+1900;
  // Print complete date:
  // String currentDate = String(Y) + "-" + String(M) + "-" + String(D);
 
  if( D!=DD ) {
    calcPrayerTime(); 
  }
  lcd.setCursor(0,0);
  lcd.print( "Pray " );
  if( HH < FAJR ) {
    // subuh
    print2digits(int( FAJR ));
    lcd.print( ":" );
    print2digits(int( (FAJR-int(FAJR) )*60)); 

    lcd.print( " " );
    print2digits(int( ZUHR ));
    lcd.print( ":" );
    print2digits(int( (ZUHR-int(ZUHR) )*60));
  } else if ((HH>FAJR) && (HH<ZUHR)) {
    // zuhr
    print2digits(int( ZUHR ));
    lcd.print( ":" );
    print2digits(int( (ZUHR-int(ZUHR))*60 )); 

    lcd.print( " " );
    print2digits(int( ASR ));
    lcd.print( ":" );
    print2digits(int( (ASR-int(ASR) )*60));
    
  } else if ((HH>ZUHR) && (HH<ASR)) {
    // asr
    print2digits(int( ASR ));
    lcd.print( ":" );
    print2digits(int( (ASR-int(ASR) )*60));

    lcd.print( " " );
    print2digits(int( MAGHRIB ));
    lcd.print( ":" );
    print2digits(int( (MAGHRIB-int(MAGHRIB) )*60));

  } else if ((HH>ASR) && (HH<MAGHRIB)) {
    // maghrib
    print2digits(int( MAGHRIB ));
    lcd.print( ":" );
    print2digits(int( (MAGHRIB-int(MAGHRIB) )*60));

    lcd.print( " " );
    print2digits(int( ISHA ));
    lcd.print( ":" );
    print2digits(int( (ISHA-int(ISHA) )*60));
  } else if ((HH>MAGHRIB) && (HH<ISHA)) {
    // isha
    print2digits(int( ISHA ));
    lcd.print( ":" );
    print2digits(int( (ISHA-HH )*60)); 

    lcd.print( " " );
    print2digits(int( FAJR ));
    lcd.print( ":" );
    print2digits(int( (FAJR-int(FAJR) )*60));
  } else {
    // night
    print2digits(int( FAJR ));
    lcd.print( ":" );
    print2digits(int( (FAJR-int(FAJR) )*60)); 

    lcd.print( " " );
    print2digits(int( ZUHR ));
    lcd.print( ":" );
    print2digits(int( (ZUHR-int(ZUHR) )*60));
  }

  lcd.setCursor(4,1);
  print2digits(H);
  lcd.print(':');
  print2digits(m);
  lcd.print(':');
  print2digits(s);
  
  DD = D;
  delay(1000);
}

void PrintFloatToHour( float prayertime ) {
  int hh;
  int mm; 

  hh = int( prayertime );
  mm = int( ( prayertime - hh )*60 );
  if( hh<9 ) Serial.print( String( "0" ) );
  Serial.print( String(hh) + ":" );
  if( mm<9 ) Serial.print( String( "0" ) );
  Serial.print( String(mm) ); 
}

void print2digits(int number) {
  if (number >= 0 && number < 10) lcd.write('0');
  lcd.print(number);
}


void calcPrayerTime() {
  int A = Y/100 ;
  int B = 2 + int(A/4) - A;
  float JD = 1720994.5 + int(365.25*Y) + int(30.6001*(M + 1)) + B + D + ((H*3600 + m*60 + s) / 86400) - (Z / 24 );
  float T = 2 * PI * (JD - 2451545) / 365.25;
  float DELTA = 0.37877 + 23.264 * sin( (57.297*T - 79.547) * PI/180 ) + 0.3812 * sin( (2*57.297*T - 82.682) * PI/180 ) + 0.17132 * sin( (3*57.297*T - 59.722) * PI/180 );
  
  float U = (JD - 2451545) / 36525;
  float L0 = 280.46607 + 36000.7698*U;
  float ET1000 = -(1789 + 237*U) * sin(L0 * PI/180) - (7146 - 62*U) * cos(L0 * PI/180) + (9934 - 14*U) * sin(2*L0 * PI/180) - (29 + 5*U) * cos(2*L0 * PI/180) + (74 + 10*U) * sin(3*L0 * PI/180) + (320 - 4*U) * cos(3*L0 * PI/180) - 212*sin(4*L0 * PI/180);
  float ET = ET1000 / 1000;
  float TT = 12 + Z - (LONG / 15) - (ET / 60);

  float SA_FAJR = -(FAJR_ANGLE);
  float SA_MAGHRIB = -0.8333 - (0.0347 * sqrt(H));
  float SA_SUNRISE = SA_MAGHRIB;
  // acot(A) = atan(1/A)
  // float SA_ASR = acot(SF + tan(abs(DELTA-LAT)*PI/180))*180/PI;
  float SA_ASR = atan(1/(SF + tan(abs(DELTA-LAT)*PI/180)))*180/PI;
  float SA_ISHA = -(ISHA_ANGLE);
   
  float COSHA_FAJR = (sin(SA_FAJR *PI/180) - sin(LAT*PI/180) * sin(DELTA*PI/180)) / (cos(LAT*PI/180) * cos(DELTA*PI/180));
  float COSHA_MAGHRIB = (sin(SA_SUNRISE*PI/180) - sin(LAT*PI/180) * sin(DELTA*PI/180)) / (cos(LAT*PI/180) * cos(DELTA*PI/180));
  float COSHA_SUNRISE = COSHA_MAGHRIB;
  float COSHA_ASR = (sin(SA_ASR*PI/180) - sin(LAT*PI/180) * sin(DELTA*PI/180)) / (cos(LAT*PI/180) * cos(DELTA*PI/180));
  float COSHA_ISHA = (sin(SA_ISHA*PI/180) - sin(LAT*PI/180) * sin(DELTA*PI/180)) / (cos(LAT*PI/180) * cos(DELTA*PI/180));
  
  float HA_FAJR = acos(COSHA_FAJR)*180/PI;
  float HA_MAGHRIB = acos(COSHA_SUNRISE)*180/PI;
  float HA_SUNRISE = HA_MAGHRIB;
  float HA_ASR = acos(COSHA_ASR)*180/PI;
  float HA_ISHA = acos(COSHA_ISHA)*180/PI;
  
  FAJR = TT - HA_FAJR / 15;
  SUNRISE = TT - HA_SUNRISE / 15;
  ZUHR = TT + DESCEND_CORRECTION;
  ASR = TT + HA_ASR / 15;
  MAGHRIB = TT + HA_MAGHRIB / 15;
  ISHA = TT + HA_ISHA / 15;  
}