Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Web Sockets #30

Closed
samehhady opened this issue Dec 13, 2014 · 26 comments
Closed

Implementing Web Sockets #30

samehhady opened this issue Dec 13, 2014 · 26 comments

Comments

@samehhady
Copy link

Hi Zeroday,

You think its possible to implement web sockets on ESP?

Think of it as we will be able to control all ESPs using a cloud server & there will be 2 way communication between all ESPs and the server!

Also this can provide a cloud based flashing tool for ESP, we will be able to flash the ESPs from anywhere!

@nodemcu
Copy link
Collaborator

nodemcu commented Dec 19, 2014

Is websocket a application layer above tcp socket? like http?

@samehhady
Copy link
Author

Yes it is a protocol providing full-duplex communications channels over a single TCP connection.

@samehhady
Copy link
Author

It let you connect directly and have 2 way communication with server and ESP (Client)

@nodemcu
Copy link
Collaborator

nodemcu commented Dec 19, 2014

I google it and seems that this websocket work with html5, isn't it too "heavy" for mcu?
maybe mqtt or coap?
Is it easy to implement in pure Lua?

@samehhady
Copy link
Author

I've found several libraries online like
https://github.com/openresty/lua-resty-websocket
https://github.com/lipp/lua-websockets

They are in pure lua

@samehhady
Copy link
Author

its implementation should be fairly simple, you connect to tcp server and send special get request with special headers and wait for the response, you then compare the response id with the one you already generated, once confirmed you then have a successful handshake.

Then you have to add a special frame and encode the message with a special way for the socket server to accept the message and response back.

I've done it all using node Js by just using tcp connection so it should be fairly easy to implement on Lua

@jgmbrand
Copy link

Try to connect to esp8266 AP from other device ,but the configured password is not recognised.
Why?
How to configure an open AP?
Tia hans

@ghost
Copy link

ghost commented Dec 26, 2014

on my esp it works normaly. Open AP can be configured with nil pwd:
wifi.ap.config({ssid="test"});

@jgmbrand
Copy link

Neironx,
Neironx,
Works great now.
Probably just same weird copy/paste behaviour.
Regards
hans

@redlava
Copy link

redlava commented Dec 29, 2014

@samehhady have you been able to implement a WebSockets client on the ESP? You mentioned that you've developed something using node.js with a tcp connection, was this using the tcp capabilities of this firmware? If so, can you provide some example of how this can be achieved?

I'd be keen to see a Lua implementation included in nodemcu as I believe it is a powerful and light-weight means of managing communications between nodes.

@dvv
Copy link
Contributor

dvv commented Dec 29, 2014

My early websocket experience
https://github.com/dvv/luvit-websocket
On Dec 29, 2014 3:16 AM, "redlava" [email protected] wrote:

@samehhady https://github.com/samehhady have you been able to implement
a WebSockets client on the ESP? You mentioned that you've developed
something using node.js with a tcp connection, was this using the tcp
capabilities of this firmware? If so, can you provide some example of how
this can be achieved?

I'd be keen to see a Lua implementation included in nodemcu as I believe
it is a powerful and light-weight means of managing communications between
nodes.

Reply to this email directly or view it on GitHub
#30 (comment)
.

@samehhady
Copy link
Author

@redlava I have did some studies on how to implement it and still waiting for my ESPs to try it out.
For the TCP yes it should be fairly simple to talk to the ESPs using a TCP connection, the ESP as a client and nodeJS as a server. Soon I will share some codes after testing everything on real as most of my testing now are just basic.

@AutomationD
Copy link

I used rabbitmq with web-stomp and mqtt to connect nodemcu.
I would rather have mqtt working more stable on the ESP8266.

@dvv
Copy link
Contributor

dvv commented Mar 22, 2015

There's also a good and lightweight https://developer.mozilla.org/en-US/docs/Web/API/EventSource

@nodemcu
Copy link
Collaborator

nodemcu commented Apr 5, 2015

mqtt and coap available.
no plan for websocket.
let the cloud server bridge it.

@nodemcu nodemcu closed this as completed Apr 5, 2015
@AutomationD
Copy link

Thanks. mosquitto / rabbitmq has websocket support. I use rabbitmq on a raspberry pi as a central hub, works great.

@urmilparikh
Copy link

@samehhady, it will be nice if you can share your lua or js code to implement Websocket over TCP connection.

I'm planning to do so, can leverage on your work.

Thanks!

@creationix
Copy link
Contributor

Websocket is a little heavy, but I think it can be done. I tried porting my luvit websocket library but it used too much ram. The protocol itself is pretty simple. When I get a chance I'll write another pure lua websocket server that uses less ram so that an html5 based app can talk directly to the nodemcu.

@pastukhov
Copy link
Contributor

The main problem is md5 and sha1 algorithms, as i see.

@creationix
Copy link
Contributor

Those can also be implemented in lua, I've done it a few times before in JS. Though I do wonder how much memory that would use.

@creationix
Copy link
Contributor

@pastukhov There is no MD5 in websocket, but you do need SHA1 and Base64 encoding. Here is a sha1 function I just wrote using the bit.* library that should work.
https://gist.github.com/creationix/7ce3796e65b66549c4b0

@pastukhov
Copy link
Contributor

I'm getting

> dofile('testsha1.lua')
string length overflow

@creationix
Copy link
Contributor

@pastukhov You probably don't want to run all the unit tests on the nodemcu. One is calculating the sha1 of a string containing 1,000,000 "a"s long. The values that will be used in websocket handshake are under 100 characters long. I ran that one on a raspberry pi using luajit (it's bit library is compatible)

@ikaras
Copy link

ikaras commented Feb 17, 2016

Hi, all!
I tried to resolve issue with websocket client for nodemcu: https://github.com/ikaras/nodemcu-websocket-client
Take a look and be free to use it.

@creationix
Copy link
Contributor

Now that we have sha1 and friends in the core and a lot more free ram than when I initially started this, making a websocket server and/or client is quite feasible.

See also my past experiments in this area:

I'm especially excited about websocket server since it means that browsers can directly talk to the mcu without any proxy in between. You can still host the bulk of the web ide (js/css/html) in the cloud and use cache-manifest or service workers to make it work when the laptop is offline.

I'm currently working on a new VM for esp chips that should use less ram than lua. It will have websocket built-in. The basic workflow will be browser based so that any device on the same wifi network as the mcu can program it. There will be a repl in the browser as well as a code editor. My compiler from the high-level language down to the bytecode running in the vm will be implemented in JS so the mcu doesn't have the overhead of a compiler.

@ikaras
Copy link

ikaras commented Feb 17, 2016

Hi @creationix.
That's great.
I saw your websocket server - it's great! and as I'm noob in Lua and in nodemcu/esp8266 I used your code to encode/decode messages and it works perfect. A little bit later I'll add notes about it to readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants