The nikolaindustry-realtime library enables ESP32 devices to communicate securely with a nikolaindustry-realtime server, auto-reconnect on disconnection, switch to AP mode on repeated Wi-Fi failures, and handle real-time JSON messaging.
- Auto-connect to nikolaindustry-realtime over SSL
- JSON-based messaging (using
ArduinoJson) - Callback for incoming messages
- Callback for connection status changes
- Retry logic with exponential backoff
- Automatic switch to AP mode after multiple Wi-Fi failures
- AP mode timeout and recovery
- Send JSON messages to specific target devices
#include <WiFi.h>
#include "nikolaindustry-realtime.h"
const char* ssid = "SENSORFLOW";
const char* pass = "12345678";
const char* deviceid = "device-123";
nikolaindustryrealtime realtime;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nβ
WiFi connected");
realtime.begin(deviceid);
realtime.setOnMessageCallback([](JsonObject& msg) {
if (!msg.containsKey("payload")) return;
JsonObject payload = msg["payload"];
JsonArray commands = payload["commands"];
for (JsonObject commandObj : commands) {
const char* command = commandObj["command"];
if (strcmp(command, "GPIO_MANAGEMENT") == 0) {
JsonArray actions = commandObj["actions"];
for (JsonObject actionObj : actions) {
const char* action = actionObj["action"];
JsonObject params = actionObj["params"];
// Extract parameters
int gpio = atoi(params["gpio"] | "0");
String pinmodeStr = params["pinmode"] | "OUTPUT";
String statusStr = params["status"] | "LOW";
// Resolve pin mode
int mode = OUTPUT;
if (pinmodeStr == "INPUT") mode = INPUT;
else if (pinmodeStr == "INPUT_PULLUP") mode = INPUT_PULLUP;
// Resolve output state
int value = LOW;
if (statusStr == "HIGH") value = HIGH;
// Apply pin configuration
pinMode(gpio, mode);
if (strcmp(action, "ON") == 0 || strcmp(action, "OFF") == 0) {
digitalWrite(gpio, value);
Serial.printf("Pin %d set to %s with mode %s\n", gpio, statusStr.c_str(), pinmodeStr.c_str());
}
}
}
}
});
realtime.setOnConnectionStatusChange([](bool connected) {
Serial.println(connected ? "π’ WS Connected" : "π΄ WS Disconnected");
});
}
void loop() {
realtime.loop();
}
{
"targetId": "device-123",
"payload": {
"commands": [
{
"command": "GPIO_MANAGEMENT",
"actions": [
{
"action": "ON",
"params": {
"gpio": "12",
"pinmode": "OUTPUT",
"status": "HIGH"
}
}
]
}
]
}
}
{
"targetId": "device-123",
"payload": {
"commands": [
{
"command": "GPIO_MANAGEMENT",
"actions": [
{
"action": "OFF",
"params": {
"gpio": "14",
"pinmode": "OUTPUT",
"status": "LOW"
}
}
]
}
]
}
}
{
"targetId": "device-123",
"payload": {
"commands": [
{
"command": "GPIO_MANAGEMENT",
"actions": [
{
"action": "ON",
"params": {
"gpio": "25",
"pinmode": "INPUT_PULLUP",
"status": "LOW"
}
}
]
}
]
}
}
{
"targetId": "device-123",
"payload": {
"commands": [
{
"command": "GPIO_MANAGEMENT",
"actions": [
{
"action": "ON",
"params": {
"gpio": "5",
"pinmode": "OUTPUT",
"status": "HIGH"
}
},
{
"action": "OFF",
"params": {
"gpio": "18",
"pinmode": "OUTPUT",
"status": "LOW"
}
}
]
}
]
}
}
{
"targetId": "device-123",
"payload": {
"commands": [
{
"command": "GPIO_MANAGEMENT",
"actions": [
{
"action": "ON",
"params": {
"gpio": "4",
"pinmode": "INPUT",
"status": "LOW"
}
}
]
}
]
}
}
This configures the pin, but the status has no real effect since it's an input. Still accepted for uniformity.
Initializes Wi-Fi, connects to nikolaindustry-realtime, and sets up auto-reconnect.
ssid: Wi-Fi network name.password: Wi-Fi password.deviceId: Unique ID passed to nikolaindustry-realtime as query parameter.
Must be called in the loop() function to process nikolaindustry-realtime events and manage AP/Wi-Fi recovery.
Sends a raw JSON object over nikolaindustry-realtime. Useful for full control of payload structure.
Builds and sends a JSON payload with a targetId. Simplifies common messaging.
targetId: Destination device.payloadBuilder: Lambda that fills thepayloadobject.
Example:
realtime.sendTo("target-device", [](JsonObject &payload) {
payload["temp"] = 25;
});Registers a callback that triggers when a valid JSON message is received.
Registers a callback that notifies when nikolaindustry-realtime connects or disconnects.
true= connectedfalse= disconnected
Switches the ESP32 to AP mode with optional SSID and password. If none are provided:
- Default SSID:
NIKOLAINDUSTRY_Setup - Default Password:
xVv9O9B4tV
Stops AP mode and reverts to station mode (STA).
Sets AP mode timeout in milliseconds. After the timeout, AP mode stops automatically.
Returns true if AP mode is active.
Returns true if the nikolaindustry-realtime is currently connected.
- Retries Wi-Fi with exponential backoff (starting at 5s, capped at 60s).
- After
maxWifiRetriesBeforeAPattempts (default: 5), it switches to AP mode. - If
setAPTimeout()is used, it will exit AP mode after the timeout and retry Wi-Fi.