Skip to content

Commit

Permalink
Merge pull request #24 from fikin/dev
Browse files Browse the repository at this point in the history
Adding bootstrap support. Fixing some readmes.
  • Loading branch information
fikin committed Sep 8, 2023
2 parents 7ea91a1 + 615415c commit 7db49c4
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.config
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ lua=53
lua-init=require('init')

lua-modules=bootprotect
lua-modules=bootstrap
lua-modules=device-settings
lua-modules=ds18b20
lua-modules=dummy-strings
Expand Down
14 changes: 14 additions & 0 deletions lua_modules/bootstrap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Bootstrap

To be called at boot time to execute one-time configuration actions.

Typically used in sw upgrade use cases.

In case there is a need to one-off configuration during next reboot,
place commands in a file called `bootstrap-sw.lua` and reboot the device.

It such file and if it exists:

- runs it
- captures all errors into `bootstrap-sw.PANIC.txt`
- removes it regardless of execution status, so next reboot does not take place
22 changes: 22 additions & 0 deletions lua_modules/bootstrap/fs/bootstrap-sw.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--[[
This file is template, modify it with one-time configuration steps
to be executed after reboot.
Following settings are relevant for initial flashing of the device,
consequent sw upgrades should have its own changes if needed be.
]]

local fs = require("factory-settings")

--fs("wifi-sta"):set("config.ssid","<SSID>"):set("config.pwd","<PWD>"):set("hostname","<HOSTNAME>"):done()

-- minimal set of modules on
-- fs("init-seq"):set("bootsequence",{ "bootstrap", "user-settings", "log-start", "wifi-apply-config", "wifi-mgr", "http-srv", "telnet" }):done()

-- likely same credentials for admin services
-- fs("telnet"):set("usr", "<ADMIN>"):set("pwd", "<APWD>"):done()
-- fs("web-portal"):set("usr", "<ADMIN>"):set("pwd", "<APWD>"):done()
-- fs("web-ota"):set("usr", "<ADMIN>"):set("pwd", "<APWD>"):done()

-- minimal HomeAssistant modules and credentials
--fs("web-ha"):set("entities", { "system-hass" }):set("credentials.usr", "<HAUSER>"):set("credentials.pwd", "<HAPWD>"):done()
25 changes: 25 additions & 0 deletions lua_modules/bootstrap/lua/bootstrap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local file, log = require("file"), require("log")

local fName = "bootstrap-sw.lua"
local fNameErr = "bootstrap-sw.PANIC.txt"

local function main()
if file.exists(fName) then
log.info("running %s", fName)

local ok, err = pcall(require, "bootstrap-sw")

file.remove(fName)

if not ok then
log.error("bootstrap failed : %s : %s", fName, err)
file.remove(fNameErr)
file.putcontents(fNameErr, err)
end

collectgarbage()
collectgarbage()
end
end

return main
1 change: 1 addition & 0 deletions lua_modules/device-settings/lua/device-settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local modname = ...
---@param fName string
---@return table
local function loadJsonFile(fName)
local file = require("file")
if file.exists(fName) then
return require("read-json-file")(fName)
end
Expand Down
2 changes: 1 addition & 1 deletion lua_modules/init/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

Main entry during bootup.

It initializes LFS, then boots all startup modules mentioned in the device settings using bootprotect.
It initializes LFS, then boots all startup modules mentioned in the device settings using `bootprotect`.

Probably best place to add own logic is to write it in own Lua module and include it at the end of the boot sequence.
1 change: 1 addition & 0 deletions lua_modules/init/fs/fs-init-seq.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"bootsequence": [
"bootstrap",
"user-settings",
"log-start",
"wifi-apply-config",
Expand Down
15 changes: 15 additions & 0 deletions lua_modules/sct013-sensor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SCT014 Current sensor

Expose `SCT013` connected via `ADS1115` as Home Assistant `current` sensor.

## Setup

TODO

## References

[Using the ADS1115](https://www.best-microcontroller-projects.com/ads1115.html)

[ADS1115 analog-to-digital converter and ESP8266](http://www.esp8266learning.com/ads1115-analog-to-digital-converter-and-esp8266.php)

[SCT013-000 CT USED WITH ARDUINO](https://www.poweruc.pl/blogs/news/non-invasive-sensor-yhdc-sct013-000-ct-used-with-arduino-sct-013)
2 changes: 1 addition & 1 deletion lua_modules/sct013-sensor/lua/sct013-sensor-start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ local function prepareRteState(ads)
-- remember in RTE state
require("state")(moduleName, {
ads = ads,
data = { native_value = 0 },
data = { native_value = 0.0 },
})
end

Expand Down

0 comments on commit 7db49c4

Please sign in to comment.