commit 9b342a7f34e760496826e163258eeae6f595b57b Author: cqc Date: Sat Jul 24 00:53:12 2021 -0500 basic nodemcu stuff in lua/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e807778 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +#*# +.#* +*~ + +nodemcu-firmware-esp32/ +nodemcu-firmware/ +ref/ \ No newline at end of file diff --git a/_.org b/_.org new file mode 100644 index 0000000..3ac9a58 --- /dev/null +++ b/_.org @@ -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 + diff --git a/lua/Makefile b/lua/Makefile new file mode 100644 index 0000000..5e76781 --- /dev/null +++ b/lua/Makefile @@ -0,0 +1,2 @@ +upload: init.lua + luatool.py --ip 192.168.1.174:2323 -vd -f init.lua diff --git a/lua/adc.lua b/lua/adc.lua new file mode 100644 index 0000000..9187fe9 --- /dev/null +++ b/lua/adc.lua @@ -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 + + + + diff --git a/lua/display.lua b/lua/display.lua new file mode 100644 index 0000000..23da308 --- /dev/null +++ b/lua/display.lua @@ -0,0 +1 @@ +dofile('lcd.lua') diff --git a/lua/init.lua b/lua/init.lua new file mode 100644 index 0000000..3427ff2 --- /dev/null +++ b/lua/init.lua @@ -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 + diff --git a/lua/lcd.lua b/lua/lcd.lua new file mode 100644 index 0000000..65dd223 --- /dev/null +++ b/lua/lcd.lua @@ -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!") + diff --git a/lua/print.lua b/lua/print.lua new file mode 100644 index 0000000..989e292 --- /dev/null +++ b/lua/print.lua @@ -0,0 +1,4 @@ + +function table_print(t) + return sjson.encode(t) +end diff --git a/lua/telnet.lua b/lua/telnet.lua new file mode 100644 index 0000000..76f1bf7 --- /dev/null +++ b/lua/telnet.lua @@ -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...") diff --git a/lua/wifi.lua b/lua/wifi.lua new file mode 100644 index 0000000..7f4fcd2 --- /dev/null +++ b/lua/wifi.lua @@ -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 + +