purpleair 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/exe/purpleair_mqtt_bridge +103 -0
  3. metadata +142 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7a46f10e9ea92f5cf51ca2fbdcede2555d82e9f63a8deab6aa2d2367e34c3744
4
+ data.tar.gz: f33f4dc111d719da869533ddbde9f8cc3084600d3d38a993c3b7ccd81c112405
5
+ SHA512:
6
+ metadata.gz: 603ba3dae50a9e3f132df6d959cdad330dcac6fc69be1287ec009a383125e3c3c110be7283bf561d20c0a37f628be9c2b9981e48635cc2e6b52386519f2109e8
7
+ data.tar.gz: d92dad0ba46e4c44901fc6386546f09b6f69574a2e1b5a10ac3e84b12e5c04b10e60ea0ea51a42d4e1d1ba0c348f53b921db007cf6f8a246cc81779869ce6722
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "faraday_middleware"
5
+ require "homie-mqtt"
6
+ require "optparse"
7
+
8
+ refresh = 30.0
9
+ live = true
10
+
11
+ options = OptionParser.new do |opts|
12
+ opts.banner = "Usage: purpleair_mqtt_bridge SENSOR_IP MQTT_URI [options]"
13
+
14
+ opts.on("--refresh-interval=INTERVAL", "Refresh interval (default 30s)") { |v| refresh = v.to_f }
15
+ opts.on("--[no-]live", "Use live data (or 120s average) (default live)") { |v| live = v }
16
+ opts.on("-h", "--help", "Prints this help") do
17
+ puts opts
18
+ exit
19
+ end
20
+ end
21
+
22
+ options.parse!
23
+
24
+ unless ARGV.length == 2
25
+ puts options
26
+ exit 1
27
+ end
28
+
29
+ purple = Faraday.new(url: "http://#{ARGV[0]}/json?live=#{live}") do |f|
30
+ f.request :retry
31
+ f.response :raise_error
32
+ f.response :json
33
+ f.adapter :net_http_persistent
34
+ end
35
+
36
+ data = purple.get.body
37
+
38
+ homie = MQTT::Homie::Device.new(MQTT::Homie.escape_id(data["Geo"]), "PurpleAir Sensor", mqtt: ARGV[1])
39
+ device = homie.node("device", "Device", "Device")
40
+ device.property("temperature", "Current Temperature", :integer, data["current_temp_f"], unit: "ºF")
41
+ device.property("relative-humidity", "Current Relative Humidity", :integer, data["current_humidity"],
42
+ format: 0..100, unit: "%")
43
+ device.property("dewpoint", "Current Dew Point", :integer, data["current_dewpoint_f"], unit: "ºF")
44
+ device.property("pressure", "Barometric Pressure", :integer, data["pressure"], unit: "hPA")
45
+ device.property("rssi", "WiFi RSSI", :integer, data["rssi"], unit: "dB")
46
+
47
+ FIELDS = %w[
48
+ pm2.5_aqi
49
+ pm1_0_atm
50
+ pm2_5_atm
51
+ pm10_0_atm
52
+ p_0_3_um
53
+ p_0_5_um
54
+ p_1_0_um
55
+ p_2_5_um
56
+ p_5_0_um
57
+ p_10_0_um
58
+ ].freeze
59
+
60
+ def get_fields(data, channel_id)
61
+ suffix = "_b" if channel_id == "b"
62
+ result = {}
63
+ FIELDS.each do |field|
64
+ result[field] = data["#{field}#{suffix}"]
65
+ end
66
+ result
67
+ end
68
+
69
+ %w[a b].each do |channel_id|
70
+ fields = get_fields(data, channel_id)
71
+ channel = homie.node(channel_id, "Sensor Channel #{channel_id.upcase}", "Air Sensor")
72
+ channel.property("pm2-5-aqi", "PM2.5 AQI", :integer, fields["pm2.5_aqi"])
73
+ channel.property("pm1-0-atm", "PM1.0", :float, fields["pm1_0_atm"], unit: "µg/m3")
74
+ channel.property("pm2-5-atm", "PM2.5", :float, fields["pm2_5_atm"], unit: "µg/m3")
75
+ channel.property("pm10-0-atm", "PM10", :float, fields["pm10_0_atm"], unit: "µg/m3")
76
+ channel.property("p-0-3-um", "# particles >= 0.3µm per decileter", :float, fields["p_0_3_um"])
77
+ channel.property("p-0-5-um", "# particles >= 0.5µm per decileter", :float, fields["p_0_5_um"])
78
+ channel.property("p-1-0-um", "# particles >= 1.0µm per decileter", :float, fields["p_1_0_um"])
79
+ channel.property("p-2-5-um", "# particles >= 2.5µm per decileter", :float, fields["p_2_5_um"])
80
+ channel.property("p-5-0-um", "# particles >= 5.0µm per decileter", :float, fields["p_5_0_um"])
81
+ channel.property("p-10-0-um", "# particles >= 10µm per decileter", :float, fields["p_10_0_um"])
82
+ end
83
+
84
+ homie.publish
85
+
86
+ loop do
87
+ data = purple.get.body
88
+ device["temperature"].value = data["current_temp_f"]
89
+ device["relative-humidity"].value = data["current_humidity"]
90
+ device["dewpoint"].value = data["current_dewpoint_f"]
91
+ device["pressure"].value = data["pressure"]
92
+ device["rssi"].value = data["rssi"]
93
+
94
+ %w[a b].each do |channel_id|
95
+ fields = get_fields(data, channel_id)
96
+ channel = homie[channel_id]
97
+ FIELDS.each do |field|
98
+ channel[field.gsub(/[._]/, "-")].value = fields[field]
99
+ end
100
+ end
101
+
102
+ sleep refresh
103
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: purpleair
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cody Cutrer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday_middleware
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: homie-mqtt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: net-http-persistent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '9.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '9.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.19'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.19'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.6'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.6'
111
+ description:
112
+ email: cody@cutrer.com'
113
+ executables:
114
+ - purpleair_mqtt_bridge
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - exe/purpleair_mqtt_bridge
119
+ homepage: https://github.com/ccutrer/ruby-purpleair
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '2.5'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.1.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Publish PurpleAir sensor data to mqtt
142
+ test_files: []