hi, i’m needing to use MQTT for some iot projects, and since there isn’t anything like that within uLisp as of now i’ve decided to try to embed the arduino mqttclient library.
however it’s my first extension, and a pretty complex at that…
right now, i’m trying to implement a (mqtt/subscribe topic fn) that’d register callbacks for certain topics which the mqtt receive loop will be able to use to call the appropriate handler.
i’m struggling to retrieve the lisp-side lambda and to register it somehow in the C side; i’ve done a first (mqtt/connect addr &port) method and i’m stuck with that subscribe one…
i’d appreciate any help in designing that kind of “complex” extension
#include <ArduinoMqttClient.h>
#include <WiFi.h>
extern WiFiClient client;
MqttClient mqtt(client);
#define URI_LENGTH 2048
#define TOPIC_LENGTH 1024
// (mqtt/init URI)
object *fn_MQTTInitClient(object *args, object *env) {
(void) env;
int port = 1883;
if (listlength(args) >= 3 || listlength(args) < 1) error2(PSTR("wrong arity"));
if (listlength(args) >= 2) {
port = checkinteger(second(args));
}
object *arg = first(args);
char uri[URI_LENGTH] = "";
cstring(arg, uri, URI_LENGTH);
if (!mqtt.connect(uri, port)) {
formaterr(arg, "cannot connect to mqtt broker, error: ~{", mqtt.connectError());
}
return nil;
}
// (mqtt/listen topic fn)
object *fn_MQTTListen(object *args, object *env) {
if (listlength(args) != 2) error2(PSTR("wrong arity"));
object *topic_obj = first(args);
char topic[TOPIC_LENGTH] = "";
cstring(topic_obj, topic, TOPIC_LENGTH);
object *fn = second(args);
return nil;
}