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 on nuttx apps/ directory and copied then 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
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
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: