Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions elixir/EspNvs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!---
Copyright 2026 Masatoshi Nishiguchi
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
-->

# `EspNvs` Application

Welcome to the `EspNvs` AtomVM application.

The `EspNvs` AtomVM application uses ESP32 non-volatile storage (NVS) to store a small value.
If the NVS storage has not been set for this application, it will store a default value once. The application then logs the result and restarts every 10 seconds.

For more information about programming on the AtomVM platform, see the [AtomVM Programmers Guide](https://doc.atomvm.org/latest/programmers-guide.html).

For general information about building and executing Elixir AtomVM example programs, see the Elixir example program [README](../README.md).
60 changes: 60 additions & 0 deletions elixir/EspNvs/lib/esp_nvs.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#
# This file is part of AtomVM.
#
# Copyright 2026 Masatoshi Nishiguchi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
#

defmodule EspNvs do
@moduledoc """
Demonstrates ESP32 NVS usage by storing a small value once.
"""

@compile {:no_warn_undefined, :atomvm}
@compile {:no_warn_undefined, :esp}

@nvs_namespace :esp_nvs
@nvs_key :word
@default_word "Hello"
@restart_interval_ms 10_000

def start do
case verify_platform(:atomvm.platform()) do
:ok ->
case :esp.nvs_fetch_binary(@nvs_namespace, @nvs_key) do
{:ok, word} when is_binary(word) ->
IO.puts("Fetched word from NVS: #{word}")

{:error, :not_found} ->
IO.puts("No word found. Storing default word to NVS once: #{@default_word}")
:ok = :esp.nvs_put_binary(@nvs_namespace, @nvs_key, @default_word)

_ ->
IO.puts("Error reading NVS. Skipping write.")
end

IO.puts("Restarting in 10 seconds ...")
Process.sleep(@restart_interval_ms)
:esp.restart()

error ->
error
end
end

defp verify_platform(:esp32), do: :ok
defp verify_platform(platform), do: {:error, {:unsupported_platform, platform}}
end
51 changes: 51 additions & 0 deletions elixir/EspNvs/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# This file is part of AtomVM.
#
# Copyright 2026 Masatoshi Nishiguchi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
#

defmodule EspNvs.MixProject do
use Mix.Project

def project do
[
app: :esp_nvs,
version: "0.1.0",
elixir: "~> 1.17",
start_permanent: Mix.env() == :prod,
deps: deps(),
atomvm: [
start: EspNvs,
flash_offset: 0x250000
]
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:exatomvm, git: "https://github.com/atomvm/ExAtomVM/", branch: "main"}
]
end
end
16 changes: 16 additions & 0 deletions elixir/EspRtc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!---
Copyright 2026 Masatoshi Nishiguchi
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
-->

# `EspRtc` Application

Welcome to the `EspRtc` AtomVM application.

The `EspRtc` AtomVM application uses ESP32 RTC slow memory to record the number of times the device has restarted.
If no value has been stored in RTC slow memory yet, the counter is initialized to 0. The device will then sleep for 10 seconds and restart. After each restart, the counter is incremented and stored back to RTC slow memory.

For more information about programming on the AtomVM platform, see the [AtomVM Programmers Guide](https://doc.atomvm.org/latest/programmers-guide.html).

For general information about building and executing Elixir AtomVM example programs, see the Elixir example program [README](../README.md).
105 changes: 105 additions & 0 deletions elixir/EspRtc/lib/esp_rtc.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#
# This file is part of AtomVM.
#
# Copyright 2026 Masatoshi Nishiguchi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
#

defmodule EspRtc do
@moduledoc """
Demonstrates ESP32 RTC slow memory usage by persisting a restart counter.
"""

@compile {:no_warn_undefined, :atomvm}
@compile {:no_warn_undefined, :esp}

@restart_delay_ms 10_000

defmodule State do
@moduledoc false
defstruct count: 0
end
Comment on lines +31 to +34
Copy link
Collaborator

@petermm petermm Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick:
I'm not a fan of a nested module - I would just defstruct count: 0 here and then use %__MODULE__{count: count} or %EspRtcMemory{count: count} ?

(but certainly a matter of taste, so I'm good with a nested module if that is preferred )


def start do
case verify_platform(:atomvm.platform()) do
:ok ->
load_state()
|> increment_state()
|> save_state()

IO.puts("Restarting in 10 seconds ...")
Process.sleep(@restart_delay_ms)
:esp.restart()

:ok

error ->
error
end
end

defp load_state do
IO.puts("Loading count from RTC slow memory ...")

encoded_state =
try do
:esp.rtc_slow_get_binary()
catch
:error, :badarg ->
IO.puts("This device has not recorded the number of starts. Initializing to 0.")
:erlang.term_to_binary(%State{})
end

case safe_binary_to_term(encoded_state) do
{:ok, %State{count: count} = state} when is_integer(count) and count >= 0 ->
state

_ ->
IO.puts("Error: bad value, resetting to 0.")
%State{}
end
end

defp safe_binary_to_term(encoded_term) when is_binary(encoded_term) do
try do
{:ok, :erlang.binary_to_term(encoded_term)}
catch
:error, _ -> :error
end
end

defp save_state(current_state) do
IO.puts("Saving count to RTC slow memory ...")

current_state
|> :erlang.term_to_binary()
|> :esp.rtc_slow_set_binary()
end

defp increment_state(%State{count: count} = state) when is_integer(count) and count >= 0 do
new_count = count + 1
IO.puts("This device has restarted #{new_count} time(s).")
%State{state | count: new_count}
end

defp increment_state(_) do
IO.puts("Error: bad value, resetting to 0.")
%State{}
end

defp verify_platform(:esp32), do: :ok
defp verify_platform(platform), do: {:error, {:unsupported_platform, platform}}
end
51 changes: 51 additions & 0 deletions elixir/EspRtc/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# This file is part of AtomVM.
#
# Copyright 2026 Masatoshi Nishiguchi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
#

defmodule EspRtc.MixProject do
use Mix.Project

def project do
[
app: :esp_rtc,
version: "0.1.0",
elixir: "~> 1.17",
start_permanent: Mix.env() == :prod,
deps: deps(),
atomvm: [
start: EspRtc,
flash_offset: 0x250000
]
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:exatomvm, git: "https://github.com/atomvm/ExAtomVM/", branch: "main"}
]
end
end
6 changes: 4 additions & 2 deletions elixir/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ Some example programs can only run on specific platform. The following table su
|-----------------|-------|-------|------|--------|--------------|
| Blinky | ✅ | ✅ | ✅ | ✦ | ❌ |
| HelloWorld | ✅ | ✅ | ✅ | ✅ | ✅ |
| LEDC_Example | ✅ | ❌ | ❌ | ❌ | ❌ |
| WifiExample | ✅ | ❌ | ❌ | ✅ | ❌ |
| LEDC | ✅ | ❌ | ❌ | ❌ | ❌ |
| Wifi | ✅ | ❌ | ❌ | ✅ | ❌ |
| EspNvs | ✅ | ❌ | ❌ | ❌ | ❌ |
| EspRtc | ✅ | ❌ | ❌ | ❌ | ❌ |

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

Expand Down