nRF24-ruby 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: UTF-8
3
+
4
+
5
+ require "pp"
6
+ require 'time'
7
+ require 'nRF24-ruby'
8
+
9
+ r=NRF24.new id: :eka, ce: 27,cs: 22, irq: 17, chan:4, ack: false, mac: "A1:A1",mac_header: true
10
+
11
+ chan=0
12
+ loop do
13
+ r.wreg :RF_CH,chan
14
+ cd=0
15
+ 100.times do
16
+ sleep 0.01
17
+ s,d,_ = r.rreg :CD
18
+ if d==0x01
19
+ cd+=1
20
+ end
21
+ end
22
+ printf "chan=%d, cd=%d\n",chan,cd
23
+ chan+=1
24
+ chan=0 if chan>127
25
+ end
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: UTF-8
3
+
4
+
5
+ require "pp"
6
+ require 'socket'
7
+ require 'json'
8
+ require 'uri'
9
+ require 'ipaddr'
10
+ require 'time'
11
+ require 'thread'
12
+ require 'mqtt-sn-ruby'
13
+
14
+ local=false
15
+ if File.file? './lib/nRF24-ruby.rb'
16
+ require './lib/nRF24-ruby.rb'
17
+ puts "using local lib"
18
+ local=true
19
+ else
20
+ require 'nRF24-ruby'
21
+ end
22
+
23
+ puts "\nPure Ruby nRF24 <-> UDP MQTT-SN Bridge Starting..."
24
+
25
+ http=true
26
+ #http=false
27
+
28
+ if http
29
+ puts "Loading Http-server.. hold on.."
30
+ require 'minimal-http-ruby'
31
+ if local
32
+ minimal_http_server http_port: 8088, http_path: './http/'
33
+ else
34
+ minimal_http_server http_port: 8088, http_path: File.join( Gem.loaded_specs['nRF24-ruby'].full_gem_path, 'http/')
35
+ end
36
+ puts "\n"
37
+ end
38
+
39
+ def open_port uri_s
40
+ begin
41
+ uri = URI.parse(uri_s)
42
+ if uri.scheme== 'udp'
43
+ return [UDPSocket.new,uri.host,uri.port]
44
+ else
45
+ raise "Error: Cannot open socket for '#{uri_s}', unsupported scheme: '#{uri.scheme}'"
46
+ end
47
+ rescue => e
48
+ pp e.backtrace
49
+ raise "Error: Cannot open socket for '#{uri_s}': #{e}"
50
+ end
51
+ end
52
+
53
+ def poll_packet socket
54
+ begin
55
+ r,stuff=socket.recvfrom_nonblock(200) #get_packet --high level func!
56
+ client_ip=stuff[2]
57
+ client_port=stuff[1]
58
+ return [r,client_ip,client_port]
59
+ rescue IO::WaitReadable
60
+ sleep 0.1
61
+ rescue => e
62
+ puts "Error: receive thread died: #{e}"
63
+ pp e.backtrace
64
+ end
65
+ return nil
66
+ end
67
+
68
+ def poll_packet_block socket
69
+ #decide how to get data -- UDP-socket or FM-radio
70
+ r,stuff=socket.recvfrom(200) #get_packet --high level func!
71
+ client_ip=stuff[2]
72
+ client_port=stuff[1]
73
+ return [r,client_ip,client_port]
74
+ end
75
+
76
+ def send_raw_packet msg,socket,server,port
77
+ if socket
78
+ if socket.class.name=="UDPSocket"
79
+ socket.send(msg, 0, server, port)
80
+ else
81
+ a=msg.unpack('c*')
82
+ socket.send_q << {msg: a, to: server, socket: port,ack:false}
83
+ end
84
+ #MqttSN::hexdump msg
85
+ else
86
+ puts "Error: no socket at send_raw_packet"
87
+ end
88
+ end
89
+
90
+
91
+ r0=NRF24.new id: :eka, ce: 22,cs: 27, irq: 17, chan:4, ack: false, mac: "A9:A9",mac_header: true
92
+
93
+ s0,s0_host,s0_port=open_port("udp://20.20.20.21:1882") # our port for forwarder/broker connection
94
+
95
+
96
+ loopc=0;
97
+ sc=0;
98
+
99
+ def poll_packet_radio r
100
+ if not r.recv_q.empty? #get packets from broker's radio and send them to udp broker/forwader
101
+ msg=r.recv_q.pop
102
+ pac=msg[:msg].pack("c*")
103
+ len=msg[:msg][0]
104
+ if len<1 or len>30
105
+ puts "crap #{msg}"
106
+ return nil
107
+ end
108
+ if msg[:checksum]!=msg[:check]
109
+ puts "checksum error #{msg}"
110
+ return nil
111
+ end
112
+ pac=pac[0...len]
113
+ #puts "got #{msg}, '#{pac}'"
114
+ return [pac,msg[:from],msg[:socket] ]
115
+ end
116
+ return nil
117
+ end
118
+
119
+ @bcast_mac="45:45:45"
120
+ @bcast_period=20
121
+ Thread.new do
122
+ loop do
123
+ begin
124
+ msg=[MqttSN::ADVERTISE_TYPE,222,@bcast_period>>8,@bcast_period&0xff]
125
+ r = MqttSN::build_packet msg
126
+ send_raw_packet r,r0,@bcast_mac,0
127
+ puts "adv: #{msg} "
128
+ sleep @bcast_period
129
+ rescue => e
130
+ puts "Error: receive thread died: #{e}"
131
+ pp e.backtrace
132
+ end
133
+ end
134
+ end
135
+
136
+ puts "Gateway Main Loop Starts:"
137
+ ### gateway!!!
138
+ loop do
139
+ begin
140
+ if pac=poll_packet(s0)
141
+ r,client_ip,client_port=pac
142
+ puts "UDP(#{client_ip}:#{client_port})->RAD(#{@client_ip}:#{@client_port}): #{pac}"
143
+ send_raw_packet r,r0,@client_ip,@client_port
144
+ end
145
+
146
+ if pac=poll_packet_radio(r0)
147
+ r,@client_ip,@client_port=pac
148
+ puts "RAD(#{@client_ip}:#{@client_port})->UDP(#{s0_host}:#{s0_port}): #{pac}"
149
+ send_raw_packet r,s0,s0_host,s0_port
150
+ end
151
+
152
+ rescue => e
153
+ puts "Error: receive thread died: #{e}"
154
+ pp e.backtrace
155
+ end
156
+ sleep 0.01
157
+ end
158
+
@@ -0,0 +1,148 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: UTF-8
3
+
4
+
5
+ require "pp"
6
+ require 'socket'
7
+ require 'json'
8
+ require 'uri'
9
+ require 'ipaddr'
10
+ require 'time'
11
+ require 'thread'
12
+ require 'mqtt-sn-ruby'
13
+
14
+
15
+ local=false
16
+ if File.file? './lib/nRF24-ruby.rb'
17
+ require './lib/nRF24-ruby.rb'
18
+ puts "using local lib"
19
+ local=true
20
+ else
21
+ require 'nRF24-ruby'
22
+ end
23
+
24
+ puts "\nPure Ruby UDP <-> nRF24 Client Starting..."
25
+
26
+ http=true
27
+ #http=false
28
+
29
+ if http
30
+ puts "Loading Http-server.. hold on.."
31
+ require 'minimal-http-ruby'
32
+ if local
33
+ minimal_http_server http_port: 8088, http_path: './http/'
34
+ else
35
+ minimal_http_server http_port: 8088, http_path: File.join( Gem.loaded_specs['nRF24-ruby'].full_gem_path, 'http/')
36
+ end
37
+ puts "\n"
38
+ end
39
+
40
+ def open_port uri_s
41
+ begin
42
+ uri = URI.parse(uri_s)
43
+ if uri.scheme== 'udp'
44
+ return [UDPSocket.new,uri.host,uri.port]
45
+ else
46
+ raise "Error: Cannot open socket for '#{uri_s}', unsupported scheme: '#{uri.scheme}'"
47
+ end
48
+ rescue => e
49
+ pp e.backtrace
50
+ raise "Error: Cannot open socket for '#{uri_s}': #{e}"
51
+ end
52
+ end
53
+
54
+ def poll_packet socket
55
+ if socket.class.name=="UDPSocket"
56
+ begin
57
+ r,stuff=socket.recvfrom_nonblock(200) #get_packet --high level func!
58
+ client_ip=stuff[2]
59
+ client_port=stuff[1]
60
+ return [r,client_ip,client_port]
61
+ rescue IO::WaitReadable
62
+ sleep 0.1
63
+ rescue => e
64
+ puts "Error: receive thread died: #{e}"
65
+ pp e.backtrace
66
+ end
67
+ else
68
+ if not socket.recv_q.empty? #get packets from broker's radio and send them to udp broker/forwader
69
+ msg=socket.recv_q.pop
70
+ pac=msg[:msg].pack("c*")
71
+ len=msg[:msg][0]
72
+ if len<1 or len>30
73
+ puts "crap #{msg}"
74
+ return nil
75
+ end
76
+ if msg[:checksum]!=msg[:check]
77
+ puts "checksum error #{msg}"
78
+ return nil
79
+ end
80
+ pac=pac[0...len]
81
+ return [pac,msg[:from],msg[:socket] ]
82
+ end
83
+ end
84
+ return nil
85
+ end
86
+
87
+ def poll_packet_block socket
88
+ #decide how to get data -- UDP-socket or FM-radio
89
+ r,stuff=socket.recvfrom(200) #get_packet --high level func!
90
+ client_ip=stuff[2]
91
+ client_port=stuff[1]
92
+ return [r,client_ip,client_port]
93
+ end
94
+
95
+ def send_raw_packet msg,socket,server,port
96
+ if socket
97
+ if socket.class.name=="UDPSocket"
98
+ socket.send(msg, 0, server, port)
99
+ else
100
+ a=msg.unpack('c*')
101
+ socket.send_q << {msg: a, to: server, socket: port,ack:false}
102
+ end
103
+ #MqttSN::hexdump msg
104
+ else
105
+ puts "Error: no socket at send_raw_packet"
106
+ end
107
+ end
108
+
109
+
110
+ r=NRF24.new id: :eka, ce: 27,cs: 22, irq: 17, chan:4, ack: false, mac: "A1:A1",mac_header: true
111
+
112
+ s=UDPSocket.new
113
+ s.bind("0.0.0.0",5555) # our port for clients
114
+
115
+ puts "Main Loop Starts:"
116
+
117
+ loopc=0;
118
+ sc=0;
119
+
120
+
121
+ #CLIENT HERE
122
+ GW="A9:A9"
123
+ loop do
124
+ begin
125
+ if pac=poll_packet(s)
126
+ msg,@client_ip,@client_port=pac
127
+ puts "UDP(#{@client_ip}:#{@client_port})->RAD(#{GW}:#{3}): #{pac}"
128
+ send_raw_packet msg,r,GW,3
129
+ end
130
+
131
+ if pac=poll_packet(r)
132
+ msg,client_ip,client_port=pac
133
+ puts "RAD(#{client_ip}:#{client_port})->UDP(#{@client_ip}:#{@client_port}): #{pac}"
134
+ if client_port==:broadcast
135
+ mm=MqttSN::parse_message msg
136
+ puts "bcast! -- we handle it! #{mm}"
137
+ else
138
+ send_raw_packet msg,s,@client_ip,@client_port
139
+ end
140
+ end
141
+
142
+ rescue => e
143
+ puts "Error: receive thread died: #{e}"
144
+ pp e.backtrace
145
+ end
146
+ sleep 0.01
147
+ end
148
+
File without changes
data/lib/nRF24-ruby.rb CHANGED
@@ -370,6 +370,11 @@ class NRF24
370
370
  }
371
371
  @id=hash[:id]
372
372
  wreg :CONFIG,0x0b
373
+ _,check,_=rreg :CONFIG
374
+ if check!=0x0b
375
+ puts "No nRF24 Found [#{check}] @(cs:#{hash[:cs]}, ce:#{hash[:ce]}, mosi: 10, miso: 9, sckl: 11)! - Aborting"
376
+ exit(-1)
377
+ end
373
378
  rf_dr=(hash[:rf_dr]||1).to_i&0x01
374
379
  rf_pwr=(hash[:rf_pwr]||3).to_i&0x03
375
380
  lna_hcurr=(hash[:lna_hcurr]||1).to_i&0x01
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nRF24-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-30 00:00:00.000000000 Z
12
+ date: 2014-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minimal-http-ruby
@@ -43,17 +43,35 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 1.3.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: mqtt-sn-ruby
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.1.8
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.8
46
62
  description: ! 'Pure Ruby Driver and Utilitity with Http-server for the Ultra Cheap
47
63
  Radio Chip nRF24 '
48
64
  email: jalopuuverstas@gmail.com
49
65
  executables:
50
- - nRF24_udp.rb
66
+ - nRF24_dual_bridge.rb
67
+ - nRF24_MQTT-SN_bridge.rb
68
+ - nRF24_MQTT-SN_client.rb
69
+ - nRF24-scanner.rb
51
70
  extensions: []
52
71
  extra_rdoc_files: []
53
72
  files:
54
73
  - lib/nRF24-ruby.rb
55
74
  - examples/nRF24-demo.rb
56
- - bin/nRF24_udp.rb
57
75
  - http/json/logger.rb
58
76
  - http/json/nRF24.rb
59
77
  - http/json/action.rb
@@ -62,6 +80,10 @@ files:
62
80
  - http/css/nRF24.css
63
81
  - http/haml/index.html
64
82
  - http/haml/index.haml
83
+ - bin/nRF24_dual_bridge.rb
84
+ - bin/nRF24_MQTT-SN_bridge.rb
85
+ - bin/nRF24_MQTT-SN_client.rb
86
+ - bin/nRF24-scanner.rb
65
87
  homepage: https://github.com/arisi/nRF24-ruby
66
88
  licenses:
67
89
  - MIT