waterboy/nodemcu/application.fnl

38 lines
1.2 KiB
Fennel

(local adc (require "adc"))
(local httpserver (require "httpserver"))
(local configuration (require "configuration"))
(fn configure-adc []
(when (adc.force_init_mode adc.INIT_ADC)
(node.restart)))
(fn moisture-value []
(let [value (adc.read 0)
max configuration.vals.air
min configuration.vals.water]
(let [adjusted (- max value)
range (- max min)]
{:raw value :range range :adjusted adjusted :perc (/ adjusted range)})))
(fn prom-format [key val props]
(var base (.. key "{"))
(each [prop-key prop-value (pairs props)]
(set base (.. base prop-key "=\"" prop-value "\",")))
(.. (string.sub base 1 -2) "} " (string.format "%3.2f" val) "\n"))
(fn httphandler [req res]
(let [{:raw raw :range range :adjusted adjusted :perc perc} (moisture-value)
props configuration.props]
(: res :send "# OK\n")
(: res :send (prom-format "moisture_perc" perc props))
(: res :send (prom-format "moisture_raw" raw props))
(: res :send (prom-format "moisture_range" range props))
(: res :send (prom-format "moisture_adjusted" adjusted props))
(: res :finish)))
(fn doit []
(configure-adc)
(local server (httpserver.createServer 80 httphandler)))