-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
ESP32: Websocket client ESP32 library in pure Lua #2694
Changes from 1 commit
b112528
06d0c90
531bbba
772dd57
c631743
52d0a20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
-- ChiliPeppr Websocket client library for Lua ESP32 | ||
-- This library lets you talk over a websocket using pure Lua | ||
-- | ||
-- Visit http://chilipeppr.com/esp32 | ||
-- By John Lauer | ||
-- | ||
-- There is no license needed to use/modify this software. It is given freely | ||
-- to the open source community. Modify at will. | ||
-- | ||
-- Working example of using this library: | ||
-- https://www.youtube.com/watch?v=ITgh5epyPRk&t=119s | ||
|
||
-- The websocket Lua library is in the lua_modules directory of the nodemcu_firmware | ||
-- /nodemcu-firmware/lua_modules/websocket/websocket.lua | ||
-- Upload it to your ESP32 and node.compile() it to get websocket.lc | ||
-- Then you can require() it below | ||
ws = require('websocket') | ||
ws.on("receive", function(data, fin) | ||
-- fin of 1 or nil means you got all data | ||
-- fin of 0 means extended data will come in | ||
print("Got data:" .. data .. ", fin:", fin) | ||
end) | ||
ws.on("connection", function(host, port, path) | ||
print("Websocket connected to host:", host, "port:", port, "path:", path) | ||
ws.send("list") | ||
end) | ||
ws.on("disconnection", function() | ||
print("Websocket got disconnect from:", ws.wsUrl) | ||
end) | ||
ws.on("pingsend", function() | ||
print("Ping") | ||
end) | ||
ws.on("pongrecv", function() | ||
print("Got pong. We're alive.") | ||
end) | ||
|
||
-- Use ChiliPeppr wifi library to auto-connect to wifi | ||
wf = require("esp32_wifi") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the example maybe should not require external modules. Does it also work with the standard wifi module? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it just needs normal connectivity. I should remove that. |
||
wf.on("connection", function(info) | ||
print("Got wifi. IP:", info.ip, "Netmask:", info.netmask, "GW:", info.gw) | ||
ws.init(info.ip) | ||
-- This sample websocket is Serial Port JSON server | ||
-- Set this to your own 2nd SPJS, not the local one, or you'll get loopbacks | ||
-- ws.connect("ws://10.0.0.201:8989/ws") | ||
-- Example public websocket server | ||
ws.connect("ws://demos.kaazing.com/echo") | ||
end) | ||
wf.init() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually lua_module examples are right besides the module in the same folder.
See http module for example,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I did it like the email module which had a folder in examples and a folder in lua_modules. I can do it like http though if that's the more preferred format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok checked some others and both ways seem to be common.
So a task to decide for Collaborator (or not to decide)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved them to the same directory. I think it's fine that way.