websocket on raspberry pi examples not working -- SOLVED
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
In case anyone else has gotten stuck ... hope this helps you!
I had been unable to get websockets working on Raspberry Pi trying several Mathwork examples including https://www.mathworks.com/help/supportpkg/raspberrypi/ref/websocket-publish-and-dashboard.html
The chrome browser reported a connection failure. I could not connect with a python websocket client either. (I could however successfully connect/send/receive with udp and tcp to other simulink models using python servers or clients as appropriate.)
I was using a sdcard with mathworks_raspbian_R22.1.0.img. My default python is python3. I have Simulink coder and clicked "Build, Deploy & Start" to create /home/pi/MATLAB_ws/R2022b/raspberrypi_sensehat_dashboard.elf
Running the executable creates a log which indicates that a Mathworks internal tool MW_pyserver.py is failing. I noted that a) syntax errors for "()" indicating it was written for python2 not python3, b) it imports tornado web server but tornado is not installed c) it imports nnpy but nnpy is not installed. I tried installing these but it then complains no current event loop in Thread-1... (Log is attached.)
I guessed that websockets were not working because of further python2 vs python3 issues so I started from scratch by creating a new sdcard using mathworks_raspbian_R22.2.0.img. I did not use "sudo apt update" or "sudo apt upgrade". The default python is now python v2.7.16. I ran "pip install websocket-client" to allow testing with a python websocket client. I ran both the simulink executable .elf and the python websocket client on the Rpi. The websocket block and client both use 127.0.0.1 as the IP. And now the ws client works fine :)
< It would be nice if the Mathwork's code asserts for python v2, or better yet supports both v2 and v3 - hint hint >
pi@rpi3B:~/Documents $ python wsclient.py
client connected
{"BlockLabel":"webPub 1","Signal":[{"DataType":"double","Value":[-0.059814,-0.097198,-0.097198,-0.067291,-0.044861,-0.014954,-0.127106,-0.089722,-0.059814,-0.059814]},{"DataType":"double","Value":[6.736603,6.833801,6.833801,6.818848,6.841278,6.818848,6.848755,6.811371,6.736603,6.773987]},{"DataType":"double","Value":[0.201874,0.254211,0.291595,0.261688,0.254211,0.246735,0.201874,0.299072,0.284119,0.239258]},{"DataType":"double","Value":[-0.006714,-0.007324,-0.006897,-0.006470,-0.007629,-0.006897,-0.006958,-0.006897,-0.007629,-0.006104]},{"DataType":"double","Value":[-0.108643,-0.108643,-0.108765,-0.108521,-0.108704,-0.108887,-0.108704,-0.108887,-0.108704,-0.108704]},{"DataType":"double","Value":[0.953003,0.955261,0.954773,0.954956,0.954346,0.954224,0.954529,0.955383,0.955444,0.955627]},{"DataType":"double","Value":[0.072144,0.071167,0.071167,0.071167,0.078735,0.078735,0.075317,0.075317,0.075317,0.075806]},{"DataType":"double","Value":[0.131958,0.136353,0.136353,0.136353,0.130249,0.130249,0.131714,0.131714,0.131714,0.134155]},{"DataType":"double","Value":[-0.225464,-0.232544,-0.232544,-0.232544,-0.240234,-0.240234,-0.220581,-0.220581,-0.220581,-0.232178]},{"DataType":"double","Value":[28.237535,1010.543457,42.073814,0.000000]}]}
pi@rpi3B:~ $ python --version
Python 2.7.16
pi@rpi3B:~ $ python3 --version
Python 3.7.3
pi@rpi3B:~/Documents $ cat wsclient.py
from websocket import create_connection, WebSocketConnectionClosedException
import json
# install with "pip install websocket-client"
ws = create_connection("ws://127.0.0.1:9000") # ok
print("client connected")
try:
#data = json.loads(ws.recv())
data = ws.recv()
print(data)
except Exception as inst:
print(inst)
0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Python Client Programming dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!