Send data via websocket from ESP32 to server?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
sepSig
Posts: 4
Joined: Wed Dec 19, 2018 6:10 pm

Send data via websocket from ESP32 to server?

Post by sepSig » Wed Dec 19, 2018 6:25 pm

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?

HermannSW
Posts: 144
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Send data via websocket from ESP32 to server?

Post by HermannSW » Wed Dec 19, 2018 10:26 pm

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.

sepSig
Posts: 4
Joined: Wed Dec 19, 2018 6:10 pm

Re: Send data via websocket from ESP32 to server?

Post by sepSig » Fri Dec 21, 2018 11:47 am

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!

sepSig
Posts: 4
Joined: Wed Dec 19, 2018 6:10 pm

Re: Send data via websocket from ESP32 to server?

Post by sepSig » Fri Dec 21, 2018 1:27 pm

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?

sepSig
Posts: 4
Joined: Wed Dec 19, 2018 6:10 pm

Re: Send data via websocket from ESP32 to server?

Post by sepSig » Fri Dec 21, 2018 4:31 pm

Ok, encapsulating the send statement with a try/except environment combined with a reconnect on connection loss works.

HermannSW
Posts: 144
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Send data via websocket from ESP32 to server?

Post by HermannSW » Sat Dec 22, 2018 4:50 pm

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

Post Reply