@pammyleong
#define CHANNEL 0
#define CHANNELJPEG 1
// ----------------- global variables------------------
uint32_t img_addr = 0;
uint32_t img_len = 0;
bool initialMessagesSent = false;
unsigned long connectionTime = 0;
VideoSetting configWS(VIDEO_HD, CAM_FPS, VIDEO_JPEG, 1);
VideoSetting config(CHANNEL);
RTSP rtsp;
StreamIO videoStreamer(1, 1);
int status = WL_IDLE_STATUS;
IPAddress ip;
WiFiClient wifiClient;
char serverAddress = “192.168.10.13”;
int wsPort = 6060;
// char endpoint = “/”;
char endpoint = “/?mac=00:1A:2B:3C:4D:5F”;
WebSocketClient wsClient = WebSocketClient(wifiClient, serverAddress, wsPort);
unsigned long lastSend = 0;
String receivedData = “”;
bool messageComplete = false;
//------------function declarations---------------
void videoStreamTask(void *pvParameters);
void webSocketTask(void *pvParameters);
void sendVideoFrame(void);
void processReceivedMessage(String message);
void sendAcknowledgement(String command, String requestId);
void setup()
{
Serial.begin(115200);
while (status != WL_CONNECTED)
{
Serial.print("Connecting to: ");
Serial.println(SECRET_SSID);
status = WiFi.begin(SECRET_SSID, SECRET_PASS);
}
ip = WiFi.localIP();
Camera.configVideoChannel(CHANNEL, config);
Camera.configVideoChannel(CHANNELJPEG, configWS);
Camera.videoInit();
rtsp.configVideo(config);
rtsp.begin();
videoStreamer.registerInput(Camera.getStream(CHANNEL));
videoStreamer.registerOutput(rtsp);
if (videoStreamer.begin() != 0)
{
Serial.println("StreamIO link start failed");
}
Camera.channelBegin(CHANNELJPEG);
Camera.channelBegin(CHANNEL);
// --------------Threads for tasks here -----------------
xTaskCreate(webSocketTask, "webSocketTask", 4096, NULL, 1, NULL);
xTaskCreate(videoStreamTask, "VideoStreamTask", 4096, NULL, 1, NULL);
}
void loop()
{
// blank loop
}
// WebSocket task function
void webSocketTask(void *pvParameters)
{
(void)pvParameters;
wsClient.begin(endpoint);
for (;;)
{
if (!wsClient.connected())
{
wsClient.begin(endpoint);
Serial.println("attempting to connect to server");
vTaskDelay(pdMS_TO_TICKS(1000));
initialMessagesSent = false;
continue;
}
// Handle initial messages after connection
if (!initialMessagesSent)
{
// Send first message immediately after connection
String hello = "{\"message\":\"Hello from device\"}";
wsClient.beginMessage(TYPE_TEXT);
wsClient.print(hello);
wsClient.endMessage();
Serial.println("Sent: " + hello);
delay(2000);
String ready = "{\"status\":\"device ready\"}";
wsClient.beginMessage(TYPE_TEXT);
wsClient.print(ready);
wsClient.endMessage();
Serial.println("Sent: " + ready);
connectionTime = millis();
initialMessagesSent = true;
}
while (wsClient.connected())
{
char c = (char)wsClient.read();
if (c == -1)
break;
receivedData += c;
if (c == '}')
{
messageComplete = true;
}
if (messageComplete)
{
processReceivedMessage(receivedData);
receivedData = "";
messageComplete = false;
}
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
void videoStreamTask(void *pvParameters)
{
(void)pvParameters;
for (;;)
{
if (initialMessagesSent && wsClient.connected())
{
sendVideoFrame();
}
vTaskDelay(pdMS_TO_TICKS(66)); // ~15 fps
}
}
void processReceivedMessage(String message)
{
int jsonStart = message.indexOf(‘{’);
if (jsonStart != -1)
{
message = message.substring(jsonStart);
}
JsonDocument doc;
DeserializationError error = deserializeJson(doc, message);
if (error)
{
Serial.println("Failed to parse JSON");
Serial.println("Raw message: " + message);
return;
}
const char *command = doc["command"];
const char *requestId = doc["requestId"];
sendAcknowledgement(command, requestId);
}
void sendAcknowledgement(String command, String requestId)
{
if (strcmp(command.c_str(), "open") == 0 || strcmp(command.c_str(), "close") == 0)
{
String status = (command == "open") ? "opened" : "closed";
String response = "{\"requestId\":\"" + requestId + "\",\"status\":\"" + status + "\",\"success\":true}";
// Send acknowledgement
wsClient.beginMessage(TYPE_TEXT);
wsClient.print(response);
wsClient.endMessage();
Serial.print("Sent Acknowledgement: ");
Serial.println(response);
}
}
void sendVideoFrame()
{
Serial.println(“Inside video Frame”);
Camera.getImage(CHANNELJPEG, &img_addr, &img_len);
if (img_len > 0) {
Serial.println("Inside image Condition");
Serial.println(img_len);
wsClient.beginMessage(TYPE_BINARY);
wsClient.write((uint8_t *)img_addr, img_len);
wsClient.endMessage();
}
Serial.println("Image Sent");
}
----- I am trying to Image frames over websocket but i get that error after 14-15 frames are sent.-----