mqtt-device

bradzilla84/pubsub-mqtt/mqtt-device

MQTT Device setup. Username and Password are optional.
mqtt-device
@/mqtt-device
MQTT Device setup. Username and Password are optional.
INETxod-dev/esp8266-mcu/esp8266-mcu-inet
Serverstring
MQTT Server
IDstring
The ID of This device to show in MQTT
Usernamestring
Leave Blank for NONE
Passwordstring
Leave Blank for NONE
mqtt-device
INET
Server
ID
Username
Password
MQTT
MQTT@/mqtt-device
To use the node in your project you should have the bradzilla84/pubsub-mqtt library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

#pragma XOD require "https://github.com/256dpi/arduino-mqtt"

// Include C++ library:
{{#global}}
#include <MQTT.h>
String Buff [10][2]; //Setup buffer
int Buffloc = 0; //current buffer location

void messageReceived(String &topic, String &payload) {
    if(Buffloc > 10){Buffloc = 0;}
    Buff[Buffloc][0] = topic; //Populate buffer
    Buff[Buffloc][1] = payload;
    Buffloc = Buffloc +1;
}
{{/global}}


struct State {
    uint8_t mem[sizeof(MQTTClient)];
    WiFiClient client;
};

// Define our custom type as a pointer on the class instance.
using Type = MQTTClient*;

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
    // It should be evaluated only once on the first (setup) transaction
    if (isSettingUp()){

    auto state = getState(ctx);

    // Create a new object in the memory area reserved previously.
    Type MQTT = new (state->mem) MQTTClient();

        auto server = getValue<input_Server>(ctx);
        auto serverLength = length(server);
        char _server[serverLength + 1];
        dump(server, _server);
        _server[serverLength] = '\0';



        auto username = getValue<input_Username>(ctx);
        auto usernameLength = length(username);
        char _username[usernameLength + 1];
        dump(username, _username);
        _username[usernameLength] = '\0';

        auto password = getValue<input_Password>(ctx);
        auto passwordLength = length(password);
        char _password[passwordLength + 1];
        dump(password, _password);
        _password[passwordLength] = '\0';

        auto id = getValue<input_ID>(ctx);
        auto idLength = length(id);
        char _id[idLength + 1];
        dump(id, _id);
        _id[idLength] = '\0';

    MQTT->begin(_server, state->client);
    MQTT->onMessage(messageReceived);
    while (!MQTT->connect(_id, _username, _password)) {
      delay(1000);
        }
    }
    auto state = getState(ctx);
    auto MQTT = reinterpret_cast<MQTTClient*>(state->mem);
    MQTT->loop();
    delay(10);
    emitValue<output_MQTT>(ctx, MQTT);
}