By | 13/08/2020

Helo temen-temen anakkendali.com

Banyak yang membuat projek running teks untuk kebutuhan jadwal sholat di masjid, namun masih menggunakan modul RTC lokal seperti DS3231 dan DS1307, namun sekarang website anakkendali menyarankan data waktu bisa di ambil secara realtime dari internet. baik tanggal, tahun, jam bahkan sampai menit, semua datanya dapat di ambil dari internet menggunakan NTP server. tidak hanya itu, pada tutorial kali ini kita akan mengakses API JWS (Jadwal Waktu Sholat) sehingga mendapatkan data jadwa waktu sholat secara akurat dari Internet.

Untuk mempelajari cara bagaiaman mengambil data waktu di internet, silahkan baca artikel berikut NodeMCU NTP Server Membuat Jam Digital TM1637.h Wemos ESP8266 dan juga Jam Dot Matrix Waktu dari NTP Server Wemos ESP8266.

ESP8266/ESP32 Mengakses NTP Server dan Api Jadwal Waktu Sholat (JWS)

Skematik

Kali ini kita tidak menggunakan rangkaian atau skematik apapun, karena hanya akan menggunakan satu modul ESP8266 seperti NodeMCU, Wemos D1 Mini atau ESP32 seperti Lolin 32, Wemos D1 mini ESP32, ESP32 CAM dan lain sebagainya.

Code

Code ESP8266 NTP Server dan API Jadwal Waktu Sholat

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>

const char *ssid     = "anakkendali.com";
const char *password = "bayardikitlimangewu";


const long utcOffsetInSeconds = 25200;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", utcOffsetInSeconds);

#define tahun timeClient.getYear()
#define bulan timeClient.getMonth()
#define tanggal timeClient.getDate()
#define hari daysOfTheWeek[timeClient.getDay()]
#define jam timeClient.getHours()
#define menit timeClient.getMinutes()
#define detik timeClient.getSeconds()

char tanggalapi[11];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
}

unsigned long lasttime = 0;

void loop() {
  // put your main code here, to run repeatedly:
  timeClient.update();
  if (millis() - lasttime >= 1000) {
    Serial.print(daysOfTheWeek[timeClient.getDay()]);
    Serial.print(", ");
    
    if (bulan >= 10 && tanggal >= 10) {
      sprintf (tanggalapi, "%d-%d-%d", tahun, bulan, tanggal);
    }
    else if (bulan >= 10 && tanggal < 10) {
      sprintf (tanggalapi, "%d-%d-0%d", tahun, bulan, tanggal);
    }
    else if (bulan < 10 && tanggal >= 10) {
      sprintf (tanggalapi, "%d-0%d-%d", tahun, bulan, tanggal);
    }
    else if (bulan < 10 && tanggal < 10) {
      sprintf (tanggalapi, "%d-0%d-0%d", tahun, bulan, tanggal);
    }

    Serial.print(tanggalapi);
    Serial.print("\t");
    
    Serial.print(jam);
    Serial.print(":");
    Serial.print(menit);
    Serial.print(":");
    Serial.println(detik);
    Serial.println();
    lasttime = millis();
  }
  req_api();
}

void req_api(){
  StaticJsonDocument<1024> doc;
  HTTPClient http;
  String url = "http://api.banghasan.com/sholat/format/json/jadwal/kota/687/tanggal/";
  url += tanggalapi;
  
//  Serial.println(
  http.begin(url);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  int httpCode = http.POST("?");
  String payload = http.getString();
//  Serial.println(payload);

  DeserializationError error = deserializeJson(doc, payload);

  JsonObject results = doc["jadwal"]["data"];
  String ashar = results["ashar"];
  String dhuha = results["dhuha"];
  String dzuhur = results["dzuhur"];
  String imsak = results["imsak"];
  String isya = results["isya"];
  String subuh = results["subuh"];

  Serial.print("Ashar = ");
  Serial.println(ashar);
  Serial.print("Dhuha = ");
  Serial.println(dhuha);
  Serial.print("Dzuhur = ");
  Serial.println(dzuhur);
  Serial.print("Imsak = ");
  Serial.println(imsak);
  Serial.print("Isya = ");
  Serial.println(isya);
  Serial.print("Subuh = ");
  Serial.println(subuh);
  Serial.println();
  
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }
}

ESP32 NTP Server dan API Jadwal Waktu Sholat

#include <NTPClient.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>

const char *ssid     = "anakkendali.com";
const char *password = "bayardikitlimangewu";


const long utcOffsetInSeconds = 25200;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", utcOffsetInSeconds);

#define tahun timeClient.getYear()
#define bulan timeClient.getMonth()
#define tanggal timeClient.getDate()
#define hari daysOfTheWeek[timeClient.getDay()]
#define jam timeClient.getHours()
#define menit timeClient.getMinutes()
#define detik timeClient.getSeconds()

char tanggalapi[11];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
}

unsigned long lasttime = 0;

void loop() {
  // put your main code here, to run repeatedly:
  timeClient.update();
  if (millis() - lasttime >= 1000) {
    Serial.print(daysOfTheWeek[timeClient.getDay()]);
    Serial.print(", ");
    
    if (bulan >= 10 && tanggal >= 10) {
      sprintf (tanggalapi, "%d-%d-%d", tahun, bulan, tanggal);
    }
    else if (bulan >= 10 && tanggal < 10) {
      sprintf (tanggalapi, "%d-%d-0%d", tahun, bulan, tanggal);
    }
    else if (bulan < 10 && tanggal >= 10) {
      sprintf (tanggalapi, "%d-0%d-%d", tahun, bulan, tanggal);
    }
    else if (bulan < 10 && tanggal < 10) {
      sprintf (tanggalapi, "%d-0%d-0%d", tahun, bulan, tanggal);
    }

    Serial.print(tanggalapi);
    Serial.print("\t");
    
    Serial.print(jam);
    Serial.print(":");
    Serial.print(menit);
    Serial.print(":");
    Serial.println(detik);
    Serial.println();
    lasttime = millis();
  }
  req_api();
}

void req_api(){
  StaticJsonDocument<1024> doc;
  HTTPClient http;
  String url = "http://api.banghasan.com/sholat/format/json/jadwal/kota/687/tanggal/";
  url += tanggalapi;
  
//  Serial.println(
  http.begin(url);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  int httpCode = http.POST("?");
  String payload = http.getString();
//  Serial.println(payload);

  DeserializationError error = deserializeJson(doc, payload);

  JsonObject results = doc["jadwal"]["data"];
  String ashar = results["ashar"];
  String dhuha = results["dhuha"];
  String dzuhur = results["dzuhur"];
  String imsak = results["imsak"];
  String isya = results["isya"];
  String subuh = results["subuh"];

  Serial.print("Ashar = ");
  Serial.println(ashar);
  Serial.print("Dhuha = ");
  Serial.println(dhuha);
  Serial.print("Dzuhur = ");
  Serial.println(dzuhur);
  Serial.print("Imsak = ");
  Serial.println(imsak);
  Serial.print("Isya = ");
  Serial.println(isya);
  Serial.print("Subuh = ");
  Serial.println(subuh);
  Serial.println();
  
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }
}

Untuk Hasilnya dapat dilihat pada gambar berikut.

ESP8266/ESP32 Mengakses NTP Server dan Api Jadwal Waktu Sholat (JWS)

Untuk melihat tutorial video di youtube silahkan tonton dibawah ini.

Keyword

  1. Api Jadwal Sholat Internet,
  2. ESP32 NodemCU JWS,
  3. JWS Online Wemos D1 mini,
  4. JWS Oline ESP8266,
  5. Jam Digital Internet ESP8266 ESP32,

12 Replies to “ESP8266/ESP32 Mengakses NTP Server dan Api Jadwal Waktu Sholat (JWS)”

  1. Nanda

    Saya mengalami error, Mas…
    ‘class NTPClient’ has no member named ‘getMonth’

    Reply
      1. rac

        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino: In function ‘void loop()’:
        exe:13:26: error: ‘class NTPClient’ has no member named ‘getMonth’
        #define bulan timeClient.getMonth()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:38:9: note: in expansion of macro ‘bulan’
        if (bulan >= 10 && tanggal >= 10) {
        ^
        exe:14:28: error: ‘class NTPClient’ has no member named ‘getDate’
        #define tanggal timeClient.getDate()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:38:24: note: in expansion of macro ‘tanggal’
        if (bulan >= 10 && tanggal >= 10) {
        ^
        exe:12:26: error: ‘class NTPClient’ has no member named ‘getYear’
        #define tahun timeClient.getYear()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:39:40: note: in expansion of macro ‘tahun’
        sprintf (tanggalapi, “%d-%d-%d”, tahun, bulan, tanggal);
        ^
        exe:13:26: error: ‘class NTPClient’ has no member named ‘getMonth’
        #define bulan timeClient.getMonth()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:39:47: note: in expansion of macro ‘bulan’
        sprintf (tanggalapi, “%d-%d-%d”, tahun, bulan, tanggal);
        ^
        exe:14:28: error: ‘class NTPClient’ has no member named ‘getDate’
        #define tanggal timeClient.getDate()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:39:54: note: in expansion of macro ‘tanggal’
        sprintf (tanggalapi, “%d-%d-%d”, tahun, bulan, tanggal);
        ^
        exe:13:26: error: ‘class NTPClient’ has no member named ‘getMonth’
        #define bulan timeClient.getMonth()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:41:14: note: in expansion of macro ‘bulan’
        else if (bulan >= 10 && tanggal = 10 && tanggal < 10) {
        ^
        exe:12:26: error: 'class NTPClient' has no member named 'getYear'
        #define tahun timeClient.getYear()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:42:41: note: in expansion of macro 'tahun'
        sprintf (tanggalapi, "%d-%d-0%d", tahun, bulan, tanggal);
        ^
        exe:13:26: error: 'class NTPClient' has no member named 'getMonth'
        #define bulan timeClient.getMonth()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:42:48: note: in expansion of macro 'bulan'
        sprintf (tanggalapi, "%d-%d-0%d", tahun, bulan, tanggal);
        ^
        exe:14:28: error: 'class NTPClient' has no member named 'getDate'
        #define tanggal timeClient.getDate()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:42:55: note: in expansion of macro 'tanggal'
        sprintf (tanggalapi, "%d-%d-0%d", tahun, bulan, tanggal);
        ^
        exe:13:26: error: 'class NTPClient' has no member named 'getMonth'
        #define bulan timeClient.getMonth()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:44:14: note: in expansion of macro 'bulan'
        else if (bulan = 10) {
        ^
        exe:14:28: error: ‘class NTPClient’ has no member named ‘getDate’
        #define tanggal timeClient.getDate()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:44:28: note: in expansion of macro ‘tanggal’
        else if (bulan = 10) {
        ^
        exe:12:26: error: ‘class NTPClient’ has no member named ‘getYear’
        #define tahun timeClient.getYear()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:45:41: note: in expansion of macro ‘tahun’
        sprintf (tanggalapi, “%d-0%d-%d”, tahun, bulan, tanggal);
        ^
        exe:13:26: error: ‘class NTPClient’ has no member named ‘getMonth’
        #define bulan timeClient.getMonth()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:45:48: note: in expansion of macro ‘bulan’
        sprintf (tanggalapi, “%d-0%d-%d”, tahun, bulan, tanggal);
        ^
        exe:14:28: error: ‘class NTPClient’ has no member named ‘getDate’
        #define tanggal timeClient.getDate()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:45:55: note: in expansion of macro ‘tanggal’
        sprintf (tanggalapi, “%d-0%d-%d”, tahun, bulan, tanggal);
        ^
        exe:13:26: error: ‘class NTPClient’ has no member named ‘getMonth’
        #define bulan timeClient.getMonth()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:47:14: note: in expansion of macro ‘bulan’
        else if (bulan < 10 && tanggal < 10) {
        ^
        exe:14:28: error: 'class NTPClient' has no member named 'getDate'
        #define tanggal timeClient.getDate()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:47:28: note: in expansion of macro 'tanggal'
        else if (bulan < 10 && tanggal < 10) {
        ^
        exe:12:26: error: 'class NTPClient' has no member named 'getYear'
        #define tahun timeClient.getYear()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:48:42: note: in expansion of macro 'tahun'
        sprintf (tanggalapi, "%d-0%d-0%d", tahun, bulan, tanggal);
        ^
        exe:13:26: error: 'class NTPClient' has no member named 'getMonth'
        #define bulan timeClient.getMonth()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:48:49: note: in expansion of macro 'bulan'
        sprintf (tanggalapi, "%d-0%d-0%d", tahun, bulan, tanggal);
        ^
        exe:14:28: error: 'class NTPClient' has no member named 'getDate'
        #define tanggal timeClient.getDate()
        ^
        C:\Users\PC-2021\Desktop\PROJET\exe\exe.ino:48:56: note: in expansion of macro 'tanggal'
        sprintf (tanggalapi, "%d-0%d-0%d", tahun, bulan, tanggal);
        ^
        exit status 1
        'class NTPClient' has no member named 'getMonth'

        Reply
    1. admin Post author

      karena https, ditambahin fingerprint atau sertifikat nya,, atau paksa jangan menggunakan secure.

      Reply
      1. git

        {“status”:true,”data”:{“id”:”1607″,”lokasi”:”KAB. JEMBER”,”daerah”:”JAWA TIMUR”,”koordinat”:{“lat”:-8.168455555555555,”lon”:113.70053888888889,”lintang”:”8\u00b0 10′ 6.44\” S”,”bujur”:”113\u00b0 42′ 1.94\” E”},”jadwal”:{“tanggal”:”Sabtu, 31\/07\/2021″,”imsak”:”04:10″,”subuh”:”04:20″,”terbit”:”05:36″,”dhuha”:”06:04″,”dzuhur”:”11:35″,”ashar”:”14:56″,”maghrib”:”17:28″,”isya”:”18:39″,”date”:”2021-07-31″}}}

        sudah bisa narik cuman bagian depan misal data bisa diambil tapi kl jadwal hasilnya null
        Subuh = null

        Reply
          1. git

            {“status”:true,”data”:{“id”:”1607″,”lokasi”:”KAB. JEMBER”,”daerah”:”JAWA TIMUR”,”koordinat”:{“lat”:-8.168455555555555,”lon”:113.70053888888889,”lintang”:”8\u00b0 10′ 6.44\” S”,”bujur”:”113\u00b0 42′ 1.94\” E”},”jadwal”:{“tanggal”:”Sabtu, 31\/07\/2021″,”imsak”:”04:10″,”subuh”:”04:20″,”terbit”:”05:36″,”dhuha”:”06:04″,”dzuhur”:”11:35″,”ashar”:”14:56″,”maghrib”:”17:28″,”isya”:”18:39″,”date”:”2021-07-31″}}}

            saat mau ambil jadwal sholatnya null terus
            JsonObject results = doc[“jadwal”];
            String subuh = results[“subuh”];

            JsonObject results = doc[“data”] [“jadwal”];
            String subuh = results[“subuh”];
            ini hasilnya juga null

            sonObject results = doc[“data”] [“koordinat”][“jadwal”];
            String subuh = results[“subuh”];
            null juga

          2. git

            {“status”:true,”data”:{“id”:”1607″,”lokasi”:”KAB. JEMBER”,”daerah”:”JAWA TIMUR”,”koordinat”:{“lat”:-8.168455555555555,”lon”:113.70053888888889,”lintang”:”8\u00b0 10′ 6.44\” S”,”bujur”:”113\u00b0 42′ 1.94\” E”},”jadwal”:{“tanggal”:”Sabtu, 31\/07\/2021″,”imsak”:”04:10″,”subuh”:”04:20″,”terbit”:”05:36″,”dhuha”:”06:04″,”dzuhur”:”11:35″,”ashar”:”14:56″,”maghrib”:”17:28″,”isya”:”18:39″,”date”:”2021-07-31″}}}

            ambil jadwal sholat gimana ya??

            JsonObject results = doc[“jadwal”];
            String subuh = results[“subuh”];
            hasil null

            JsonObject results = doc[“data”][“jadwal”];
            String subuh = results[“subuh”];
            hasil null

            JsonObject results = doc[“data”][“koordinat”][“jadwal”];
            String subuh = results[“subuh”];
            hasil null

Leave a Reply

Your email address will not be published. Required fields are marked *