Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 9 months ago.
Connecting mbed to tornado websocket?
I am trying to connect a k64f mbed board to a websocket so basic commands can be sent from and to the k64f from the websocket, just single characters that will be dealt with in the mbed software.
I currently have a very basic chat websocket using tornado python, now when i run the server I can view it on any PC connected to my LAN and send messages to the server. I am struggling however to connect my Mbed k64f.
I am using the websocketclient library alongside the ethernetinterface library, and have it running with the sockets.mbed.org websocket. when I change the ws definiion line to Websocket ws("ws:192.168.1.247:8888");, the while(!ws.connect()); function always returns -1 so never exits the while loop. My server reports that the mbed has been connected, and every time my mbed software reattempts ws.connect() the python server side index handler def get(self): code runs.
Can anybody help get the k64f connected properly?
server side:
/media/uploads/mcollinge/websocket2.py
Client Side:
1 Answer
9 years, 9 months ago.
Hi. The websocket libraries I have used on MBED for the K64F require an ending slash. So, be careful to create your websocket like so:
Websocket ws("ws://192.168.1.247:8888/"); // note that the ending slash is required
Thanks for the help!, the connect function was still returning false, and using the serial terminal to debug hI found that this statement was returning false:
if ( strstr(cmd, "DdLWT/1JcX+nQFHebYP+rqEx5xI=") == NULL ) { dc.printf("null\r\n"); ERR("Wrong answer from server, got \"%s\" instead\r\n", cmd); do { ret = read(cmd, 200, 100); dc.printf("ret = %s\r\n", ret); if (ret < 0) { ERR("Could not receive answer\r\n"); return false; } cmd[ret] = '\0'; printf("%s",cmd); } while (ret > 0); close(); return false;
I replaced DdLWT/1JcX+nQFHebYP+rqEx5xI= from the statement, with the etag shown for my own server that was printed by my mbed board while trying to connect
recv: HTTP/1.1 200 OK Date: Thu, 29 Jan 2015 11:29:12 GMT Content-Length: 4 Etag: "4170ac2a2782a1516fe9e13d7322ae482c1bd594" Content-Type: text/html; charset=UTF-8 Server: TornadoServer/4.0.2
This then fixed the problem and (!ws.connect()); now returns true and runs my main code, although I'm not sure this was correct as I can't seem to send or receive anything between my server and k64f board using ws.send() and ws.read() :(
Thanks for any help! :)
posted by 29 Jan 2015Hi. For the K64F, I am using the websocket libraries from the K64F code share here: http://developer.mbed.org/teams/FRDM-K64F-Code-Share/code/Websocket_Test/
I had to get the absolute latest WebSocket and EthernetInterface classes after importing them before my code was functional. The latest codebase also compiles with a few warnings, but is functional nonetheless on my K64F.
One more thing. I have been working with Node.js (instead of Tornado) simply because Node.js installs and works on pretty much everything. I have a rather generic Node.js server script that instantiates a web server and a websocket server and works pretty reliably (has been through a few demonstrations).
I have not played with Tornado, but that seems to be what MBED's websocket server stuff is built on. Can you post to the MBED server?
posted by 29 Jan 2015Success!!, i switched over to using node instead of tornado, and used this simple echo server taken from https://www.npmjs.com/package/nodejs-websocket which worked first time!
var ws = require("nodejs-websocket") // Scream server example: "hi" -> "HI!!!" var server = ws.createServer(function (conn) { console.log("New connection") conn.on("text", function (str) { console.log("Received message"+str) conn.sendText(str.toUpperCase()+"!!!") }) conn.on("close", function (code, reason) { console.log("Connection closed") }) }).listen(8888)
The k64f connected without having to make any changes to websocket.h and is sending and receiving data now! still not sure what caused the problem with the tornado server but i'm just going to avoid it and stick to node.js
Cheers for all the help! MC
posted by 29 Jan 2015
I know you've already accepted an answer, but I thought I'd mention something I noticed. From the code you posted in websocket2.py, you didn't install WebSocketHandler() in your tornado.web.Application() constructor. Without that, you won't see any response from your client's messages. You should take a look at the mbed WebSocket Server Tutorial. It shows how that needs to be done. One thing I had to do was to override the check_origin() method to return True. See the Tornado docs for details.
I've got my LPC1768 WebSocket client communicating with my Tornado WebSocket Server. The only issue I've had is with the WebSocket::is_connected() method in the mbed WebSocket Client. Once connected, it always returns true, even after closing the connection. This would seem to be a bug to me, but I may not be understanding something about its behavior.
posted by David G 12 Feb 2015Hi David I have also noticed that the WS::is_connected() returns true even though I close the connection or even if the internet drops out/reconnects. Have you got a solution for this?
posted by Marcus Parker 18 Mar 2015