commit
898d8d73a9
|
@ -27,6 +27,10 @@ Or install it yourself as:
|
||||||
puts sms
|
puts sms
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# method to send sms
|
||||||
|
sms = Biju::Sms.new(:phone_number => '+3312345678', :message => 'hello world')
|
||||||
|
@modem.send(sms)
|
||||||
|
|
||||||
@modem.close
|
@modem.close
|
||||||
```
|
```
|
||||||
## TODO
|
## TODO
|
||||||
|
|
|
@ -9,11 +9,20 @@ module Biju
|
||||||
#
|
#
|
||||||
# Biju::Modem.new(:port => '/dev/ttyUSB0')
|
# Biju::Modem.new(:port => '/dev/ttyUSB0')
|
||||||
#
|
#
|
||||||
def initialize(options={})
|
def initialize(options={}, &block)
|
||||||
raise Exception.new("Port is required") unless options[:port]
|
raise Exception.new("Port is required") unless options[:port]
|
||||||
|
pin = options.delete(:pin)
|
||||||
@connection = connection(options)
|
@connection = connection(options)
|
||||||
cmd("AT")
|
cmd("AT")
|
||||||
|
# initialize modem
|
||||||
|
cmd("ATZ")
|
||||||
|
# unlock pin code
|
||||||
|
cmd("AT+CPIN=\"#{pin}\"") if pin
|
||||||
|
# set SMS text mode
|
||||||
cmd("AT+CMGF=1")
|
cmd("AT+CMGF=1")
|
||||||
|
# set extended error reports
|
||||||
|
cmd('AT+CMEE=1')
|
||||||
|
#instance_eval &block if block_given?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Close the serial connection.
|
# Close the serial connection.
|
||||||
|
@ -22,8 +31,12 @@ module Biju
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return an Array of Sms if there is messages nad return nil if not.
|
# Return an Array of Sms if there is messages nad return nil if not.
|
||||||
def messages
|
def messages(which = "ALL")
|
||||||
sms = cmd("AT+CMGL=\"ALL\"")
|
# read message from all storage in the mobile phone (sim+mem)
|
||||||
|
cmd('AT+CPMS="MT"')
|
||||||
|
# get message list
|
||||||
|
sms = cmd('AT+CMGL="%s"' % which )
|
||||||
|
# collect messages
|
||||||
msgs = sms.scan(/\+CMGL\:\s*?(\d+)\,.*?\,\"(.+?)\"\,.*?\,\"(.+?)\".*?\n(.*)/)
|
msgs = sms.scan(/\+CMGL\:\s*?(\d+)\,.*?\,\"(.+?)\"\,.*?\,\"(.+?)\".*?\n(.*)/)
|
||||||
return nil unless msgs
|
return nil unless msgs
|
||||||
msgs.collect!{ |msg| Biju::Sms.new(:id => msg[0], :phone_number => msg[1], :datetime => msg[2], :message => msg[3].chomp) }
|
msgs.collect!{ |msg| Biju::Sms.new(:id => msg[0], :phone_number => msg[1], :datetime => msg[2], :message => msg[3].chomp) }
|
||||||
|
@ -35,6 +48,17 @@ module Biju
|
||||||
cmd("AT+CMGD=#{id}")
|
cmd("AT+CMGD=#{id}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def send(sms, options = {})
|
||||||
|
# initiate the sms, and wait for either
|
||||||
|
# the text prompt or an error message
|
||||||
|
cmd("AT+CMGS=\"#{sms.phone_number}\"")
|
||||||
|
|
||||||
|
# send the sms, and wait until
|
||||||
|
# it is accepted or rejected
|
||||||
|
cmd("#{sms.message}#{26.chr}")
|
||||||
|
# ... check reception
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def connection(options)
|
def connection(options)
|
||||||
port = options.delete(:port)
|
port = options.delete(:port)
|
||||||
|
@ -47,7 +71,8 @@ module Biju
|
||||||
|
|
||||||
def cmd(cmd)
|
def cmd(cmd)
|
||||||
@connection.write(cmd + "\r")
|
@connection.write(cmd + "\r")
|
||||||
wait
|
wait_str = wait
|
||||||
|
#p "#{cmd} --> #{wait_str}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def wait
|
def wait
|
||||||
|
|
Loading…
Reference in New Issue