C/C++ Question how to create a wrapper for a library with 3 callback ?

candide

Active Member
Licensed User
i wanted to create a wrapper for GSM_MQTT because i have a module SIM800 and i wanted to chech what we can do with a 8266 and MQTT.

it was supposed simple at start, but after more analysis, i found 3 callback on this library:
this library is able to launch 3 sub directly in Arduino programme, and i whould like the same with B4X.

in arduino .cpp file we have :
if (ReceivedMessageType == CONNACK)
{
MQTT.ConnectionAcknowledgement = MQTT.inputString[0] * 256 + MQTT.inputString[1];
if (MQTT.ConnectionAcknowledgement == 0)
{
MQTT.MQTT_Flag = true;
MQTT.OnConnect();
}
..........
else if (strstr(MQTT.inputString, "CONNECT") != NULL)
{
GSM_Response = 4;
MQTT.TCP_Flag = true;
mySerial.println("MQTT.TCP_Flag = True");
MQTT.AutoConnect();
MQTT.pingFlag = true;
MQTT.tcpATerrorcount = 0;
}
...............
MQTT.Message[MQTT.PublishIndex] = 0;
mySerial.println("'");
MQTT.MessageLength = MQTT.PublishIndex;
if (QoS == 1)
{
MQTT.publishACK(MessageID);
}
else if (QoS == 2)
{
MQTT.publishREC(MessageID);
}
MQTT.OnMessage(MQTT.Topic, MQTT.TopicLength, MQTT.Message, MQTT.MessageLength);
MQTT.MessageFlag = true;
}

in h file we have :
void AutoConnect(void);
void OnConnect(void);
void OnMessage(char *Topic, int TopicLength, char *Message, int MessageLength);


and a basic MQTT programme in Arduino is :
#include "GSM_MQTT.h"
#include <SoftwareSerial.h>
String MQTT_HOST = "test.mosquitto.org";
String MQTT_PORT = "1883";
SoftwareSerial mySerial(10, 11); // RX, TX

void GSM_MQTT::AutoConnect(void)
{

connect("qwertyuiop", 0, 0, "", "", 1, 0, 0, 0, "", "");
}
void GSM_MQTT::OnConnect(void)
{
publish(0, 0, 0, _generateMessageID(), "SampleTopic", "Hello");
}
void GSM_MQTT::OnMessage(char *Topic, int TopicLength, char *Message, int MessageLength)
{
mySerial.println(TopicLength);
mySerial.println(Topic);
mySerial.println(MessageLength);
mySerial.println(Message);
}
GSM_MQTT MQTT(20);

void setup()
{
MQTT.begin();
}

void loop()
{
if (MQTT.available())
{
.................
}
MQTT.processing();
}

=> in arduino we have just to add in code the 3 subs callback to be able to use it: it is simple and efficient.

and on B4X:
On a wrapper B4X, i have a problem is with the 3 callbacks, i don't what i can do :
- if i can use similar process by addition of the 3 sub in my wrapper (i didn't find a way to do that)
- or if i have to make a complex callback like we have in wrapper xMQTT, (i have to modify code of library in this case)
- or if i have to modify code of library, to make 3 loops in my wrapper to have at last 3 callbacks in B4X code. (i have to modify code of library )

i share with you arduino library, first wrapper i wrote (blocked at compilation) and code B4x used for test.

If someone can give an idea of code to add for the 3 callbacks management, he will be welcome.
 

Attachments

  • rGSM_MQTT_NOK.zip
    11.7 KB · Views: 78
Last edited:
Top