How to Publish Simulink output into Websocket

조회 수: 12 (최근 30일)
Amish Chavda
Amish Chavda 2023년 7월 20일
댓글: Amish Chavda 2023년 8월 31일
I want to publish the simulink output into Webscket in JSON format. How can i do this? The Rasberry Pi websocket block does not work
  댓글 수: 2
Prasanth Sunkara
Prasanth Sunkara 2023년 8월 30일
Hi Amish,
Could you please share more info on what you have tried with the Raspberry Pi block? Also, who did you conclude it did not work. Did you try any Simulink example related to web socket blocks?
Amish Chavda
Amish Chavda 2023년 8월 31일
Hi Prashant,
The Rasberry Pi block needs dedicated Rasberry Pi HW. I tried in Simulink with Matlab function block. But the JSOn encode and websocksts needs Matlab engine to run. .So it works only in Simulation. No C code possibility.
In the end i just switched to Python

댓글을 달려면 로그인하십시오.

답변 (1개)

Aman
Aman 2023년 8월 2일
Hi,
I understand that you have a Simulink model and want to publish its output to a WebSocket but are unable to do so, and the Raspberry Pi WebSocket block is also not able to do the task.
Without the actual error information, it is difficult to give an upfront explanation of why the Raspberry Pi WebSocket block failed to publish the signal.
Alternatively, you can create a function in MATLAB to publish the data received to a WebSocket; for this, you need to use the "webwrite" function that writes the content to the WebSocket. Once the function is developed, you can call it by using the "MATLAB Function" block in Simulink.
You can refer the below example code for reference.
function sendJSONDataToWebSocket(data)
% Convert data to JSON format
jsonData = jsonencode(data);
% Specify the WebSocket URL
webSocketURL = 'ws://your-websocket-url';
% Set the HTTP headers
headers = struct('Content-Type', 'application/json');
% Send the JSON data to the WebSocket server
webwrite(webSocketURL, jsonData, 'Headers', headers);
end
Please refer the following documentation to learn more about the “webwrite” method.
Also, to learn about “MATLAB Function” block of Simulink, please refer the following documentation.
I hope it helps!
  댓글 수: 1
Amish Chavda
Amish Chavda 2023년 8월 10일
Webrite doesnt support Ws.
Is theer any other method
The 'ws' protocol specified in URL, 'ws://localhost:16016', is not supported. Specify the URL with the protocol 'http://' or 'https://'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Run on Target Hardware에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by