basic nodemcu stuff in lua/
This commit is contained in:
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#*#
|
||||||
|
.#*
|
||||||
|
*~
|
||||||
|
|
||||||
|
nodemcu-firmware-esp32/
|
||||||
|
nodemcu-firmware/
|
||||||
|
ref/
|
||||||
29
_.org
Normal file
29
_.org
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* old stuff??!
|
||||||
|
|
||||||
|
-- sdcard
|
||||||
|
tf_cs=4 -- CS=GPIO4
|
||||||
|
tf_mosi=23 -- DI=GPIO23
|
||||||
|
tf_sclk=18 -- SCLK=GPIO18
|
||||||
|
tf_miso=19 -- DO=GPIO19
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- buttons (active low)
|
||||||
|
pin_btn_a=39
|
||||||
|
pin_btn_b=38
|
||||||
|
pin_btn_c=37
|
||||||
|
|
||||||
|
-- usb console
|
||||||
|
pin_console_rx=3
|
||||||
|
pin_console_tx=1
|
||||||
|
|
||||||
|
-- Speaker output
|
||||||
|
pin_spkr=25
|
||||||
|
|
||||||
2
lua/Makefile
Normal file
2
lua/Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
upload: init.lua
|
||||||
|
luatool.py --ip 192.168.1.174:2323 -vd -f init.lua
|
||||||
25
lua/adc.lua
Normal file
25
lua/adc.lua
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
function init()
|
||||||
|
adc.setup(adc.ADC1, 0, adc.ATTEN_11db)
|
||||||
|
adc.setwidth(adc.ADC1, 12)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function read(ch)
|
||||||
|
adc.setwidth(adc.ADC1, 12)
|
||||||
|
read = adc.read(adc.ADC1, ch)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function readall()
|
||||||
|
for i=0, 8 do
|
||||||
|
print("ADC1 read ch", i, "returned:", read(i));
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
1
lua/display.lua
Normal file
1
lua/display.lua
Normal file
@ -0,0 +1 @@
|
|||||||
|
dofile('lcd.lua')
|
||||||
12
lua/init.lua
Normal file
12
lua/init.lua
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
spimaster = spi.master(spi.HSPI, {sclk = 18, mosi = 23, miso = 19}) -- HSPI (LCD/TF Card)
|
||||||
|
|
||||||
|
function tryfile(f)
|
||||||
|
if file.exists(f) then s,err=pcall(function() dofile(f) end) if not s then print(err) end
|
||||||
|
else print('ERR: "'..f..'" does not exist...') end
|
||||||
|
end
|
||||||
|
|
||||||
|
tryfile('display.lua')
|
||||||
|
tryfile('wifi.lua')
|
||||||
|
|
||||||
|
function ls() print(sjson.encode(file.list())) end
|
||||||
|
|
||||||
21
lua/lcd.lua
Normal file
21
lua/lcd.lua
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
-- lcd
|
||||||
|
-- MOSI=GPIO23
|
||||||
|
-- SCK=GPIO18
|
||||||
|
-- CS=GPIO14
|
||||||
|
-- #RST=GPIO33
|
||||||
|
-- R/S=GPIO27
|
||||||
|
-- BL=GPIO32
|
||||||
|
|
||||||
|
-- enable backlight
|
||||||
|
gpio.config({gpio=32, dir=gpio.OUT, opendrain=0, pull=gpio.FLOATING})
|
||||||
|
gpio.write(32, 1)
|
||||||
|
|
||||||
|
disp=ucg.ili9341_18x240x320_hw_spi(spimaster, 14, 27, 33)
|
||||||
|
disp:begin(ucg.FONT_MODE_TRANSPARENT)
|
||||||
|
disp:clearScreen()
|
||||||
|
disp:setFont(ucg.font_ncenR12_tr);
|
||||||
|
disp:setColor(255, 255, 255);
|
||||||
|
disp:setColor(1, 255, 0,0);
|
||||||
|
disp:setPrintPos(0, 25)
|
||||||
|
disp:print("Hello World!")
|
||||||
|
|
||||||
4
lua/print.lua
Normal file
4
lua/print.lua
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
function table_print(t)
|
||||||
|
return sjson.encode(t)
|
||||||
|
end
|
||||||
50
lua/telnet.lua
Normal file
50
lua/telnet.lua
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
-- implement:
|
||||||
|
-- quit/ctrl-d disconnect
|
||||||
|
-- security?????
|
||||||
|
-- multiclient?
|
||||||
|
-- ssh????
|
||||||
|
-- environment??????????
|
||||||
|
|
||||||
|
|
||||||
|
-- a simple telnet server
|
||||||
|
|
||||||
|
-- restart server if needed
|
||||||
|
if telnet_srv ~= nil then
|
||||||
|
telnet_srv:close()
|
||||||
|
end
|
||||||
|
telnet_srv = net.createServer(net.TCP, 180)
|
||||||
|
|
||||||
|
telnet_srv:listen(23, function(socket)
|
||||||
|
local fifo = {}
|
||||||
|
local fifo_drained = true
|
||||||
|
|
||||||
|
local function sender(c)
|
||||||
|
if #fifo > 0 then
|
||||||
|
c:send(table.remove(fifo, 1))
|
||||||
|
else
|
||||||
|
fifo_drained = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function s_output(str)
|
||||||
|
table.insert(fifo, str)
|
||||||
|
if socket ~= nil and fifo_drained then
|
||||||
|
fifo_drained = false
|
||||||
|
sender(socket)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
node.output(s_output, 1) -- re-direct output to function s_ouput.
|
||||||
|
|
||||||
|
socket:on("receive", function(c, l)
|
||||||
|
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
|
||||||
|
end)
|
||||||
|
socket:on("disconnection", function(c)
|
||||||
|
node.output(nil) -- un-regist the redirect output function, output goes to serial
|
||||||
|
end)
|
||||||
|
socket:on("sent", sender)
|
||||||
|
|
||||||
|
print("Welcome to NodeMCU world.")
|
||||||
|
end)
|
||||||
|
|
||||||
|
print("Telnet server running...")
|
||||||
48
lua/wifi.lua
Normal file
48
lua/wifi.lua
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
--local M, module = {}, ...
|
||||||
|
--_G[module] = M
|
||||||
|
|
||||||
|
|
||||||
|
hostname="doi-kontrol-m5core1"
|
||||||
|
|
||||||
|
--function M.init()
|
||||||
|
function sta_init()
|
||||||
|
--wifi.mode(wifi.STATION, true)
|
||||||
|
wifi.sta.on("start", function(ev, a) print("NodeMCU WiFi Start") end)
|
||||||
|
wifi.sta.on("start", function(ev, a) print("NodeMCU WiFi Stop") end)
|
||||||
|
wifi.sta.on("connected", function(ev, a) print("NodeMCU wifi connected! ssid:", a.ssid, "bssid:", a.bssid,
|
||||||
|
"channel:", a.channel, "auth:", a.auth) end)
|
||||||
|
wifi.sta.on("disconnected", function(ev, a) print("NodeMCU wifi disconnected! ssid:", a.ssid, "bssid:", a.bssid,
|
||||||
|
"reason:", a.reason) end)
|
||||||
|
wifi.sta.on("authmode_changed", function(ev, a) print("NodeMCU authmode_changed! old_mode:", a.old_mode,
|
||||||
|
"new_mode:", new_mode) end)
|
||||||
|
wifi.sta.on("got_ip", function(ev, info)
|
||||||
|
dofile('telnet.lua') -- TKTK TODO XXX security risk lol
|
||||||
|
print("NodeMCU got_ip! ip:", info.ip, "netmask", info.netmask,
|
||||||
|
"gw", info.gw) end)
|
||||||
|
wifi.start()
|
||||||
|
wifi.sta.sethostname("doi-kontrol-m5core1")
|
||||||
|
wifi.sta.config({ssid="departmentofinter.net", pwd="baguette2175bagel", auto=true}, true)
|
||||||
|
print("wifi_init done")
|
||||||
|
end
|
||||||
|
|
||||||
|
sta_list = [
|
||||||
|
{ssid="departmentofinter.net", pwd="baguette2175bagel", auto=true}
|
||||||
|
]
|
||||||
|
|
||||||
|
function staionap_init()
|
||||||
|
wifi.mode(wifi.STATIONAP, true)
|
||||||
|
wifi.sta.config({ssid="departmentofinter.net", pwd="baguette2175bagel", auto=true})
|
||||||
|
|
||||||
|
wifi.ap.config({ssid="cyberkontrol", pwd="baguette2175bagel", auth=wifi.AUTH_WPA2_PSK,
|
||||||
|
channel=11, hidden=false, max=4, becaon=100}, true)
|
||||||
|
|
||||||
|
wifi.ap.setip({ip=192.168.0.1, netmask=255.255.255.0, gateway=192.168.0.1, dns=1.1.1.1})
|
||||||
|
wifi.ap.sethostname(hostname)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
init()
|
||||||
|
|
||||||
|
return init
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user