Skip to content

Commit a43f801

Browse files
author
Jimmy Bradshaw
committed
initial commit
0 parents  commit a43f801

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
NAME := lua-interpreter
2+
PORT := /dev/cu.SLAB_USBtoUART
3+
NODEMCU_BIN := nodemcu-master-11-modules-2020-04-22-14-23-50-integer.bin
4+
5+
.PHONY: help
6+
help:
7+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
8+
9+
.PHONY: flash
10+
flash: ## flash nodemcu
11+
esptool.py --port $(PORT) erase_flash
12+
esptool.py --port $(PORT) write_flash --flash_mode dio 0x00000 $(NODEMCU_BIN)
13+
sleep 5
14+
nodemcu-tool -p $(PORT) mkfs --noninteractive
15+
16+
.PHONY: init
17+
init: ## upload init file
18+
nodemcu-tool -p $(PORT) upload init.lua
19+
20+
.PHONY: term
21+
term: ## start terminal
22+
nodemcu-tool -p $(PORT) terminal

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# nodemcu-mqtt-lua-interpreter
2+
3+
Use mqtt to pass chunks to be interpreted, allows for responses on another topic.
4+
5+
## Getting Started
6+
7+
### Setup
8+
9+
Requires [esptool](https://github.com/espressif/esptool) and [nodemcu-tool](https://github.com/andidittrich/NodeMCU-Tool).
10+
11+
Modify `Makefile` with your serial port
12+
13+
Modify `init.lua` with your MQTT host.
14+
15+
`make flash` to flash nodemcu
16+
17+
`make init` to upload the lua init script
18+
19+
`make term` to start a lua terminal
20+
21+
### Usage
22+
23+
Send a mqtt message to be interpreted, e.g.
24+
25+
`mosquitto_pub -h <host> -t /lua-interpreter -m '{"chunk": "gpio.mode(1, gpio.OUTPUT) gpio.write(1, gpio.HIGH)}'`
26+
27+
which will configure pin 1 in output mode and set it high.
28+
29+
If a reply is desired, you can pass a "reply-to" key in the JSON payload, e.g.
30+
31+
mosquitto_sub -h <host> -t /lua-interpreter
32+
33+
and then in another shell
34+
35+
mosquitto_pub -h <host> -t /lua-interpreter -m '{"chunk": "return gpio.read(1)", "reply-to": "/reply"}'
36+
37+
which will read pin 1 (should be 1 now) and we receive that response on the /reply topic.

init.lua

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
mqtthost = "192.168.0.108"
2+
interpretername = "lua-interpreter"
3+
interpretertopic = "/" .. interpretername
4+
5+
function interpret(client, topic, data)
6+
print("message on " .. topic .. " with body " .. data)
7+
if topic == interpretertopic and data ~= nil then
8+
print("interpreting message")
9+
local req_t = sjson.decode(data)
10+
local status, val = pcall(loadstring(req_t["chunk"]))
11+
if status then
12+
print("interpreting successful")
13+
end
14+
local replyto = req_t["reply-to"]
15+
if replyto ~= nil then
16+
resp_t = {success=status, message=val}
17+
resp_json = sjson.encode(resp_t)
18+
print("publishing to " .. replyto .. " with " .. resp_json)
19+
client:publish(replyto, resp_json, 0, 0, function () print("publish success") end)
20+
end
21+
end
22+
end
23+
24+
function onconnect(client)
25+
client:subscribe(interpretertopic, 0, function () print("subscribe success") end)
26+
end
27+
28+
function mqttsetup()
29+
local client = mqtt.Client(interpretername, 120)
30+
client:on("message", interpret)
31+
client:connect(mqtthost, 1883, false, function ()
32+
print("connect success")
33+
onconnect(client)
34+
end,
35+
function (_, reason)
36+
print('connect failed reason: ' .. reason)
37+
end
38+
)
39+
end
40+
41+
function wificheck(timer)
42+
if wifi.sta.status() == wifi.STA_GOTIP then
43+
print('wifi connected')
44+
timer:unregister()
45+
mqttsetup()
46+
else
47+
print('wifi not connected...')
48+
timer:start()
49+
end
50+
end
51+
52+
function init()
53+
inittimer = tmr.create()
54+
inittimer:alarm(1000, tmr.ALARM_SEMI, wificheck)
55+
end
56+
57+
init()
Binary file not shown.

0 commit comments

Comments
 (0)