Skip to content

Commit b50f818

Browse files
committed
Add Elixir ESP NVS example
1 parent 23f6b12 commit b50f818

4 files changed

Lines changed: 159 additions & 2 deletions

File tree

elixir/EspNvs/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!---
2+
Copyright 2023 Fred Dushin <fred@dushin.net>
3+
Copyright 2026 Masatoshi Nishiguchi
4+
5+
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
6+
-->
7+
8+
# `EspNvs` Application
9+
10+
Welcome to the `EspNvs` AtomVM application.
11+
12+
The `EspNvs` AtomVM application uses ESP32 non-volatile storage (NVS) to record the number of times the device has restarted.
13+
If the NVS storage has not been set for this application, it will be initialized with a count of 0. The device will then sleep for 10 seconds and restart. After each restart, the number of restarts is incremented and stored in NVS.
14+
15+
For more information about programming on the AtomVM platform, see the [AtomVM Programmers Guide](https://doc.atomvm.org/latest/programmers-guide.html).
16+
17+
For general information about building and executing Elixir AtomVM example programs, see the Elixir example program [README](../README.md).

elixir/EspNvs/lib/esp_nvs.ex

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#
2+
# This file is part of AtomVM.
3+
#
4+
# Copyright 2026 Masatoshi Nishiguchi
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
#
20+
21+
defmodule EspNvs do
22+
@moduledoc """
23+
Demonstrates ESP32 NVS usage by persisting a restart counter.
24+
"""
25+
26+
@compile {:no_warn_undefined, :atomvm}
27+
@compile {:no_warn_undefined, :esp}
28+
29+
@nvs_namespace :esp_nvs
30+
@nvs_key :starts
31+
@restart_delay_ms :timer.seconds(10)
32+
33+
require Record
34+
Record.defrecord(:state, count: 0)
35+
36+
def start do
37+
case verify_platform(:atomvm.platform()) do
38+
:ok ->
39+
load_state() |> increment_state() |> save_state()
40+
41+
IO.puts("Restarting in 10 seconds ...")
42+
:timer.sleep(@restart_delay_ms)
43+
:esp.restart()
44+
45+
error ->
46+
error
47+
end
48+
end
49+
50+
defp load_state do
51+
IO.puts("Loading count from NVS ...")
52+
53+
case :esp.nvs_get_binary(@nvs_namespace, @nvs_key) do
54+
:undefined ->
55+
IO.puts("This device has not recorded the number of starts. Initializing to 0.")
56+
state()
57+
58+
bin ->
59+
case :erlang.binary_to_term(bin) do
60+
state(count: count) ->
61+
IO.puts("Loaded count: #{count}")
62+
state(count: count)
63+
64+
_ ->
65+
IO.puts("Error: bad value, resetting to 0.")
66+
state()
67+
end
68+
end
69+
end
70+
71+
defp save_state(state) do
72+
IO.puts("Saving count to NVS ...")
73+
:esp.nvs_set_binary(@nvs_namespace, @nvs_key, :erlang.term_to_binary(state))
74+
end
75+
76+
defp increment_state(state(count: count)) when is_integer(count) do
77+
IO.puts("This device has restarted #{count + 1} time(s).")
78+
state(count: count + 1)
79+
end
80+
81+
defp increment_state(_) do
82+
IO.puts("Error: bad value, resetting to 0.")
83+
state()
84+
end
85+
86+
defp verify_platform(:esp32), do: :ok
87+
defp verify_platform(platform), do: {:error, {:unsupported_platform, platform}}
88+
end

elixir/EspNvs/mix.exs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#
2+
# This file is part of AtomVM.
3+
#
4+
# Copyright 2026 Masatoshi Nishiguchi
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
#
20+
21+
defmodule EspNvs.MixProject do
22+
use Mix.Project
23+
24+
def project do
25+
[
26+
app: :esp_nvs,
27+
version: "0.1.0",
28+
elixir: "~> 1.17",
29+
start_permanent: Mix.env() == :prod,
30+
deps: deps(),
31+
atomvm: [
32+
start: EspNvs,
33+
flash_offset: 0x250000
34+
]
35+
]
36+
end
37+
38+
# Run "mix help compile.app" to learn about applications.
39+
def application do
40+
[
41+
extra_applications: [:logger]
42+
]
43+
end
44+
45+
# Run "mix help deps" to learn about dependencies.
46+
defp deps do
47+
[
48+
{:exatomvm, git: "https://github.com/atomvm/ExAtomVM/", branch: "main"}
49+
]
50+
end
51+
end

elixir/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ Some example programs can only run on specific platform. The following table su
1616
|-----------------|-------|-------|------|--------|--------------|
1717
| Blinky ||||||
1818
| HelloWorld ||||||
19-
| LEDC_Example ||||||
20-
| WifiExample ||||||
19+
| LEDC ||||||
20+
| Wifi ||||||
21+
| EspNvs ||||||
2122

2223
✦ Works, but requires editing to use onboard LED, see the README in the examples directory.
2324

0 commit comments

Comments
 (0)