Skip to content

Commit

Permalink
Add: Temperature Measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
iboard committed Aug 27, 2018
1 parent 3087f0d commit 918eb63
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
15 changes: 15 additions & 0 deletions dev.fish
@@ -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





5 changes: 5 additions & 0 deletions ui/assets/js/leds.js
Expand Up @@ -39,4 +39,9 @@ channel.on("button-released", payload => {
$("#push-button-1").html("Button 1 released")
})

channel.on("temperature", payload => {
console.log("Temperature Measurement received", payload)
$("#temperature").html(`${payload.celcius} ˚C`)
})


3 changes: 2 additions & 1 deletion ui/lib/ui/application.ex
Expand Up @@ -12,7 +12,8 @@ defmodule Ui.Application do
supervisor(UiWeb.Endpoint, []),
# Start your own worker by calling: Ui.Worker.start_link(arg1, arg2, arg3)
# worker(Ui.Worker, [arg1, arg2, arg3]),
worker(Ui.SwitchListener, [])
worker(Ui.SwitchListener, []),
worker(Ui.Temperature, [])
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
8 changes: 8 additions & 0 deletions ui/lib/ui/switch_listener.ex
Expand Up @@ -85,4 +85,12 @@ defmodule Ui.SwitchListener do
end)
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
44 changes: 44 additions & 0 deletions ui/lib/ui/temperatur.ex
@@ -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
2 changes: 2 additions & 0 deletions ui/lib/ui_web/templates/leds/index.html.eex
Expand Up @@ -47,6 +47,8 @@ PUSH-BUTTON:
<div data-led="red" class="led-button">Red <span id="red-led"></span></div>
<div data-led="off" class="led-button">All Off</div>
<div id="push-button-1" class="push-button">Push Button 1 released</div>
<hr>
<h1 id="temperature"></h1>
</div>
</div>
</div>
Expand Down

0 comments on commit 918eb63

Please sign in to comment.