kpn-iot / thingsml-c-library

C library for ThingsML on your device
https://kpn-iot.github.io/thingsml-c-library/
MIT License
6 stars 4 forks source link

`SenMLBasePack::toJson`, change `dest` type from stream to byte array #9

Closed denizdanaei closed 3 years ago

denizdanaei commented 3 years ago

Hi there. I have a suggestion to make. Since it is specifically about this library, I didn't want to put it in the KPN forum. First, I will explain my project setup and then the problem I am facing.

my project details

I am working with Sodaq One / To send and receive data using Lora communication, I am using Sodaq_RN2483 library as suggested in learn.sodaq. I can send and receive raw payloads, hex format via these functions:

uint8_t Sodaq_RN2483::sendReqAck(uint8_t port, const uint8_t* payload, uint8_t size, uint8_t maxRetries){...}
uint16_t Sodaq_RN2483::receive(uint8_t* buffer, uint16_t size, uint16_t payloadStartPosition){...}

using thingML

Next, I tried using the thingsML library and send instructions using this format. I started with a simple example based on getting started and check the result on serialUSB.

#include <thingsml.h>
#include "Arduino.h"

#define debugSerial SERIAL_PORT_MONITOR

SenMLPack device;

SenMLDoubleRecord temperature(THINGSML_TEMPERATURE, SENML_UNIT_DEGREES_CELSIUS);

void setup() {
    while ((!SerialUSB) && (millis() < 10000)){
    // Wait 10 seconds for the Serial Monitor
    }
    debugSerial.begin(57600);
    device.add(temperature);
    delay(1000);
    debugSerial.println("start");
}

void loop(){
    int val = 23;
    temperature.set(val);

    device.toJson(debugSerial,SENML_RAW);        //print to screen
    device.toJson(debugSerial,SENML_HEX);        //print to screen

    delay(60000);
}

Which results in:

start
[{"i_":-24,"v":23.0}]5B7B22695F223A2D32342C2276223A32332E307D5D

The problem

Next, I try to send this data using the functionSodaq_RN2483::sendReqAck. The changes I made in the code above is as follow:

    int val = 23, BUFF_SIZE = 1024;  
    char buff[BUFF_SIZE];

    temperature.set(val);

    BUFF_SIZE = device.toJson(buff,BUFF_SIZE ,SENML_HEX);        //print to screen

Finally, using sendReqAck(1,(uint8_t*)buff, BUFF_SIZE,3) will send completely different data than the buff value. Since it is a stream, the data within the buff is not HEX format. Instead of sending 5B7B (the first two bytes), it would send 35423742.

The question/suggestion

So I thought changing the format would be more helpful. That is if the KPN decoder does not take care of it.

P.S.

For the sake of clarity and being brief, I left out the other parts of my code. I will share them if it's necessary.

josephverburg commented 3 years ago

Sorry for getting back to you only now, this issue missed my inbox.

Few things i notice:

You are overwriting your buffer size by the size of the output. You are using JSON while CBOR is much more efficient for LoRa. Please zero init your memory to prevent any problems from occuring using: char buff[BUFF_SIZE] ={0};

The first item might cause problems in your case, let me know if you need any more help. Closing for now.