Use service_lora_set_timereq(1); to update the time ex examples of best practice rui3

Hello Beegee,

I want to add the sensor time since I want to implement a message queue. Since if I lose communication with the gateway I want to resend saved messages with exact time and data.

checking there is a way to get the time to keep the sensor updated and with the ltime api we can use the time.

when I try to use service_lora_set_timereq(1); In the void setud I don’t see that it executes the function so that when the computer restarts it updates the time and date and in this way always has the correct time.

I think I’m wrong about something, could you please guide me?

void setup()

{
   Serial.begin(9600);
	// Setup for LoRaWAN
	if (api.lorawan.nwm.get() == 1)
	{
		g_confirmed_mode = api.lorawan.cfm.get();

		g_confirmed_retry = api.lorawan.rety.get();

		g_data_rate = api.lorawan.dr.get();

		// Setup the callbacks for joined and send finished
		api.lorawan.registerRecvCallback(receiveCallback);
		api.lorawan.registerSendCallback(sendCallback);
		api.lorawan.registerJoinCallback(joinCallback);
		api.lorawan.registerLinkCheckCallback(linkcheckCallback);
	}
	else // Setup for LoRa P2P
	{
		api.lora.registerPRecvCallback(recv_cb);
		api.lora.registerPSendCallback(send_cb);
		api.lora.registerPSendCADCallback(cad_cb);
	}

	pinMode(LED_GREEN, OUTPUT);
	digitalWrite(LED_GREEN, HIGH);
	pinMode(LED_BLUE, OUTPUT);
	digitalWrite(LED_BLUE, HIGH);

	pinMode(WB_IO1, OUTPUT);
	digitalWrite(WB_IO1, LOW);
	pinMode(WB_IO2, OUTPUT);
	digitalWrite(WB_IO2, HIGH);

	service_lora_set_timereq(1);
	
	// Start Serial
	Serial.begin(115200);

	// Delay for 5 seconds to give the chance for AT+BOOT
	delay(5000);

	api.system.firmwareVersion.set("HW_Firmware_V1.0.1");

	Serial.println("HardaweSignals Sensor calidad de aire");
	Serial.println("------------------------------------------------------");
	Serial.println("Setup the device with WisToolBox or AT commands before using it");
	Serial.printf("Version %s\n", api.system.firmwareVersion.get().c_str());
	Serial.println("------------------------------------------------------");
	
	// Initialize module
	Wire.begin();

	// Register the custom AT command to get device status
	if (!init_status_at())
	{
		MYLOG("SETUP", "Add custom AT command STATUS fail");
	}

	// Register the custom AT command to set the send interval
	if (!init_interval_at())
	{
		MYLOG("SETUP", "Add custom AT command Send Interval fail");
	}
  
    

	// Get saved sending interval from flash
	get_at_setting();

	digitalWrite(LED_GREEN, LOW);

	// Create a timer.
	api.system.timer.create(RAK_TIMER_0, sensor_handler, RAK_TIMER_PERIODIC);
	if (custom_parameters.send_interval != 0)
	{
		// Start a timer.
		api.system.timer.start(RAK_TIMER_0, custom_parameters.send_interval, NULL);
	}


	// Check if it is LoRa P2P
	if (api.lorawan.nwm.get() == 0)
	{
		digitalWrite(LED_BLUE, LOW);

		sensor_handler(NULL);
	}

	if (api.lorawan.nwm.get() == 1)
	{
		if (g_confirmed_mode)
		{
			MYLOG("SETUP", "Confirmed enabled");
		}
		else
		{
			MYLOG("SETUP", "Confirmed disabled");
		}

		MYLOG("SETUP", "Retry = %d", g_confirmed_retry);

		MYLOG("SETUP", "DR = %d", g_data_rate);
	}

	// Enable low power mode
	api.system.lpm.set(1);

	// If available, enable BLE advertising for 30 seconds and open the BLE UART channel
#if defined(_VARIANT_RAK3172_) || defined(_VARIANT_RAK3172_SIP_)
// No BLE
#else
	Serial6.begin(115200, RAK_AT_MODE);
	api.ble.advertise.start(30);
#endif

	// Check if sensors are connected and initialize them
	has_rak12037 = init_rak12037();
	if (has_rak12037)
	{
		Serial.println("+EVT:RAK12037");
	}
	
	digitalWrite(LED_BLUE, LOW);

	// Power down the sensors
	digitalWrite(WB_IO2, LOW);

}

If I perform the AT command manually it works for me but for a production environment this should be executed every time the team performs a join.

image

Manual mode:

image

Best Regards

Do you enable time req with

		// Enable Timerequest
		api.lorawan.timereq.set(1);

And to check whether the time req was successful register a callback (RUI3 V4.1.1)

void timeReqCallback(int32_t status)
{
	if (status == GET_DEVICE_TIME_OK)
	{
		MYLOG("TREQ", "Get device time success");
	}
	else if (status == GET_DEVICE_TIME_FAIL)
	{
		MYLOG("TREQ", "Get device time fail");
	}
}

void setup(void)
{
		// ....
		api.lorawan.registerTimereqCallback(timeReqCallback);
		// ....
}

Log output:

------------------------------------------------------
Setup the device with WisToolBox or AT commands before using it
Version RUI3-Low-Power-V1.0.0
------------------------------------------------------
[AT_CMD] Got flag: AA
[AT_CMD] Got send interval: 00007530
[AT_CMD] Send interval found 30000
[SETUP] Confirmed disabled
[SETUP] Retry = 1
[SETUP] DR = 2
[SETUP] LINKCHECK = 0
Current Work Mode: LoRaWAN.
+EVT:JOINED
[JOIN-CB] LoRaWan OTAA - joined! 

[UPLINK] Start
[UPLINK] Sending 4 bytes
0174018C
[UPLINK] LPW Packet enqueued
[TX-CB] TX status 0
+EVT:TX_DONE
+EVT:TIMEREQ_OK
[TREQ] Get device time success
[RX-CB] RX, port 0, DR 2, RSSI -41, SNR 8

Verifying what is indicated at the moment it is not working for me and I think it is because of the Board manager. Currently I maintain the RUI3 4.1.1 version, it sends me the following error

image

void timeReqCallback(int32_t status)
{
	if (status == GET_DEVICE_TIME_OK)
	{
		MYLOG("TREQ", "Get device time success");
	}
	else if (status == GET_DEVICE_TIME_FAIL)
	{
		MYLOG("TREQ", "Get device time fail");
	}
}

void setup()

{
   Serial.begin(9600);
	// Setup for LoRaWAN
	if (api.lorawan.nwm.get() == 1)
	{
		g_confirmed_mode = api.lorawan.cfm.get();

		g_confirmed_retry = api.lorawan.rety.get();

		g_data_rate = api.lorawan.dr.get();

		// Enable Timerequest
		api.lorawan.timereq.set(1);

		// Setup the callbacks for joined and send finished
		api.lorawan.registerRecvCallback(receiveCallback);
		api.lorawan.registerSendCallback(sendCallback);
		api.lorawan.registerJoinCallback(joinCallback);
		api.lorawan.registerLinkCheckCallback(linkcheckCallback);
		api.lorawan.registerTimereqCallback(timeReqCallback);

checking the API and I see that registerRecvCallback is not declared

attached full .ino
RUI3-WisBlock-Kit-3 Rak12037.ino (10.8 KB)

It works in my V4.1.1 installation. V4.1.1 was released a few days ago in Github.

Are you on staging BSP version or on latest released BSP version?

In Additional Boards Managers URLs it should be

https://raw.githubusercontent.com/RAKWireless/RAKwireless-Arduino-BSP-Index/main/package_rakwireless_com_rui_index.json

If you are using the V4.1.1 staging, make sure that it is at least v4.1.1_282

Thank you for your quick response.

The problem with the RUI3 4.1.1 version has already been resolved.

but I don’t understand what I am doing wrong I am declaring api.lorawan.timereq.set(1); at the start of void setup(). but when I restart the computer it does not enable timereq.

where in your code do you place
api.lorawan.timereq.set(1);

You have to request it when you send a packet. It will be performed only once. If you want to get the time every time, you need to call api.lorawan.timereq.set(1); every time you send a packet.

In addition, it works only after you have successfully joined the network. The network time is not transmitted during the join request/accept sequence.

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