This sketch allows ESP32 devices connect to the Collab-Hub.io ecosystem. Devices connect to a Collab-Hub server over WebSockets (Socket.IO framing), join the iot room, and send/receive Collab-Hub Control, Event, and Chat messages.
Contact Nick Hwang (nickthwang at gmail) with any questions.
- Connects to Collab-Hub via ws:// or wss:// (TLS)
- Joins a room and identifies with a fixed or auto-generated ESP32 username
- Sends/receives control, event, and chat messages
- Modular: user logic in
user_script.cpp, config inconfig.h
CollabHubESP32/CollabHubESP32.ino: Core logic (do not edit for normal use)CollabHubESP32/config.h: Edit for WiFi, server, and room settingsCollabHubESP32/user_script.cpp&.h: Edit for your custom logic, handlers, and hardware codeCollabHubESP32/SioClient.*,WsClient.*: Protocol internals (do not edit)platformio.ini: PlatformIO build config (Optional)
- Get Started
- Connect to Collab-Hub Server
- Customizing your own logic
- Troubleshooting
- File Structure & Customization
-
Download this repo to your computer and unzip.
-
Download and install Arduino IDE
-
Install ESP32 core: Tools → Board → Boards Manager (Shift+Command+B) → search "esp32" by Espressif Systems → Install
-
Install ArduinoJson library: Tools → Manage Libraries (Shift+Command+I) → search "arduinojson"
-
Open the sketch folder:
CollabHubESP32 -
Edit
config.hfor WiFi and hub settings (in theCollabHubESP32folder) - Details here -
Select your board and the correct
/dev/cu.*port - Troubleshooting tips below -
Upload the Sketch (the triagular play button) and open Serial Monitor at 115200 baud
-
At the top left corner of the ArduinoIDE, click the button that says 'Select Board'. It will have the USB symbol within the button box. It might already have the name of a board in the box, like the image below.
-
If you don't already see your board connected the correct port, Select 'Connect other board and port...'
-
Search ESP in the search bar and select your board. This tutorial is using
ESP32 Dev Module.
-
Select the port that your board is on. (Make sure your board is plugged into your machine.) You should see a list of available ports with one that matches the list below:
Typical ESP32 ports:
/dev/cu.SLAB_USBtoUART(CP210x)/dev/cu.wchusbserial*(CH34x)/dev/ttyUSB0or/dev/ttyACM0(Linux)
**If you are machine is not recognizing your board, your list might look like the image below:
**If you are machine is recognizing your board, your list might look like the image below:
-
If you do not see any available ports, refer to the section below.
-
Select
OK. -
You should be able to upload your sketch (the triagular play button) and open Serial Monitor at 115200 baud.
Edit CollabHubESP32/config.h:
WIFI_SSID/WIFI_PASS: Your WiFi SSID and password
HUB_DEVICE_USERNAME: Recommended — Set a fixed, stable username for your ESP32 device (e.g.,esp32_stage_left,esp32_sensor_01). If left blank, a unique name is auto-generated from the device MAC address.
For Local Network (Local Collab-Hub Server or RPi):
#define HUB_HOST "192.168.1.100" // IP of your local server
#define HUB_PORT 3000 // Typical local port
#define USE_TLS false // No TLS needed on local networkFor Remote Server (Cloud-Hosted Collab-Hub):
#define HUB_HOST "server.collab-hub.io" // Remote hostname/IP
#define HUB_PORT 443 // HTTPS/WSS port
#define USE_TLS true // Enable TLS for secure remote connectionHUB_NAMESPACE: Typically/hub(the Socket.IO namespace)IOT_ROOM: Defaultiot(the room to join on the server)HUB_AUTH_USERNAME/HUB_AUTH_PASSWORD: Leave blank for now — Auth support coming in next build. For current build, leave both empty.
Best Practice: Set HUB_DEVICE_USERNAME to a descriptive name in config.h:
#define HUB_DEVICE_USERNAME "esp32_stage_left"This ensures your ESP32 always connects with a human-readable, stable identity.
Username Rules:
- Use only lowercase letters
a–z, numbers0–9, and underscores_ - Maximum 32 characters
- Avoid leading or trailing underscores
- Recommended style:
esp32_<location>_<number>(e.g.,esp32_workshop_01)
If HUB_DEVICE_USERNAME is blank, the client auto-generates a unique name in the format esp32_XXXXXX (where XXXXXX is derived from your device's MAC address).
Important: For this build, HUB_AUTH_USERNAME and HUB_AUTH_PASSWORD should remain blank. Guest authentication will connect your device with the configured username above.
-
After you have customized your
WIFI_SSID/WIFI_PASSinCollabHubESP32/config.h, upload the sketch to the board using ArduineIDE. -
The board should restart and try to connect to your WIFI.
-
After connecting to your WIFI, the ESP32 will try to connect to the Collab-Hub server. The URL/destination of Collab-Hub server is a value set at
HOST-HUBinCollabHubESP32/config.h. For testing and general purposes, you will not have to change this.
The image above show the Serial printout within the ArduinoIDE. This message is a customizable message called as an OnConnection event.
-
You can check your connection to the Collab-Hub server with the Collab-Hub webclient (http://server.collab-hub.io) and see the ESP32-[username].
-
CONGRATULATIONS! You are connected to the Collab-Hub Server.
-
If the ESP32 is connected to the ArduinoIDE, you can see the serial readout of incoming messages.
-
Open a browser window of the Collab-Hub Webclient (https://server.collab-hub.io) and click the 'event webEvent3' or slide the 'control webSlider3' UI elements. These two messages are 'PUSH ALL' type and targets, which means these messages go all users in the namespace, and the ESP32 should receive them automatically. You can type a chat message as well from the Webclient-- set the target 'all' (no quotes) or the username of your ESP32.
-
After you clicked and used the slider, look back at the Arduino Serial Monitor window. You should see similar printouts like the image below:

You should see the Event and Control information. Every Collab-Hub message has a
Header, aFrom, and sometimes aValueportion to the messages.
-
In the existing, you can send all three types of messages being using the GPIO.
-
We'll cover triggering those messages and then dive into the code structure and how you might edit the code for your purposes.
-
Currently, the user_script is set to trigger messages when GPIO pins are connected to ground.
- GPIO 0 - send an event
espEvent - GPIO 4 - increment an integer, send that integer as a control message with header
espControl - GPIO 16 - increment the integer, send that integer as a control message with header
espControl - GPIO 17 - send a chat message
- GPIO 0 - send an event
-
You should be able to connect the individiual pins to Ground and should see a Serial printout in the ArduinoIDE
-
You should be able to see the event, control values, and chat appear at the top to the Webclient.
Youtube: Intro Video: (https://youtube.com/shorts/QIJJktYq_Q0)
# Wiring:
1. Connect one side of a pushbutton to the selected GPIO pin (e.g., GPIO 0).
2. Connect the other side of the button to GND.
3. The pin is set as INPUT_PULLUP, so pressing the button pulls it LOW and triggers the event.
You can change most of this script run your own logic. Most those files do not need to change, but understanding the Setup and Loop structure will help you structure your usage.
Summary of Execution Order
CollabHubESP32.ino
|-(Boot)
|-(Setup)
|-userScriptSetup() (user_sript.cpp)
|-WIFI Setup and Connect (config.h)
|-Collab-Hub Connect (SioClient.cpp/.h)
|--Set Username
|--Join 'iot' room
|--Subscribe to all Events and all Control
|--Establish Event, Control, Chat message handlers
|
|-(Loop)
| |-Connection/Heartbeat maintainance
| |-userScriptLoop() user_sript.cpp
|___|The first section of the file shown below should not be changed and setup the shape of messages to send to the server. You can change most of the other code beneath that. You can change the content of userScriptSetup() and userScriptLoop(), do not remove these functions completely.
#include "SioClient.h"
#include "user_script.h"
#include <ArduinoJson.h>
extern SioClient sio;
// ================= USER MESSAGE EMITTERS (Don't Edit)=================
void emitControl(const char *header, float value, const char *mode, const char *target)
{
StaticJsonDocument<256> doc;
doc["header"] = header;
doc["values"] = value;
doc["mode"] = mode;
doc["target"] = target;
String s;
serializeJson(doc, s);
sio.emit("control", s.c_str());
}
void emitEvent(const char *header, const char *payload)
{
StaticJsonDocument<256> doc;
doc["header"] = header;
doc["mode"] = "push";
doc["target"] = "all";
if (payload && strlen(payload) > 0)
{
doc["payload"] = payload;
}
String s;
serializeJson(doc, s);
sio.emit("event", s.c_str());
}
void emitChat(const char *text)
{
StaticJsonDocument<128> doc;
doc["chat"] = text;
doc["mode"] = "push";
doc["target"] = "all";
String s;
serializeJson(doc, s);
sio.emit("chat", s.c_str());
}Your computer might have trouble recognizing your board. Double check the following:
-
Not all cables are created equal. Make sure the ESP32 board is connected to your computer through a data cable, as some cables are just charging cables and may not have their data nodes connected.
-
If you're on a Mac, you might need to install drivers:
-
CP210x: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers
-
If you are installing drivers, unplug your board before install. Make sure you Allow these drivers in MacOS. You might see an alert window pop-up. In other cases you will have to go to either Security & Privacy settings or Login Items & Extensions to press Allow from the drivers. Reboot.
-
-
If upload stalls, hold BOOT, tap EN (RST), release BOOT during connection.
# macOS Terminal
ls /dev/cu.*
# Linux Terminal
ls /dev/ttyUSB*
ls /dev/ttyACM*Typical ESP32 ports:
-
/dev/cu.SLAB_USBtoUART(CP210x) -
/dev/cu.wchusbserial*(CH34x) -
/dev/ttyUSB0or/dev/ttyACM0(Linux) -
No serial ports: Install USB driver, allow extension, reboot, replug board
-
Upload stalls: Hold BOOT, tap EN (RST), release BOOT during connection
-
Wrong port: Set
--upload-portor select correct port in IDE -
Hub not receiving: Check WiFi, IP, and port settings
-
TLS errors: use non-TLS
ws://or provide CA certs forwss://support -
Upload stalls: Hold BOOT, tap EN (RST), release BOOT during connection.
-
Hub not receiving: Check WiFi, IP, and port settings.
This ESP32 client supports both unencrypted (ws://) and encrypted (wss://, TLS) WebSocket connections to Collab-Hub servers.
- TLS/SSL (wss://) is fully supported using
WiFiClientSecure. By default, the config useswss://server.collab-hub.io(port 443,USE_TLS true). - For local or non-TLS servers, set
USE_TLS falseand use the appropriateHUB_HOSTandHUB_PORT(e.g.,ws://192.168.1.100:3000). - No CA certificate is required for the public server; the client uses
setInsecure()for convenience. For production security, you may add CA validation. - All connection settings are in
config.h:HUB_HOST(server address)HUB_PORT(443 for wss, 3000 for ws)USE_TLS(true for wss, false for ws) Default: The client connects towss://server.collab-hub.ioout of the box. If you encounter TLS handshake issues, ensure your ESP32 board has sufficient memory and is running the latest ESP32 Arduino core. For most users, secure connections should work reliably.
VS Code PlatformIO:
- Install "PlatformIO IDE" extension.
- Open
Collab-Hub-ESP32folder. - Select env
esp32devand use "Build" → "Upload" → "Monitor".
On serial monitor you should see:
- WiFi connected (IP address)
- Event messages sent when a button is pressed on the configured GPIO pin
- Client listens to
control,event, andchatframes. - Pressing a button connected to the configured GPIO pin (default: GPIO 0) sends an event
This project is organized for easy configuration and user extension:
CollabHubESP32/CollabHubESP32.ino: Core logic. Do not modify unless you are changing the package itself. Handles connection, routing, and setup.CollabHubESP32/config.h: Edit this file to set your WiFi, server, and room settings.CollabHubESP32/user_script.cpp&user_script.h: This is where you add your own logic.- Implement your custom event, control, and chat handlers here.
- Add your own methods, variables, and hardware logic in
user_script.cpp. - Use the provided hooks (e.g.,
onControlMessage,onEventPinPressed, etc.) to respond to messages and hardware events.
CollabHubESP32/SioClient.h/.cpp: Socket.IO protocol shim (do not edit unless you are modifying protocol internals).CollabHubESP32/WsClient.h/.cpp: Minimal WebSocket client (do not edit unless you are modifying protocol internals).platformio.ini: PlatformIO build configuration (if you choose to use Platform.io)
To customize behavior:
- Only edit
config.h(for network/server settings) anduser_script.cpp/user_script.h(for your application logic). - You do NOT need to change
CollabHubESP32.ino,SioClient, orWsClientfor normal use.
Notes:
- No extra Socket.IO libraries are required; the sketch includes a minimal WebSocket client (
WsClient) and a small Socket.IO framing shim (SioClient). - If your Mac shows only Bluetooth ports, install CP210x or CH34x driver, allow the system extension, reboot, and replug the board.
Tip: You can use the server.collab-hub.io web page to test your ESP32 client. This web interface lets you send and receive control, event, and chat messages in real time, making it easy to verify your device's connectivity and message handling.






