Page 1 of 1

Send data via websocket from ESP32 to server?

Posted: Wed Dec 19, 2018 6:25 pm
by sepSig
Hi all,

I'm trying to send sensor measurement data from an ESP32 to a raspberry based server.

A test setup running on another computer with the complete python3 environment works like a charm. The minimal working example under python3 looks like this:

import websocket
ws = websocket.WebSocket()
ws.connect("ws://192.168.0.5:3000")
ws.send( data ... )

I like to achieve the same on the ESP32.

As part of the MicroPython installation on the ESP32 I found the "websocket" module. When I import the websocket module and try to run my example code it tells me that the module websocket has no attribute "WebSocket". So to me it seems that the websocket module in MicroPython is fairly limited.

Does someone have an idea what is the best way to feed data via websockets from the ESP32 to another server and might give me a hint?

Re: Send data via websocket from ESP32 to server?

Posted: Wed Dec 19, 2018 10:26 pm
by HermannSW
The builtin websocket module is completely undocumented and therefore useless.

I used Danni's MicroPython uwebsocket module without problems, see thread Minimal MicroPython uwebsockets.client that works on esp8266.

Re: Send data via websocket from ESP32 to server?

Posted: Fri Dec 21, 2018 11:47 am
by sepSig
Thank you!

I had to remove the "import logging" and all corresponding call in protocol.py and client.py since the module is not available. But then it works just fine!

Re: Send data via websocket from ESP32 to server?

Posted: Fri Dec 21, 2018 1:27 pm
by sepSig
Well, not quite ;)

After a while my script crashes:

Traceback (most recent call last):
File "main.py", line 167, in <module>
File "uwebsockets/protocol.py", line 212, in send
File "uwebsockets/protocol.py", line 137, in write_frame
OSError: [Errno 104] ECONNRESET

After googling for a while it seems that I should regularly disconnect and reconnect to the server to avoid being kicked by the server. Is this the right approach? How would I explicitly disconnect from the server?

Re: Send data via websocket from ESP32 to server?

Posted: Fri Dec 21, 2018 4:31 pm
by sepSig
Ok, encapsulating the send statement with a try/except environment combined with a reconnect on connection loss works.

Re: Send data via websocket from ESP32 to server?

Posted: Sat Dec 22, 2018 4:50 pm
by HermannSW
sepSig wrote:
Fri Dec 21, 2018 11:47 am
I had to remove the "import logging" and all corresponding call in protocol.py and client.py since the module is not available. But then it works just fine!
You don't have to do that, I took logging.py from micropython-lib github repo and all was fine:
https://github.com/micropython/micropyt ... logging.py