Skip to content

Commit

Permalink
Add epmd_disterl esp32 example
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
  • Loading branch information
pguyot committed Jan 11, 2025
1 parent 87b2607 commit 78f2755
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/erlang/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ pack_runnable(sx127x sx127x eavmlib estdlib)
pack_runnable(reformat_nvs reformat_nvs eavmlib)
pack_runnable(uartecho uartecho eavmlib estdlib)
pack_runnable(ledc_example ledc_example eavmlib estdlib)
pack_runnable(epmd_disterl epmd_disterl eavmlib estdlib)
56 changes: 56 additions & 0 deletions examples/erlang/esp32/epmd_disterl.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
%
% This file is part of AtomVM.
%
% Copyright 2025 Paul Guyot <pguyot@kallisys.net>
%
% 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
%

-module(epmd_disterl).

-export([start/0]).

start() ->
Creds = [
{ssid, "myssid"},
{psk, "mypsk"}
],
case network:wait_for_sta(Creds, 30000) of
{ok, {Address, _Netmask, _Gateway}} ->
distribution_start(Address);
Error ->
io:format("An error occurred starting network: ~p~n", [Error])
end.

distribution_start(Address) ->
{ok, _EPMDPid} = epmd:start_link([]),
{ok, _KernelPid} = kernel:start(normal, []),
{X, Y, Z, T} = Address,
Node = list_to_atom(lists:flatten(io_lib:format("atomvm@~B.~B.~B.~B", [X, Y, Z, T]))),
{ok, _NetKernelPid} = net_kernel:start(Node, #{name_domain => longnames}),
io:format("Distribution was started\n"),
io:format("Node is ~p\n", [node()]),
net_kernel:set_cookie(<<"AtomVM">>),
io:format("Cookie is ~s\n", [net_kernel:get_cookie()]),
register(disterl, self()),
io:format(
"This AtomVM node is waiting for 'quit' message, and this process is registered as 'disterl'\n"
),
io:format("On an OTP node with long names distribution, run:\n"),
io:format("erlang:set_cookie('~s', 'AtomVM').\n", [Node]),
io:format("{disterl, '~s'} ! quit.\n", [Node]),
receive
quit -> ok
end.

0 comments on commit 78f2755

Please sign in to comment.