Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env fish | ||
|
||
|
||
set -gx MIX_TARGET "host" | ||
set -gx NERVES_NETWORK_SSID "not used" | ||
set -gx NERVES_NETWORK_PSK "not used" | ||
set -gx MIX_ENV "dev" | ||
set -gx MIX_HOST "0.0.0.0" | ||
set -gx MIX_PORT 4000 | ||
set -gx PORT 4000 | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule Ui.Temperature do | ||
|
||
alias ElixirALE.I2C | ||
|
||
def start_link() do | ||
if System.get_env("MIX_TARGET") != "host" do | ||
{:ok, sensors} = I2C.start_link("i2c-1", 0x48) | ||
spawn(fn -> loop(%{sensors: sensors}) end) | ||
else | ||
spawn(fn -> loop(%{sensors: -1}) end) | ||
end | ||
{:ok, self()} | ||
end | ||
|
||
def loop(%{sensors: sensors} = state) do | ||
celcius = read_celcius(sensors,0) | ||
UiWeb.Endpoint.broadcast("nerves:lobby", "temperature", %{ celcius: celcius }) | ||
:timer.sleep(1000) | ||
loop(state) | ||
end | ||
|
||
defp read_celcius(sensors,register) do | ||
advalue = read_sensor(sensors,register) | ||
volt = advalue*3.3/255.0; | ||
|
||
rt = 10*volt / (3.3-volt) | ||
temp_k = 1/(1/(273.15+25) + :math.log(rt/10)/3950) | ||
temp_c = temp_k - 273.15 | ||
to_string(:io_lib.format("~6.2f~n", [temp_c])) | ||
end | ||
|
||
defp read_sensor(pid, channel) when pid == -1 do | ||
channel_value = Enum.random((0..255)) | ||
end | ||
defp read_sensor(pid, channel) do | ||
{channel_value, _} = Integer.parse("#{channel + 40}", 16) | ||
I2C.write(pid, <<channel_value>>) | ||
I2C.read(pid, 1) | ||
<<value>> = I2C.read(pid, 1) | ||
value | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters