0

I'm working on a project with the PIC-IoT WG wifi development board. I'm trying to modify the demo code to be able to use it on my own servers. The demo uses code like this in several places:

#include <time.h>
static void connectMQTT()
{
   uint32_t currentTime = time(NULL);

   if (currentTime > 0)
   {
      updateJWT(currentTime + UNIX_OFFSET);   
      MQTT_CLIENT_connect();
   }      
   debug_print("CLOUD: MQTT Connect");

   sendSubscribe = true;
}

and every time I turn it on, for 15-20 minutes it tries connecting to the server, but these parts of the code don't run, as time(NULL) seems to return 0.

As far as I understand, in case of PIC microcontrollers time(NULL) should return the number of instruction cycles.

Why does it return 0? Why does it stop returning 0?

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
Dave12311
  • 29
  • 5
  • 1
    `in case of PIC microcontrollers time(NULL) should return the number of instruction cycles` where did you find such information? Most probably `time(NULL)` is not implemented on your platform and it's just `time_t time(..) { return 0; }` Inspect with your debugger ans see it there's anything inside `time()` function. – KamilCuk Oct 02 '19 at 22:14
  • @KamilCuk It's from the documentation of the compiler I'm using (XC16) `Returns the calendar time encoded as a value of time_t. If the target environment cannot determine the time, the function returns -1 cast as a time_t. By default, the 16-bit compiler returns the time as instruction cycles.` So worst case I should be getting -1, but I only get 0 when it doesn't work or the actual time if it works. – Dave12311 Oct 02 '19 at 22:35
  • The example is rather strange. `time(0); /* start time */`. Maybe try running the example in the docs and see if it prints correctly? Also they use `time(&var)` not the return value in the example, try that? – KamilCuk Oct 02 '19 at 22:49
  • @KamilCuk I tried the code from the example, and it's returning 0 too... – Dave12311 Oct 02 '19 at 22:58

1 Answers1

0

In general, time(NULL) from time.h returns a time_t and represents the number of seconds since the epoch (1970 Jan 1 00:00:00). Putting the output of time() into a uint32 could be causing issues on your "servers". Change it to a time_t and see if it fixes the issue.

Topher
  • 451
  • 2
  • 8