Connect to Offbeat IoT over WebSocket
1. Add the required libraries and credentials
import these
//Check out the documentation section on how to manage credentials like this
#include "wifiCredentials.h"
#include "offbeatIotCredentials.h"
#include <ESP8266WiFi.h>
#include <WiFiClientSecureBearSSL.h>
//connect to Offbeat-IoT using websockets
#include <WebSocketsClient.h>
//using a timer to synchronize with spotify
#include <SimpleTimer.h>
Credentials are stored in separate header files:
-
wifiCredentials.h— your WiFi SSID and password -
offbeatIotCredentials.h— your Offbeat IoT user and password (created in the UI)
See Credentials Handling for details.
2. Define the WebSocket URL
The WebSocket endpoint runs at wss://www.offbeat-iot.com/ws. Pass your device id as a query parameter:
// The Offbeat IoT server host
char host[] = "www.offbeat-iot.com";
// WebSocket path with your device id (get this from the UI)
char path[] = "/ws?device=YOUR_DEVICE_ID";
You can also use the /name path variant:
wss://www.offbeat-iot.com/name?device=YOUR_DEVICE_ID
3. Initialize the WebSocket connection
after configuring your device, add the information
webSocketClient.begin(host, 443, path);
//Tell Offbeat-IoT that you'd like to receive data in json format
webSocketClient.setExtraHeaders("Accept=application/json");
webSocketClient.setAuthorization(offbeatIotUser, offbeatIotPassword);
The connection uses:
* Port 443 (standard WSS) for production
* Basic Auth with your Offbeat IoT credentials
* JSON format via the Accept=application/json header
4. Handle incoming messages
case WStype_TEXT:
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
break;