sds011 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sds011.rb +85 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a0136a9fd9be9fd0084c45318ef501b61616e0eb6f161319b59750f422a96984
4
+ data.tar.gz: fa1f490d362f00157a3e5e99fc38aef4d27c502ae188c11e2c2d8d572e129805
5
+ SHA512:
6
+ metadata.gz: 5c6070571dfc36b1f713f6282fc764e3659ac97c1fb07815e9ad47395c4a846db6ef96288b5c4cca77be46765901eaae65e0a7b7ebf43c360da6e82727027c27
7
+ data.tar.gz: 5be2bfd99e3103443f13aed1a1415626cb501b17bd04e757f5b0cf783930e3fe61ef3fd89536a8f979ee0bff64ccdccb18b76c7db32c4abd83e8fdb89d65241c
data/lib/sds011.rb ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/ruby
2
+ require "rubygems"
3
+ require "serialport"
4
+ require "digest"
5
+ require "pp"
6
+
7
+ class SDS011
8
+ # SDS emits this packet at 1Hz frequency
9
+ @@reading = [
10
+ 'header', # 0xAA
11
+ 'cmd_type', # 0xC0
12
+ 'pm25_low', # PM2.5, low byte
13
+ 'pm25_high', # PM2.5, hi byte
14
+ 'pm10_low', # PM10, low byte
15
+ 'pm10_high', # PM10, hi byte
16
+ 'id_low', # Sensor ID, low byte
17
+ 'id_high', # Senbsor ID, hi byte
18
+ 'checksum', # OR result of the 6 data bytes
19
+ 'tail' # 0xAB
20
+ ];
21
+
22
+ # Command packet format
23
+ @@query = {
24
+ head: '0xAA',
25
+ cmd_id: '0xC0',
26
+ data01: '0x00',
27
+ data02: '0x00',
28
+ data03: '0x00',
29
+ data04: '0x00',
30
+ data05: '0x00',
31
+ data06: '0x00',
32
+ data07: '0x00',
33
+ data09: '0x00',
34
+ data10: '0x00',
35
+ data11: '0x00',
36
+ data12: '0x00',
37
+ data13: '0x00',
38
+ data14: '0xFF', # FF: All sensors, otherwise Device ID byte 1
39
+ data15: '0xFF', # FF: All sensors, otherwise Device ID byte 2
40
+ checksum: '0',
41
+ tail: '0xAB'
42
+ };
43
+
44
+ def reading
45
+ @@reading
46
+ end
47
+
48
+ def query
49
+ @@query
50
+ end
51
+
52
+ def bytes_to_int(low_byte, high_byte)
53
+ ((high_byte.to_i << 8) + low_byte.to_i) /10
54
+ end
55
+
56
+ def send_command(cmd_hash)
57
+ sp = SerialPort.new("/dev/ttyUSB0", "9600".to_i)
58
+ sp.read_timeout = 100
59
+ sp.flush()
60
+ puts cmd_hash[:data14].to_s
61
+ sp.close
62
+ end
63
+
64
+ def reading()
65
+ # Baud, databits, stopbits, parity
66
+ sp = SerialPort.new("/dev/ttyUSB0", 9600, 8, 1, SerialPort::NONE)
67
+ sp.read_timeout = 100
68
+ sp.flush()
69
+ sds_bytes=0
70
+ c=nil
71
+ reading_packet = Hash.new()
72
+
73
+ while sds_bytes < 10
74
+ c=sp.read(1)
75
+ reading_packet[@@reading[sds_bytes]] = c.ord.to_s(10)
76
+ sds_bytes = sds_bytes + 1
77
+ end
78
+
79
+ sp.close
80
+ {
81
+ pm25: bytes_to_int(reading_packet['pm25_low'],reading_packet['pm25_high']),
82
+ pm10: bytes_to_int(reading_packet['pm10_low'],reading_packet['pm10_high'])
83
+ }
84
+ end
85
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sds011
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Sambarino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: serialport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.2
27
+ description: A simple gem to interact with SDS011 sensors via serial/USB
28
+ email: gabriel.sambarino@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/sds011.rb
34
+ homepage: https://github.com/chrean/ruby-sds011
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.2.3
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: SDS011 gem
57
+ test_files: []