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-5 of 5 total  RSS 

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()

s60 http post python

From (http://www.python.org/doc/current/lib/httplib-examples.html)

==
import httplib, urllib

# replace with whatever you want POSTed...
gps_coords = urllib.urlencode({'param_1': 'value_1', 'param_2': 'value_2'})

# form contents
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

conn = httplib.HTTPConnection("location-server.com:80")
conn.request("POST", "/cgi-bin/collect_loc", gps_coords, headers)
response = conn.getresponse()
data = response.read()
conn.close()
==

Both httplib and urllib are shipped with PyS60. Of course, you will have to create the CGI script separately.

Cheers,
Sandeep
http://sandeep.weblogs.us/

Discover bluetooth devices around you

Pys60 has good bluetooth support from the beginning.
However, to discover another device, it require you to
interact with the app, choosing a device from the list.
PDIS has a library that help you list all devices silently.
# need to install these 2 modules from PDIS first
import aosocketnativenew
from aosocket.symbian.bt_device_discoverer import *

def callback(error, devices, cb_param=None):
    for address, name in devices:      
        print "Found: ", name, address
# You can get more data by importing socket and try 
# bt_discover(address) or bt_obex_discover(address)
# see details in official pys60 doc on socket module

lister = BtDeviceLister()
lister.discover_all(callback, None)

I summarize the code above from thejamo's example here.
His code is more complete with error checking.

Getting key press

In Pys60 1.2 there are 3 types for app.body, namely
- Canvas
- Text
- Listbox
They can recieve and process key press.
In older versions, they all have a bind method:
bind(event_code, callback)

event codes are defined in key_codes module
You can import some or all of them
from key_codes import EKeyLeftArrow, EKeySelect, EKey9, EKeyEdit
See diagram for 6630.

In the latest version, Canvas gains ability to respond
to events in more details. You can give it 2 callbacks
when creating a Canvas object.
c = Canvas(redraw_callback=None, event_callback=None)

event_callback will get a dict of the key event containing:
- 'type': one of EEventKeyDown, EEventKey, or EEventKeyUp
- 'keycode': the logical key
- 'scancode': the physical key
- 'modifier': probably about Shift, Ctrl ?

The simplest use is to detect when type=EEventKey
and use the 'keycode' value.
For advanced use, look at keyviewer.py example.

Nokia platform version

The way Nokia names their phone versions is rather confusing.
For example.
Series 60 Platform 2nd edition feature pack 2


I wonder why don't they just use s60.2.2
Now, at least they have renamed 'Series 60' to 'S60'
which is a good step in the right direction.

So, I will tag all new python for series 60 scripts with
'python, s60' instead of the old 'python, series60'.
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS