I am trying to send an MQTT payload to a broker in the following syntax:

env,type=BME280 temp=25.03,humid=50 (timestamp_from_sntp)

I am referring to the XDK-Snippets on GitHub from Apropos for SNTP

As a base I am using the SendDataOverMQTT from the IDE. Within the AppController.c file I added the following header files

#include "BCDS_NetworkConfig.h"
#include "Serval_Sntp.h"

As mentioned in one of the community thread for the similar topic I added the following lines

    /*UPDATE*/
    Ip_Address_T destAddr;
    Sntp_initialize();
    vTaskDelay(APP_MQTT_DATA_PUBLISH_PERIODICITY);
    Sntp_start(Ip_convertIntToPort(SNTP_SERVER_PORT), onTimeReceive);
    vTaskDelay(APP_MQTT_DATA_PUBLISH_PERIODICITY);

in the AppControllerFire before the while(1) block to initialize the SNTP.

Within the while(1) block I added

        /*Get timestamp*/
        NetworkConfig_GetIpAddress((uint8_t *) SNTP_SERVER_URL, &destAddr);

        retcode = Sntp_getTime(&destAddr, Ip_convertIntToPort(SNTP_SERVER_PORT),onSent);

 

where the onSent is the callback function exactly as mentioned in the Repository.

My onTimeReceive callback function looks like this:

/*UPDATE*/
static uint32_t currentTime = 0UL;
static void onTimeReceive(Ip_Address_T* sourceIp, Ip_Port_T sourcePort, uint32_t timestamp) {
	currentTime = timestamp;
	printf('%lu\n\r', timestamp);
}

Where currentTime is a static unsigned 32 bit value which I will finally add in while(1) block as follows:

 

int32_t length = snprintf((char *) publishBuffer, APP_MQTT_DATA_BUFFER_SIZE, publishDataFormat,
            		(sensorValue.Temp /= 1000),
                    (long int) sensorValue.RH,
		(unsigned long int) currentTime
		);

            MqttPublishInfo.Payload = publishBuffer;
            MqttPublishInfo.PayloadLength = length;

 

Unfortunately on the MQTT Broker I get the just a 0 instead of a timestamp (in image)

and the printf doesn't print data in the right fashion.

INFO | XDK DEVICE 2: WLANPersonalWPA2Connect : Connected to WPA network successfully. 
 INFO | XDK DEVICE 2: MqttEventHandler : Event - 0
 INFO | XDK DEVICE 2:         10 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2:         11 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2:         11 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2:         11 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 2:         12 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2:         12 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2:         12 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 2:         13 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2:         13 [EXH:2] M=0x03, E=0x04, S=0x2 (0x0384)
 INFO | XDK DEVICE 2:         13 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 2:         14 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 INFO | XDK DEVICE 2:         14 [EXH:2] M=0x03, E=0x04, S=0x2 (0x0384)
 INFO | XDK DEVICE 2:         14 [EXH:2] M=0x03, E=0x09, S=0x2 (0x0389)
 

Can you please guide me as to where I am going wrong with this example?

 

I am unable to grasp the API for SNTP code and why would it get the timestamp on Sntp_Start and not for Sntp_getTime()

0 (0 Votes)
RE: SNTP Timestamp in the message payload for MQTT Example
Answer
7/22/19 9:08 AM as a reply to Shantanoo Desai.

Hello,

Reg SNTP:

In Sntp_start function you have registered the callback onTimeReceive(). Sntp_start() will initialize the socket and listens to the given port. Whereas Sntp_getTime() will only get the current time from a remote server by sending an SNTP request. Data will be received in the registered callback onTimeReceive().

Reg MQTT message:

Can you tell me how you have defined the publishDataFormat ?

Example of using publishDataFormat,
 

const char *publishDataFormat = "\r\tHumidity : %ld\n";
int32_t length = snprintf((char *) publishBuffer, APP_MQTT_DATA_BUFFER_SIZE, publishDataFormt,(long int) sensorValue.RH);
MqttPublishInfo.Payload = publishBuffer;
MqttPublishInfo.PayloadLength = length;
MQTT_PublishToTopic(&MqttPublishInfo, MQTT_PUBLISH_TIMEOUT_IN_MS);

 

Please refer https://developer.bosch.com/web/xdk/mqtt

0 (0 Votes)
RE: SNTP Timestamp in the message payload for MQTT Example
Answer
7/22/19 9:20 AM as a reply to Padmapriya G.

Hi

I was actually able to solve this issue by using the code within the ENABLE_MQTT_SECURE

Within that code block there was already a call to the remote NTP Server which returned a timestamp.

This was the macro is commented out as well as the publishFormat
 

 const char *publishDataFormat = "env,stype=BME280 temp=%f,humid=%ld %llu";
    memset(&sensorValue, 0x00, sizeof(sensorValue));
//#if APP_MQTT_SECURE_ENABLE

    uint64_t sntpTimeStampFromServer = 0UL;

    /* We Synchronize the node with the SNTP server for time-stamp.
     * Since there is no point in doing a HTTPS communication without a valid time */
    do
    {
        retcode = SNTP_GetTimeFromServer(&sntpTimeStampFromServer, APP_RESPONSE_FROM_SNTP_SERVER_TIMEOUT);
        if ((RETCODE_OK != retcode) || (0UL == sntpTimeStampFromServer))
        {
            printf("AppControllerFire : SNTP server time was not synchronized. Retrying...\r\n");
        }
    }while (0UL == sntpTimeStampFromServer);

//     BCDS_UNUSED(sntpTimeStampFromServer); /* Copy of sntpTimeStampFromServer will be used be HTTPS for TLS handshake */
//#endif /* APP_MQTT_SECURE_ENABLE */

 

+2 (2 Votes)