Skip to content

Commit

Permalink
Merge pull request #26 from fikin/dev
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
fikin committed Sep 24, 2023
2 parents cb17a26 + eb3b876 commit f424cde
Show file tree
Hide file tree
Showing 16 changed files with 587 additions and 498 deletions.
4 changes: 3 additions & 1 deletion Makefile-spiffs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ spiffs-lst: spiffs-lc lfs-image spiffs-md5
@$(foreach f, $(FS_FILES), echo "import $(FS_DIR)/$(f) $(f)" >> ./vendor/spiffs.lst ;)

spiffs-minify: spiffs-lst
@mkdir -p vendor/minify
@docker run -it --rm \
-v $(PWD)/vendor/minify:/to \
-v $(PWD)/$(FS_DIR):/from \
-u $(id -u ${USER}):$(id -g ${USER}) \
-u $(shell id -u ${USER}):$(shell id -g ${USER}) \
tdewolff/minify -rv -o /to /from
@cp vendor/minify/*/* $(FS_DIR)/
@rm -rf vendor/minify

spiffs-image-esp32:
$(SPIFFSIMG) $(SPIFFS_SIZE) $(FS_DIR) $(FWDIR)/build/spiffs.img
Expand Down
38 changes: 38 additions & 0 deletions lua_modules/adc-ntc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Thermostat connected to A0 input.

Schematic is:

- resistor R1 connected to VCC and A0
- NTC connected to A0 and GND

The default configuration is defined for:

- VCC=5V
- R1=330k
- Rntc=50k

$Vntc = VCC * Rntc / ( Rntc + R1 )$

$Adc = 1023 * Vntc / 3.3$

where 1023 is Adc resolution and 3.3V is the A0 input max voltage i.e. NodeMCU board. Plain ESP8266 chips accept 1v max.

| Temp (C) | Rntc (kOhm) | Vntc (V) | Adc (0-1024) |
| -------- | ----------- | -------- | ------------ |
| -20C | 500 | 3.01 | 934 |
| 0C | 175 | 1.73 | 537 |
| +20C | 50 | 0.66 | 204 |
| +35C | 30 | 0.42 | 129 |

Various tutorials can be used as reference and background information. For example:

- [esp8266-ntc-temperature-thermistor](https://esp8266tutorials.blogspot.com/2016/09/esp8266-ntc-temperature-thermistor.html)
- [nodemcu thermistor](https://www.electronicwings.com/nodemcu/thermistor-interfacing-with-nodemcu)
- this one is having different schematics though
- [ntc-temperature-sensor](https://www.electronicdiys.com/2020/05/ntc-temperature-sensor-with-arduino-esp.html)

For calculating coefficients one can use:

- [NTC Calculator](https://www.thinksrs.com/downloads/programs/Therm%20Calc/NTCCalibrator/NTCcalculator.htm)
- [Thermistor 50K resistance table](https://www.bapihvac.com/wp-content/uploads/2010/11/Thermistor_50K.pdf)
- [TS104 50k resistance table](https://mcshaneinc.com/html/TS104_Specs.html?NewWin)
10 changes: 5 additions & 5 deletions lua_modules/adc-ntc/fs/fs-adc-ntc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"Rntc": 50000,
"AdcCorr": 1.09,
"Vcc": 3.3,
"R1": 330000,
"VCC": 5,
"A": 1.129148e-3,
"B": 2.34125e-4,
"C": 8.76741e-8
"A": 9.656249129e-4,
"B": 2.106952165e-4,
"C": 8.582985056e-8
}
42 changes: 15 additions & 27 deletions lua_modules/adc-ntc/lua/adc-ntc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ local modname = ...
local adc = require("adc")

---@class adcNtc_cfg
---@field Rntc integer NTC resistance in Kohm
---@field AdcCorr integer correction factor for read ADC values. 1 means no correction.
---@field Vcc integer max voltage at A0 input i.e 3.3V for NodeMCU and 1V for ESP8266
---@field R1 integer resistor 1 in Kohm
---@field VCC integer VCC in V
---@field A number Steinhart-Hart model coefficient
---@field B number Steinhart-Hart model coefficient
---@field C number Steinhart-Hart model coefficient
Expand All @@ -22,25 +22,6 @@ local adc = require("adc")
---@type adcNtc_cfg
local cfg = require("device-settings")(modname)

---natural logarithm approximation
---@param x number
---@return number
function ln(x) --natural logarithm function for x>0 real values
local y = (x - 1) / (x + 1)
local sum = 1
local val = 1
if (x == nil) then
return 0
end
-- we are using limited iterations to acquire reliable accuracy.
-- here its upto 10000 and increased by 2
for i = 3, 10000, 2 do
val = val * (y * y)
sum = sum + (val / i)
end
return 2 * y * sum
end

---@param cnt integer how to many readinds to average
---@return number average value
local function readAvg(cnt)
Expand All @@ -55,15 +36,22 @@ end
local function main(onReadCb)
package.loaded[modname] = nil

local dAdcValue = readAvg(10)
local dVout = (dAdcValue * cfg.VCC) / 1023
local dRth = (cfg.VCC * cfg.R1 / dVout) - cfg.R1
local AdcValue = readAvg(20) * cfg.AdcCorr
local Vntc = cfg.Vcc * AdcValue / 1023
local Rntc = cfg.R1 * Vntc / (cfg.Vcc - Vntc)
local LogRntc = math.log(Rntc)
local bVal = cfg.B * LogRntc
local cVal = cfg.C * LogRntc * LogRntc * LogRntc
-- Temperature in kelvin
local t = (1 / (cfg.A + (cfg.B * ln(dRth)) + (cfg.C * (ln(dRth)) ^ 3)))
local tK = 1 / (cfg.A + bVal + cVal)
-- Temperature in degree celsius
t = t - 273.15
local tC = tK - 273.15
-- Temperature in Farenheit
-- local tF = (tC * 9.0)/ 5.0 + 32.0;
-- require("log").debug("AdcValue=%f Vntc=%f Rntc=%f logRntc=%f bVal=%f cVal=%f tK=%f",
-- AdcValue, Vntc, Rntc, LogRntc, bVal, cVal, tK)

onReadCb({ adc = t })
onReadCb({ adc = tC })
end

return main
44 changes: 20 additions & 24 deletions lua_modules/ds18b20/lua/ds18b20.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local tmr = require("tmr")

local DS18B20FAMILY = 0x28
local DS1920FAMILY = 0x10 -- and DS18S20 series
local READ_ROM = 0x33
-- local READ_ROM = 0x33
local CONVERT_T = 0x44
local READ_SCRATCHPAD = 0xBE
local READ_POWERSUPPLY = 0xB4
Expand All @@ -34,22 +34,11 @@ local cfg = require("device-settings")(modname)

---@class ds18b20_struct
---@field pin integer
---@field cb function(ds18b20_temps)
---@field discoveredAddrs string[]
---@field validAddrs table

---@type ds18b20_struct
local callState = {
pin = cfg.pin,
---@type function(ds18b20_temps)
cb = nil,
---@type string[]
discoveredAddrs = nil,
---@type table
validAddrs = nil,
}

---converts ow sensor addr to string representation
---@field cb fun(ds18b20_temps)|nil
---@field discoveredAddrs string[]|nil
---@field validAddrs table|nil

---converts ow sensor addr to string representation
---@param addr string
---@return string
local function addrToStr(addr)
Expand Down Expand Up @@ -90,7 +79,7 @@ local function decodeTemps(rawTemps)
log.error("%s temp crc failed", addrToStr, addr)
end
else
log.error("%s temp signature failed", addrToStr, addr)
log.error("sensor %s provided no conversion result", addrToStr, addr)
end
end
return tbl
Expand Down Expand Up @@ -155,17 +144,17 @@ end

---@param pin integer
---@param addr string
local function startConvertion(pin, addr)
local function startConversion(pin, addr)
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, CONVERT_T, MODE)
end

---@param pin integer
---@param addrs string[]
local function startConvertions(pin, addrs)
local function startConversions(pin, addrs)
for _, addr in ipairs(addrs) do
startConvertion(pin, addr)
startConversion(pin, addr)
end
end

Expand Down Expand Up @@ -216,8 +205,8 @@ local function readT(o)
ow.setup(o.pin)
o.discoveredAddrs = readAddrs(o.pin)
o.validAddrs = assertSensors(o.pin, o.discoveredAddrs)
startConvertions(o.pin, o.validAddrs)
wait(o, 750)
startConversions(o.pin, o.validAddrs)
wait(o, 640)
end

---read DS18B20 sensor temperature over OW and returns its temperature.
Expand All @@ -228,7 +217,14 @@ end
local function main(onReadCb)
package.loaded[modname] = nil

callState.cb = onReadCb
---@type ds18b20_struct
local callState = {
pin = cfg.pin,
cb = onReadCb,
discoveredAddrs = nil,
validAddrs = nil,
}

readT(callState)
return callState
end
Expand Down
2 changes: 1 addition & 1 deletion lua_modules/http-srv/lua/http-conn-gc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ local function main(conn, hasErr)
for _, v in ipairs(conn.onGcFn) do
local ok, err = pcall(v, hasErr) -- cleanup callbacks
if not ok then
require("log").error("cleanup connection", err)
require("log").error("connection cleanup callback : %s", err)
end
end
conn.onGcFn = nil
Expand Down

0 comments on commit f424cde

Please sign in to comment.