waterboy/nodemcu/init.fnl

63 lines
2.2 KiB
Fennel

(local credentials (require "credentials"))
(local application (require "application"))
(global disconnect_ct nil)
(fn startup []
(if (not application)
(do
(print "init.lua deleted or renamed")
(/ 1 0)) ; lol
(do
(print "Running")
(application))))
(fn wifi_connect_event [T]
(print (.. "Connection to AP(" T.SSID ") established."))
(print "Waiting for IP Address...")
(if (not= disconnect_ct nil)
(global disconnect_ct nil)))
(fn wifi_got_ip_event [T]
(print (.. "WiFi connection is ready. IP address is: " T.IP))
(print "Startup will resume momentarily, you have 5 seconds to abort.")
(let [timer (tmr.create)]
(: timer :alarm 5000 tmr.ALARM_SINGLE startup)))
(fn wifi_disconnect_event [T]
(var reconnect_tries_max 75)
(when (= T.reason wifi.eventmon.reason.ASSOC_LEAVE)
(print "Lost AP..."))
(print (.. "WiFi connection to AP(" T.SSID ") has failed."))
(each [key value (pairs wifi.eventmon.reason)]
(when (= value T.reason)
(print (.. "Disconnect reason: " value " (" key ")"))))
(if (= disconnect_ct nil)
(global disconnect_ct 1)
(global disconnect_ct (+ 1 disconnect_ct)))
(if (< disconnect_ct reconnect_tries_max)
(print (.. "Retrying connection (attempt " disconnect_ct " of " reconnect_tries_max ")"))
(do (print "Aborting connection attempt.")
(wifi.sta.disconnect)
(global disconnect_ct nil))))
(wifi.eventmon.register wifi.eventmon.STA_CONNECTED wifi_connect_event)
(wifi.eventmon.register wifi.eventmon.STA_GOT_IP wifi_got_ip_event)
(wifi.eventmon.register wifi.eventmon.STA_DISCONNECTED wifi_disconnect_event)
;; ;; Scanning code for debug
;; (wifi.sta.getap 1
;; (fn [t]
;; (print "\n\t\t\tSSID\t\t\t\t\tBSSID\t\t\t RSSI\t\tAUTHMODE\t\tCHANNEL")
;; (each [bssid v (pairs t)]
;; (let [(ssid rssi authmode channel)
;; (string.match v "([^,]+),([^,]+),([^,]+),([^,]*)")]
;; (print (.. (string.format "%32s" ssid)
;; "\t" bssid "\t " rssi
;; "\t\t" authmode "\t\t\t" channel))
;; (when (= ssid credentials.SSID))))))
(print "Connecting to AP...")
(wifi.setmode wifi.STATION)
(wifi.sta.config {:ssid credentials.SSID :pwd credentials.PASSWORD})