summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Lasseter <nathan@4574.co.uk>2013-06-05 21:00:05 +0100
committerNathan Lasseter <nathan@4574.co.uk>2013-06-05 21:00:05 +0100
commitb64d5c1ddf48906b924ace4190d5fd5153901b7d (patch)
tree715d87b5a21d63cd79fda30c8c6512875b701433
Initial commit
-rw-r--r--.gitignore1
-rw-r--r--README.md26
-rw-r--r--config.yml12
-rw-r--r--temp.rb97
4 files changed, 136 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f7911d3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+tempworker.pid
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f410fd7
--- /dev/null
+++ b/README.md
@@ -0,0 +1,26 @@
+Raspberry Pi Temperature Monitor
+================================
+
+Installation
+------------
+
+1. Add the following lines to your Pi's /etc/modules
+
+> w1-gpio
+> w1-therm
+
+2. Reboot, or run:
+
+> sudo modprobe w1-gpio
+> sudo modprobe w1-therm
+
+3. Set the correct settings in the config.yml file
+
+Running
+-------
+
+The software runs like an initscript. Once started, it daemonises and returns you to a terminal.
+
+> ruby temp.rb start
+> ruby temp.rb restart
+> ruby temp.rb stop
diff --git a/config.yml b/config.yml
new file mode 100644
index 0000000..4ce8d5d
--- /dev/null
+++ b/config.yml
@@ -0,0 +1,12 @@
+---
+:broker:
+ :remote: 169.254.1.1
+ :clientid: jam256
+
+:topics:
+ :report: /dc/sensors
+ :lwt: /dc/status/failure
+ :status: /dc/status
+
+:reports:
+ :period: 30
diff --git a/temp.rb b/temp.rb
new file mode 100644
index 0000000..c899f70
--- /dev/null
+++ b/temp.rb
@@ -0,0 +1,97 @@
+require 'rubygems'
+require 'mqtt'
+require 'yaml'
+
+CONFIG = open("config.yml") do |fd| YAML.load fd end
+
+def start
+
+ pid = Process.fork
+
+ if pid.nil? then
+
+ cli = MQTT::Client.new(
+ :remote_host => CONFIG[:broker][:remote],
+ :client_id => CONFIG[:broker][:clientid],
+ :will_topic => CONFIG[:topics][:lwt],
+ :will_payload => "Last Will and Testament: #{CONFIG[:broker][:clientid]} has died!"
+ )
+
+ cli.connect do |c|
+
+ Signal.trap("TERM") do
+ c.publish "#{CONFIG[:topics][:status]}/#{CONFIG[:broker][:clientid]}", "Stopped"
+ c.disconnect
+ Process.exit! true
+ end
+
+ c.publish "#{CONFIG[:topics][:status]}/#{CONFIG[:broker][:clientid]}", "Started"
+
+ while true do
+
+ bulk = ""
+
+ IO.readlines("/sys/devices/w1_bus_master1/w1_master_slaves").each do |idn|
+ id = idn.chomp
+
+ lines = IO.readlines "/sys/devices/w1_bus_master1/#{id}/w1_slave"
+
+ spl = lines[1].split "="
+ temp = spl[1]
+
+ c.publish "#{CONFIG[:topics][:report]}/#{CONFIG[:broker][:clientid]}/#{id}", "#{temp}"
+ bulk += "#{id} : #{temp}"
+ end
+
+ c.publish "#{CONFIG[:topics][:report]}/#{CONFIG[:broker][:clientid]}/bulk", bulk
+
+ sleep CONFIG[:reports][:period]
+
+ end
+
+ end
+
+ else
+
+ File.open("tempworker.pid", "w") do |file|
+ file.write "#{CONFIG[:broker][:clientid]} : #{pid}"
+ end
+
+ puts "Worker #{CONFIG[:broker][:clientid]} started on PID #{pid}."
+
+ Process.detach(pid)
+
+ end
+
+end
+
+def stop
+
+ IO.readlines("tempworker.pid").each do |line|
+
+ linearr = line.split ":"
+ Process.kill "TERM", linearr[1].to_i
+
+ puts "Worker #{CONFIG[:broker][:clientid]}, PID #{linearr[1].to_i} killed."
+
+ end
+
+ File.unlink "tempworker.pid"
+
+end
+
+def restart
+ stop
+ start
+end
+
+case ARGV.first
+ when "start"
+ start
+ when "stop"
+ stop
+ when "restart"
+ restart
+ else
+ puts "Usage: ruby temp.rb { start | stop | restart }"
+end