Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Send and receive SMS text messages with Ruby and a GSM/GPRS modem

require 'serialport'
require 'time'

class GSM
  
  SMSC = "+447785016005"  # SMSC for Vodafone UK - change for other networks

  def initialize(options = {})
    @port = SerialPort.new(options[:port] || 3, options[:baud] || 38400, options[:bits] || 8, options[:stop] || 1, SerialPort::NONE)
    @debug = options[:debug]
    cmd("AT")
    # Set to text mode
    cmd("AT+CMGF=1")
    # Set SMSC number
    cmd("AT+CSCA=\"#{SMSC}\"")    
  end
  
  def close
    @port.close
  end
  
  def cmd(cmd)
    @port.write(cmd + "\r")
    wait
  end
  
  def wait
    buffer = ''
    while IO.select([@port], [], [], 0.25)
      chr = @port.getc.chr;
      print chr if @debug == true
      buffer += chr
    end
    buffer
  end

  def send_sms(options)
    cmd("AT+CMGS=\"#{options[:number]}\"")
    cmd("#{options[:message][0..140]}#{26.chr}\r\r")
    sleep 3
    wait
    cmd("AT")
  end
  
  class SMS
    attr_accessor :id, :sender, :message, :connection
    attr_writer :time
    
    def initialize(params)
        @id = params[:id]; @sender = params[:sender]; @time = params[:time]; @message = params[:message]; @connection = params[:connection]
    end
    
    def delete
      @connection.cmd("AT+CMGD=#{@id}")
    end
    
    def time
      # This MAY need to be changed for non-UK situations, I'm not sure
      # how standardized SMS timestamps are..
      Time.parse(@time.sub(/(\d+)\D+(\d+)\D+(\d+)/, '\2/\3/20\1'))
    end
  end
  
  def messages
    sms = cmd("AT+CMGL=\"ALL\"")
    # Ugly, ugly, ugly!
    msgs = sms.scan(/\+CMGL\:\s*?(\d+)\,.*?\,\"(.+?)\"\,.*?\,\"(.+?)\".*?\n(.*)/)
    return nil unless msgs
    msgs.collect!{ |m| GSM::SMS.new(:connection => self, :id => m[0], :sender => m[1], :time => m[2], :message => m[3].chomp) } rescue nil
  end
end


destination_number = "+44 someone else"

p = GSM.new(:debug => false)

# Send a text message
p.send_sms(:number => destination_number, :message => "Test at #{Time.now}")

# Read text messages from phone
p.messages.each do |msg|
  puts "#{msg.id} - #{msg.time} - #{msg.sender} - #{msg.message}"
  # msg.delete
end

Python - SendSMS over BT and AT

// Send SMS over Bluetooth (AT Command)

import bluetooth

sockfd = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sockfd.connect(('00:00:00:00:00:00', 1)) # BT Address
sockfd.send('ATZ\r')
sockfd.send('AT+CMGF=1\r')
sockfd.send('AT+CSCA="+393359609600"\r') # Client TIM ITA
sockfd.send('AT+CMGS="+39xxxxxxxxxx"\r') # TO PhoneNumber
sockfd.send('Messaggio da mandare...\n')
sockfd.send(chr(26)) # CTRL+Z
sockfd.close()

gps gsm location python s60

Hi Guys,

I've just put together two smaller Python apps I've seen around in this discussion board.

The resulting app prints 1) info obtained by a BT gps (i.e. $GPRMC sentence, but may change as you like) and 2) GSM cell id.

My wish is to collect these info periodically (i.e. 2/3 minutes) and send them back via an HTTP POST to a specified host.... could anybody help? ;-)

Thanks

-Luca

____________________________________


# Simple BT App
#$GPRMC,161229.487,A,3723.2475,N,12158.3416,W,0.13,309.62,120598, ,*10


import socket,location,urllib

class BTReader:

def connect(self):
self.sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
address,services=socket.bt_discover()
print "Discovered: %s, %s"%(address,services)
target=(address,services.values()[0])
print "Connecting to "+str(target)
self.sock.connect(target)

def readposition(self):
try:
buffer=""
ch = self.sock.recv(1)
while(ch !='\n'):
buffer+=ch
ch = self.sock.recv(1)
# print buffer


if (buffer[0:6]=="$GPRMC"):
(GPRMC,utc,status,lat,latns,lon,lonew,knots,course,date,xx1,xx2)=buffer.split(",")
return "GPS (%s,%s,%s,%s,%s)"%(utc,lat+latns,lon+lonew,knots,course)
except Error:
return "Error!\n"
return ""

def close(self):
self.sock.close()

class GSM_loc:

def upd(self):
self.loc = location.gsm_location()
return "GSM (MCC:%s MNC:%s LAC:%s CID=%s)"%(self.loc[0], self.loc[1], self.loc[2], self.loc[3])


gsm = GSM_loc()

bt=BTReader()
bt.connect()

i=0
while (i<15):
print gsm.upd()
print bt.readposition()
i+=1

bt.close()
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS