Skip to content

Commit

Permalink
Merge pull request #27 from fikin/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
fikin committed Oct 1, 2023
2 parents f424cde + 0c59f12 commit 7a3c2b4
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 26 deletions.
4 changes: 2 additions & 2 deletions integration-tests/lua/testInit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ local function assertHassInfo()
local r = 'GET /api/ha/info HTTP/1.0\r\nAuthorization: Basic aGFzczphZG1pbg==\r\n\r\n'
local e = 'HTTP/1.0 200 OK\r\n' ..
'Cache-Control: private, no-cache, no-store\r\n' ..
'Content-Length: 120\r\n' ..
'Content-Length: 135\r\n' ..
'Content-Type: application/json\r\n' ..
'\r\n' ..
'{"hwVersion":"1.0.0","manufacturer":"fikin","model":"WeMos D1 mini","name":"nodemcu1234567890","swVersion":"1669271656"}'
'{"hwVersion":"1.0.0","manufacturer":"Noname vendor","model":"Generic NodeMCU make","name":"nodemcu1234567890","swVersion":"1669271656"}'
assert200HttpRequest(r, e)
end

Expand Down
30 changes: 26 additions & 4 deletions lua_modules/adc-ntc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,37 @@ Schematic is:

The default configuration is defined for:

- VCC=5V
- VccR1=5V
- R1=330k
- Rntc=50k
- NodeMCU board

$Vntc = VCC * Rntc / ( Rntc + R1 )$
_Note: stock ESP8266 chip accepts 1V in A0 input. NodeMCU boards are shipped with built-in voltage divider and accept 3.3V._

$Adc = 1023 * Vntc / 3.3$
Since resistor and voltage values are not perfect, there are some discrepancies between schematic and reality.
There are two way to influence for biased results:

where 1023 is Adc resolution and 3.3V is the A0 input max voltage i.e. NodeMCU board. Plain ESP8266 chips accept 1v max.
- Adjust R1 resistor value in the settings.
- With default configuration, each +15k => +1C.
- Use `correction factor`.
- Correction factor is ADC value added to A0 reading before Vntc is being calculated.
- With default configuration, +9 => +1C.

Perform temperature calibration or observe long term and adjust these if you see fit.

$Vstep = VccA0 / 1023$

$AdcValue = A0 + correction_factor$

$Vntc = Vstep * AdvValue$

$Rntc = R1 * Vntc / ( VccR1 - Vntc )$

$TempK = A + B * ln(Rntc) + C * ln(Rntc)^3$

$TempC = TempK - 273.15$

With default configuration following values could be expected:

| Temp (C) | Rntc (kOhm) | Vntc (V) | Adc (0-1024) |
| -------- | ----------- | -------- | ------------ |
Expand Down
5 changes: 3 additions & 2 deletions lua_modules/adc-ntc/fs/fs-adc-ntc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"AdcCorr": 1.09,
"Vcc": 3.3,
"AdcCorr": 0,
"VccA0": 3.3,
"VccR1": 5,
"R1": 330000,
"A": 9.656249129e-4,
"B": 2.106952165e-4,
Expand Down
10 changes: 6 additions & 4 deletions lua_modules/adc-ntc/lua/adc-ntc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ local adc = require("adc")

---@class adcNtc_cfg
---@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 VccA0 integer max voltage at A0 input i.e 3.3V for NodeMCU and 1V for ESP8266
---@field VccR1 integer voltage at R1 i.e 5V or 3.3V
---@field R1 integer resistor 1 in Kohm
---@field A number Steinhart-Hart model coefficient
---@field B number Steinhart-Hart model coefficient
Expand All @@ -36,9 +37,10 @@ end
local function main(onReadCb)
package.loaded[modname] = nil

local AdcValue = readAvg(20) * cfg.AdcCorr
local Vntc = cfg.Vcc * AdcValue / 1023
local Rntc = cfg.R1 * Vntc / (cfg.Vcc - Vntc)
local Vstep = cfg.VccA0 / 1023
local AdcValue = readAvg(20) + cfg.AdcCorr
local Vntc = Vstep * AdcValue
local Rntc = cfg.R1 * Vntc / (cfg.VccR1 - Vntc)
local LogRntc = math.log(Rntc)
local bVal = cfg.B * LogRntc
local cVal = cfg.C * LogRntc * LogRntc * LogRntc
Expand Down
2 changes: 2 additions & 0 deletions lua_modules/temp-sensor/lua/temp-sensor-get.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local function getState()
return require("state")("temp-sensor")
end

---returns temp-sernsor current value stored in state
---@return number
local function getTemp()
package.loaded[modname] = nil

Expand Down
2 changes: 1 addition & 1 deletion lua_modules/thermostat/lua/thermostat-control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ local function handleHvacMode()
local mode = state.data.hvac_mode
if mode == "off" then
ensureIsOff()
elseif mode == "on" then
elseif mode == "heat" then
ensureIsOn()
elseif mode == "auto" then
ensureIsAuto()
Expand Down
11 changes: 6 additions & 5 deletions lua_modules/thermostat/lua/thermostat-start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ local modname = ...
local log = require("log")

---@class thermostat_cfg_mode_cfg
---@field target_temperature_high integer
---@field target_temperature_low integer
---@field target_temperature_high number
---@field target_temperature_low number

---@class thermostat_cfg_mode
---@field away thermostat_cfg_mode_cfg
Expand All @@ -26,17 +26,18 @@ local log = require("log")

---@class thermostat_cfg_data
---@field temperature_unit string
---@field target_temperature_high integer
---@field target_temperature_low integer
---@field target_temperature_high number
---@field target_temperature_low number
---@field hvac_mode string
---@field hvac_modes string[]
---@field preset_mode string
---@field preset_modes string[]
---@field supported_features integer
---@field current_temperature number
---@field hvac_action string provided by HASS when setting other values

---@class thermostat_cfg
---@field periodMs integer
---@field tempSensorPin integer
---@field relayPin integer
---@field modes thermostat_cfg_mode[]
---@field data thermostat_cfg_data
Expand Down
4 changes: 2 additions & 2 deletions lua_modules/web-ha/lua/web-ha.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ local modname = ...
---@param conn http_conn*
local function getInfo(conn)
local data = {
manufacturer = "fikin",
manufacturer = "Noname vendor",
name = require("wifi").sta.gethostname(),
model = "WeMos D1 mini",
model = "Generic NodeMCU make",
swVersion = require("get-sw-version")().version,
hwVersion = "1.0.0"
}
Expand Down
14 changes: 8 additions & 6 deletions lua_modules/web-portal/fs/wifi-portal.html
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,18 @@ <h3 class="collapsible">Temp Sensor (ds18b20)</h3>
<h3 class="collapsible">Temp Sensor (adc NTC)</h3>
<div class="collapsible-content grid-container">
<label for="adcNtc_AdcCorr" class="grid-item"
>Read ADC-values correction factor<br />
use this to long term tune temp calculation<br />
start with 1 (no correction) and adjust<br />
>Read ADC-values correction factor.<br />
Use this to long term tune temp calculation.<br />
Start with 0 (no correction) increase/decrease<br />
after comparing room temp and sensor results:</label
>
<input id="adcNtc_AdcCorr" class="cfgValue grid-item" type="number" />
<input id="adcNtc_AdcCorr" class="cfgValue grid-item" type="number" step="1" />
<label for="adcNtc_R1" class="grid-item">Resistor 1 (ohm):</label>
<input id="adcNtc_R1" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_Vcc" class="grid-item">VCC (V):</label>
<input id="adcNtc_Vcc" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_VccA0" class="grid-item">VCC at A0 (V)<br />i.e 3.3 (NodeMCU) or 1 (ESP):</label>
<input id="adcNtc_VccA0" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_VccR1" class="grid-item">VCC at R1 (V)<br />likely 3.3 or 5:</label>
<input id="adcNtc_VccR1" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_A" class="grid-item">Steinhart-Hart model coefficient A:</label>
<input id="adcNtc_A" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_B" class="grid-item">Steinhart-Hart model coefficient B:</label>
Expand Down

0 comments on commit 7a3c2b4

Please sign in to comment.