Day: October 28, 2022

Compiling same NuttX libs to use on Linux

I was facing an issue with cJSON and MQTT-C on NuttX and decided to do a test: copy these libs source code from NuttX and then compile them on Linux and link my application with them (my app is just the modified mqttc example, so I will copy it too).

So I entered into nuttxspace/ directory and copied them to my home dir:

$ cp -a apps/netutils/cjson ~/
$ cp -a apps/netutils/mqttc/MQTT-C ~/
$ cp -a apps/examples/mqttc ~/

The cJSON was easy to compile:

$ cd ~/cjson/cJSON-1.7.12/
$ make all
$ sudo make install
$ sudo ldconfig

For MQTT-C I don’t need to compile, I just copied the files:

$ cd ~/MQTT-C
$ cp src/mqtt* ~/mqttc/
$ sudo cp include/mqtt* /usr/local/include/

Now I just entered inside ~/mqttc and compiled all the files:

$ cd ~/mqttc/

$ ls
Make.defs  Make.dep  Makefile  mqtt.c  mqttc_pub.c  mqtt_pal.c  README.md
$ gcc -c mqtt.c -o mqtt.o
$ gcc -c mqtt_pal.c -o mqtt_pal.o
$ gcc -c mqttc_pub.c -o mqttc_pub.o -DFAR= -DOK=0 -DERROR=-1 -DCONFIG_EXAMPLES_MQTTC_TXSIZE=256 -DCONFIG_EXAMPLES_MQTTC_RXSIZE=256

Finally just link all .o files:

$ gcc mqttc_pub.o mqtt.o mqtt_pal.o -o mqttc_pub -lcjson -lpthread

Lets to run the executable:

$ ./mqttc_pub -h mqtt.tago.io -p 1883 -n 3
Connecting to mqtt.tago.io:1883...
Success: Connected to broker!
{
	"variable":	"temperature",
	"unit":	"C",
	"value":	10
}
Success: Published to broker!
{
	"variable":	"temperature",
	"unit":	"C",
	"value":	10
}
Success: Published to broker!
{
	"variable":	"temperature",
	"unit":	"C",
	"value":	10
}
Success: Published to broker!

Disconnecting from mqtt.tago.io

NuttX is very similar to Linux, same libs, same errors:

Update: the issue was in tago.io system: it doesn’t accept the payload string ending with ‘\0’ see:

{
    "topic": "tago/data/post",
    "payload": "[{\"variable\":\"temperature\",\"value\":32}]\u0000",
    "qos": 0,
    "isHex": false,
    "bucket": "635bb8b9e68e400011343782",
    "messageId": 0
}

A workaround to fix the issue was removing that “+ 1” after the strlen() here:

  mqtterr = mqtt_publish(&mqtt_cfg.client, mqtt_cfg.topic,
                         mqtt_cfg.msg, strlen(mqtt_cfg.msg) + 1,
                         mqtt_cfg.qos);

I contacted TagoIO and they will fix it

Update: fixed the mqttc_pub.c compilation with suggestion from Petro Karashchenko