How to use lib prayer time with esp32

Hello,

I got library prayertime.h from https://github.com/asmaklad/Arduino-Prayer-Times.
somehow if upload using board esp32, appear on serial monitor value is zero. see below.

but if uploaded with board esp8266 it's working.

here code

#include <stdio.h>
#include <Wire.h>
// get this library from https://github.com/PaulStoffregen/Time
#include <TimeLib.h>
#include "PrayerTimes.h"

double times[sizeof(TimeName)/sizeof(char*)];

void p(char *fmt, ... ){
        char tmp[128]; // resulting string limited to 128 chars
        va_list args;
        va_start (args, fmt );
        vsnprintf(tmp, 128, fmt, args);
        va_end (args);
        Serial.print(tmp);
}



void setup() {
  Serial.begin(9600);
  
}

void loop() {

  Serial.println("PTimes:");
  
  int dst=1;
  
  set_calc_method(ISNA);
  set_asr_method(Shafii);
  set_high_lats_adjust_method(AngleBased);
  set_fajr_angle(15);
  set_isha_angle(15);
  
  //MEKKA
  //float latitude=21.427378;
  //float longitude=39.814838;
  //get_prayer_times(year(), month(), day(), latitude, longitude, dst, times);
    get_prayer_times(2015, 5, 8, 46.9500f, 7.4458f, 2, times);
    
    Serial.print("YEAR:");     
    Serial.println(year());     
    Serial.print("MONTH:");     
    Serial.println(month());     
    Serial.print("DAY:");     
    Serial.println(day());     
      
  for (int i=0;i<sizeof(times)/sizeof(double);i++){
    char tmp[10];
    int hours, minutes;
    get_float_time_parts(times[i], hours, minutes);
    p("%d \t %10s %s \t %02d:%02d \n\r",i,TimeName[i],dtostrf(times[i],2,2,tmp),hours,minutes);
    /*
    Serial.print(i); 
    Serial.print(" \t "); 
    Serial.print(TimeName[i]); 
    Serial.print(" \t\t "); 
    Serial.print(times[i]);
    Serial.print(" \t ");     
    Serial.print(hours); 
    Serial.print(":"); 
    Serial.print(minutes); 
    Serial.println();
    */
  }
  delay(10000);
}


void getNextPTime(double &pTime, char* pTimeName)
{
  double times[sizeof(TimeName)/sizeof(char*)];
  double currTime=hour()+minute()/60.0;
  int i;
  
  set_calc_method(ISNA);
  set_asr_method(Shafii);
  set_high_lats_adjust_method(AngleBased);
  set_fajr_angle(15);
  set_isha_angle(15);

  //get_prayer_times(year(), month(), day(), 46.9500, 7.4458, 1, times);
  get_prayer_times(year(), month(), day(), 46.9500, 7.4458, 2, times);
  for (i=0;i<sizeof(times)/sizeof(double);i++){
    if (times[i] >= currTime) break;
  }
  if ( (times[i]-currTime) <0 ) {    
    i=0;
  }
  pTime=times[i];
  sprintf(pTimeName,"%s",TimeName[i]);
}

could you help me please?

Thanks

Please don't send pictures of the code, you must edit your post and replace it with the sketch text, included inside the "code" tags (select the code then click on the "<CODE/>" button) to make it more readable and tested.
Do the same for the sketch serial output. Thanks.

updated

Better now, but please make also the output more readable, as:

Do the same for the sketch serial output.

Anyway, the sketch appears to be the library example code (always clearly specify the code used), so it looks like an ESP32 compatibility issue, but I can't help you mainly because I don't have one to test it with.
I hope someone colud give you an answer, but if it's a compatibility problem of that library I think you should wait for an answer from the author (I see you have already raised that issue on Github).

PS: the author stated he tested the library with Mega 2560 and Uno only, so I think ESP32 achitecture and features are too different to be fully compatible. In this case, I'm afraid but I think only the author could make the necessary changes. Or you should switch to ESP8266.

How can I fix the issue with the "prayer times" library returning zero values on an ESP32 board, while it works fine on an ESP8266? Is this a compatibility problem with the ESP32, and what are the possible solutions or workarounds to make it work on the ESP32?

Yes that right.
i dont know why on board esp32 not working.
may be architure on esp32 or something like that

I can confirm that it also does not work on my ESP32 dev board

The error it gives is "Stack smashing protect failure!"
It has something to do with that 'p' function.
.....

Crappy name for a function, BTW.

ESP32 guards it's stack more rigorously than those processors. The ...nprintf() family of functions don't null-terminate if they processes n or more characters, they just truncate at whatever the n'th character is. So, maybe give this a shot:

void p(char *fmt, ... ) {
  char tmp[128]; // resulting string limited to 128 chars
  tmp[127] = '\0';  // End with Null terminator
  va_list args;
  va_start (args, fmt );
  vsnprintf(tmp, 127, fmt, args);  // don't overwrite ending Null is string too long
  va_end (args);
  Serial.print(tmp);
}

Well the OP may have lost interest but I tried your fix and it did not work.
However I don't plan on continuing with the debugging here.

1 Like

Yes, they do, unless the implementation is broken.

I'll have to dig up where I saw that they don't. Could be outdated info.

My bad, I was thinking of strncpy().

Hi @Zx80,

Thanks you.
I wil try again.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.