nodemcu / nodemcu-firmware

Lua based interactive firmware for ESP8266, ESP8285 and ESP32
https://nodemcu.readthedocs.io
MIT License
7.66k stars 3.12k forks source link

Implementing Web Sockets #30

Closed samehhady closed 9 years ago

samehhady commented 9 years ago

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 commented 9 years ago

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

samehhady commented 9 years ago

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

samehhady commented 9 years ago

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

nodemcu commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

jgmbrand commented 9 years ago

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

redlava commented 9 years ago

@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 commented 9 years ago

My early websocket experience https://github.com/dvv/luvit-websocket On Dec 29, 2014 3:16 AM, "redlava" notifications@github.com 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 https://github.com/nodemcu/nodemcu-firmware/issues/30#issuecomment-68224549 .

samehhady commented 9 years ago

@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 commented 9 years ago

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

dvv commented 9 years ago

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

nodemcu commented 9 years ago

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

AutomationD commented 9 years ago

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

urmilparikh commented 9 years ago

@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 commented 9 years ago

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 commented 9 years ago

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

creationix commented 9 years ago

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 commented 9 years ago

@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 commented 9 years ago

I'm getting

> dofile('testsha1.lua')
string length overflow
creationix commented 9 years ago

@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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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.