Millis() on RAK4631


I am modifying the example code for a periodic loraWAN update using the RUI version of the RAK4631 Wisblock.

This code comes from here:

void loop()
	static uint64_t last = 0;
	static uint64_t elapsed;
	static uint32_t execution_time = 0;
	uint64_t start = 0;
	Serial.printf("last execution = %ums\n", last);

	if ((elapsed = millis() - last) > OTAA_PERIOD - execution_time) {
		start = millis();
		Serial.printf("execute at = %ums\n", elapsed);
		last = millis();
		execution_time = (last - start);
	Serial.printf("elapsed %ums..",  execution_time);
	Serial.printf("Try sleep %ums..", OTAA_PERIOD - execution_time);
	api.system.sleep.all(OTAA_PERIOD - execution_time);

I have two questions.

First, if I understand correctly, the milis() function returns a 64 bit unsigned int, so I realistically don’t have to worry about overflow in this scenario.

Second, I noticed without the guard on line 8, the loop occasionally runs a few times before sleeping. I assume this is normal and why the guard is there. Is that correct?


Hi @hobie ,

It seems the example I uploaded needs to be updated to have longer transmit time interval. It has 10seconds which (1) there could be timing conflict on the sleep since it is too short and (2) it is too fast for LoRaWAN transmission so it not possible to be used.

I did test and increase the time to 61seconds and I verified that the device sleep until it is time to send an uplink.

Be careful if you try to use “elapsed”
What happens when millis() rolls over in however many days it is, where
millis() == 1 and last == some large number prior to the roll over

While elapsed is unsigned, seems it will be a rather large number.

But your not using it in this sample so may not matter.

1 Like

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