open-tcp

xod-dev/esp8266-mcu/open-tcp

Open a TCP connection to the specified server Possible errors: — Can't open TCP connection
open-tcp
@/open-tcp
Open a TCP connection to the specified server Possible errors: — Can't open TCP connection
INET@/esp8266-mcu-inet
An internet connection
HOSTstring
Server name
PORTnumber
A destination server port number
CONNpulse
Open the connection
open-tcp
INET'
SOCK
DONE
INET
HOST
PORT
CONN
DONEpulse
Pulses when the connection is successfully open
SOCKxod/net/socket
A socket
INET'@/esp8266-mcu-inet
An internet connection
To use the node in your project you should have the xod-dev/esp8266-mcu 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 evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_CONN
#pragma XOD error_raise enable

#include <ESP8266WiFi.h>

node {
    WiFiClient client;

    void evaluate(Context ctx) {
        if (isSettingUp()) {
            // Put the pointer to WiFiClient in the sockets array once on the setup
            auto inet = getValue<input_INET>(ctx);
            // Find a free index
            uint8_t socketIdx = 0;
            uint8_t maxIdx = sizeof(inet.sockets) / sizeof(*inet.sockets);
            while (socketIdx < maxIdx) {
                auto curPointer = inet.sockets[socketIdx];
                if (curPointer == nullptr || curPointer == &client) break;
                socketIdx++;
            }
            if (socketIdx >= maxIdx) {
                // No free sockets available
                raiseError(ctx);
            } else {
                // Store the pointer
                inet.sockets[socketIdx] = &client;
                // And emit the index and updated INET once
                emitValue<output_SOCK>(ctx, socketIdx);
                emitValue<output_INETU0027>(ctx, inet);
            }
            return;
        }

        if (!isInputDirty<input_CONN>(ctx))
            return;

        auto serverName = getValue<input_HOST>(ctx);
        auto len = length(serverName);
        char serverNameBuff[len + 1];
        dump(serverName, serverNameBuff);
        serverNameBuff[len] = '\0';

        auto port = getValue<input_PORT>(ctx);

        if (client.connect(serverNameBuff, port)) {
            emitValue<output_DONE>(ctx, 1);
        } else {
            raiseError(ctx);
        }
    }
}