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

ESP32: Websocket client ESP32 library in pure Lua #2694

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions lua_examples/websocket.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-- ChiliPeppr Websocket client library for Lua ESP32
Copy link
Member

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,

Copy link
Author

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.

Copy link
Member

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)

Copy link
Author

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.

-- 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")
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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()

Loading