esp32-connect

jesuso/esp32-wifi/esp32-connect

Establish an internet connection via Wi-Fi using ESP32.
esp32-connect
@/esp32-connect
Establish an internet connection via Wi-Fi using ESP32.
SSIDstring
Name of the Wi-Fi network
PWDstring
Wi-Fi network password
CONNpulse
Establish the connection
esp32-connect
SSID
PWD
CONN
INET
DONE
DONEpulse
Pulses on a successful connection
INET@/esp32-inet
To use the node in your project you should have the jesuso/esp32-wifi 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/espressif/arduino-esp32"

#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_CONN
#pragma XOD error_raise enable

#include <WiFi.h>
#include <WiFiClient.h>

node {
    #define WIFI_TIMEOUT_MS 20000

    WiFiClient wifiClient;

    meta {
        using Type = WiFiClient*;
    }

    void evaluate(Context ctx) {
        if (!isInputDirty<input_CONN>(ctx))
            return;

        auto ssid = getValue<input_SSID>(ctx);
        char _ssid[length(ssid) + 1] = { 0 };
        dump(ssid, _ssid);

        auto password = getValue<input_PWD>(ctx);
        char _password[length(password) + 1] = { 0 };
        dump(password, _password);

        WiFi.mode(WIFI_STA);
        WiFi.begin(_ssid, _password);

        unsigned long startAttemptTime = millis();

        while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < WIFI_TIMEOUT_MS) {
            delay(100);
        }

        if (WiFi.status() != WL_CONNECTED) {
            raiseError(ctx); // Connection failed
        } else {
            emitValue<output_DONE>(ctx, 1);
            emitValue<output_INET>(ctx, &wifiClient); // Emit the WiFiClient object
        }
    }
}