|
| 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 Wifi do |
| 22 | + @moduledoc """ |
| 23 | + Wi-Fi example for Elixir. |
| 24 | + """ |
| 25 | + |
| 26 | + @compile {:no_warn_undefined, :atomvm} |
| 27 | + @compile {:no_warn_undefined, :network} |
| 28 | + |
| 29 | + @sta_config Application.compile_env!(:wifi, :sta) |
| 30 | + @ap_config Application.compile_env!(:wifi, :ap) |
| 31 | + |
| 32 | + @clock_interval_ms 5_000 |
| 33 | + |
| 34 | + def start do |
| 35 | + case verify_platform(:atomvm.platform()) do |
| 36 | + :ok -> |
| 37 | + start_network() |
| 38 | + |
| 39 | + error -> |
| 40 | + error |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + defp start_network() do |
| 45 | + network_config = [ |
| 46 | + ap: |
| 47 | + [ |
| 48 | + ap_started: &ap_started/0, |
| 49 | + sta_connected: &sta_connected/1, |
| 50 | + sta_ip_assigned: &sta_ip_assigned/1, |
| 51 | + sta_disconnected: &sta_disconnected/1 |
| 52 | + ] ++ @ap_config, |
| 53 | + sta: |
| 54 | + [ |
| 55 | + connected: &connected/0, |
| 56 | + got_ip: &got_ip/1, |
| 57 | + disconnected: &disconnected/0 |
| 58 | + ] ++ @sta_config, |
| 59 | + sntp: [ |
| 60 | + host: "time-d-b.nist.gov", |
| 61 | + synchronized: &sntp_synchronized/1 |
| 62 | + ] |
| 63 | + ] |
| 64 | + |
| 65 | + case :network.start(network_config) do |
| 66 | + {:ok, _pid} -> |
| 67 | + IO.puts("wifi: network started") |
| 68 | + Process.sleep(:infinity) |
| 69 | + |
| 70 | + error -> |
| 71 | + error |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + defp ap_started, do: IO.puts("AP started") |
| 76 | + |
| 77 | + defp sta_connected(mac), do: IO.puts("STA connected with mac #{inspect(mac)}") |
| 78 | + |
| 79 | + defp sta_disconnected(mac), do: IO.puts("STA disconnected with mac #{inspect(mac)}") |
| 80 | + |
| 81 | + defp sta_ip_assigned(address), do: IO.puts("STA assigned address #{inspect(address)}") |
| 82 | + |
| 83 | + defp connected, do: IO.puts("STA connected") |
| 84 | + |
| 85 | + defp disconnected, do: IO.puts("STA disconnected") |
| 86 | + |
| 87 | + defp got_ip(ip_info) do |
| 88 | + IO.puts("Got IP: #{inspect(ip_info)}") |
| 89 | + _clock_pid = spawn(fn -> clock_loop() end) |
| 90 | + :ok |
| 91 | + end |
| 92 | + |
| 93 | + defp sntp_synchronized(timeval = {_tv_sec, _tv_usec}) do |
| 94 | + IO.puts("Synchronized time with SNTP server #{inspect(timeval)}") |
| 95 | + end |
| 96 | + |
| 97 | + defp verify_platform(:esp32), do: :ok |
| 98 | + defp verify_platform(:pico), do: :ok |
| 99 | + defp verify_platform(platform), do: {:error, {:unsupported_platform, platform}} |
| 100 | + |
| 101 | + defp clock_loop do |
| 102 | + {{year, month, day}, {hour, minute, second}} = :erlang.universaltime() |
| 103 | + ms = :erlang.system_time(:millisecond) |
| 104 | + |
| 105 | + :io.format( |
| 106 | + ~c"Date: ~4..0B/~2..0B/~2..0B ~2..0B:~2..0B:~2..0B (~pms)~n", |
| 107 | + [year, month, day, hour, minute, second, ms] |
| 108 | + ) |
| 109 | + |
| 110 | + Process.sleep(@clock_interval_ms) |
| 111 | + clock_loop() |
| 112 | + end |
| 113 | +end |
0 commit comments